本文整理汇总了C#中Excel.Application类的典型用法代码示例。如果您正苦于以下问题:C# Excel.Application类的具体用法?C# Excel.Application怎么用?C# Excel.Application使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Excel.Application类属于命名空间,在下文中一共展示了Excel.Application类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetExcelSheetName
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="pPath"></param>
/// <returns></returns>
private string GetExcelSheetName(string pPath)
{
//打开一个Excel应用
_excelApp = new Excel.Application();
if (_excelApp == null)
{
throw new Exception("打开Excel应用时发生错误!");
}
_books = _excelApp.Workbooks;
//打开一个现有的工作薄
_book = _books.Add(pPath);
_sheets = _book.Sheets;
//选择第一个Sheet页
_sheet = (Excel._Worksheet)_sheets.get_Item(1);
string sheetName = _sheet.Name;
ReleaseCOM(_sheet);
ReleaseCOM(_sheets);
ReleaseCOM(_book);
ReleaseCOM(_books);
_excelApp.Quit();
ReleaseCOM(_excelApp);
return sheetName;
}
示例2: CreateStudentBaseSimpleTable
public void CreateStudentBaseSimpleTable()
{
KillProcess();
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Open( //打开该文件
[email protected]"\report\ImportBaseTableForStudent(simple).xls",Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing));
m_objExcel.Visible = true;
}
catch(Exception e)
{
Util.WriteLog(e.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
}
}
示例3: StartupApp
// 启动Excel 程序
public bool StartupApp(bool visible)
{
// 检查是否有Excel正在运行
bool flag = false;
foreach (var item in Process.GetProcesses())
{
if (item.ProcessName == "EXCEL")
{
flag = true;
break;
}
}
if (!flag)
{
ExcelApp = new Excel.Application();
}
else
{
object obj = Marshal.GetActiveObject("Excel.Application"); // 引用已在执行的Excel
ExcelApp = obj as Excel.Application;
}
ExcelApp.Visible = visible; // Excel程序不可见
return true;
}
示例4: Button1_Click
private void Button1_Click(object sender, EventArgs e)
{
Excel.Application xl = new Excel.Application();
xl.Visible = true;
xl.Workbooks.Add("c:\\Pasta1.xlt");
xl.Cells[3, 4] = TextBox1.Text;
}
示例5: ExcelController
/// <summary>
/// エクセルデータの操作を行う
/// </summary>
/// <param name="path">xlsファイルパス</param>
public ExcelController(string path)
{
exPath = path;
oXls = new Excel.Application();
//Excel画面を表示しない
oXls.Visible = false;
//Excelファイルをオープンする
oWBook = (Excel.Workbook)(oXls.Workbooks.Open(exPath));
}
示例6: ExportExcel
public void ExportExcel()
{
try
{
//if (e.KeyCode != Keys.F12) return;
if (_autoExcel == false) return;
BindingSource bsTemp = this.PrimaryGrid.DataSource as BindingSource;
if (bsTemp == null) return;
DataTable tempDATA = bsTemp.DataSource as DataTable;
if (tempDATA == null) return;
DataTable DATA = tempDATA;
DATA.RejectChanges();
Excel.Application objExcel;
try
{
//Tìm instance Excel đang chạy.
objExcel = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch
{
//Không có instance nào của Excel đang chạy.
objExcel = new Excel.Application();
}
try
{
objExcel.Interactive = false;
objExcel.Interactive = true;
}
catch
{
//CrossShow2 msg = new CrossShow2(clsMx.Show);
//this.Invoke(msg, Qk.Settings.ctrmsgPrintError, eAlertType.CanhBao);
return;
}
objExcel.Visible = false;
string tmpFileXSL = Path.Combine(st.TEMP_DIR, Guid.NewGuid().ToString() + ".xls");
clsAll.ExtractXLS("AutoExcel.xls", tmpFileXSL);
objExcel.Workbooks.Open(tmpFileXSL);
object[,] objData = clsAll.DataTable2ArrayObjects(DATA);
string strRange = string.Format("A{0}:{1}{2}", 1, clsAll.GetExcelColumnLabel(DATA.Columns.Count), DATA.Rows.Count);
objExcel.Range[strRange].Value = objData;
objExcel.Visible = true;
objExcel.ActiveWorkbook.Save();
//objExcel.Worksheets.PrintPreview();
}
catch (Exception ex)
{
clsMx.Show(ex, this.Name);
}
}
示例7: LoadDataExcel
private void LoadDataExcel()
{
try
{
if (tbUploadFile.Text == "")
{
MessageBox.Show("Vui lòng load file để cập nhật!");
}
string status_name = "", status_code = "", result = "";
string resultT = "" , MSG = "";
conTTTSOA SOA = new conTTTSOA();
status_name = cbTrangthai.Text.ToString();
status_code = cbTrangthai.SelectedValue.ToString();
// string file = @"C:\Users\thongnt.NAMABANK0\Pictures\CẬP NHẬT TRẠNG THÁI ĐỒNG LOẠT FILE MẪU.xls";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(tbUploadFile.Text);
//Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
string contract_number = "";
int n_true = 0;
for (int i = 1; i <= xlRange.Count; i++)
{
if ((string)(xlRange.Cells[i, 1] as Excel.Range).Value2.ToString() == "")
{
return;
}
contract_number = (string)(xlRange.Cells[i, 1] as Excel.Range).Value2.ToString();
result = SOA.SOA_Change_Contr_Status("Web", contract_number, status_code, status_name, "82");
resultT = result.Substring(result.IndexOf("<result>") + "<result>".Length, result.IndexOf("</result>") - (result.IndexOf("<result>") + "<result>".Length));
MSG = result.Substring(result.IndexOf("<msg>") + "<msg>".Length, result.IndexOf("</msg>") - (result.IndexOf("<msg>") + "<msg>".Length));
if(resultT == "0")
{
n_true = n_true + 1;
lbKetqua.Text = MSG + " " + n_true;
}
if (i == xlRange.Count)
{
MessageBox.Show("Đã hoàn thành cập nhật!!!!");
return;
}
}
}
catch (Exception error)
{
MessageBox.Show("File excel không đúng định dạng!",error.Message);
}
}
示例8: NutritionPrint
public void NutritionPrint(string savePath)
{
try
{
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)m_objBooks.Open([email protected]"report\nutrition.xls",
m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,
m_objOpt,m_objOpt,m_objOpt);
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
int number = m_objSheets.Count;
writeCover();
writeStuAmount();
writeStuConvert();
writeACC1();
writeACC2();
writeElement();
m_objBook.SaveAs(savePath, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
}
catch(Exception ex)
{
Util.WriteLog(ex.Message,Util.EXCEPTION_LOG_TITLE);
}
finally
{
m_objRange = null;
m_objSheet = null;
m_objSheets = null;
m_objBook = null;
m_objBooks = null;
m_objExcel = null;
GC.Collect();
KillProcess();
}
}
示例9: NativeExcel_CreateExcel
public static void NativeExcel_CreateExcel(
DataTable Dt
, ClsExcel_Columns Columns
, string SaveFileName = ""
, Excel.XlFileFormat FileFormat = Excel.XlFileFormat.xlExcel5
, string Title = "")
{
Excel.Application Obj_Excel = new Excel.Application();
Excel.Workbook owbook = Obj_Excel.Workbooks.Add();
Excel.Worksheet owsheet = owbook.Worksheets.Add();
Int32 RowCt = 2;
Int32 ColCt = 1;
foreach (ClsExcel_Columns.Str_Columns? Obj in Columns.pObj)
{
owsheet.Cells[RowCt, ColCt].Value = Obj.Value.FieldDesc;
owsheet.Cells[RowCt, ColCt].Font.Bold = true;
Excel.Range Inner_ExRange =
owsheet.Range[
Do_Methods.GenerateChr(ColCt)
+ RowCt.ToString()
+ ":"
+ Do_Methods.GenerateChr(ColCt)
+ (RowCt + Dt.Rows.Count).ToString()];
Inner_ExRange.NumberFormat = Obj.Value.NumberFormat;
ColCt++;
}
RowCt++;
ColCt = 1;
Excel.Range ExRange =
owsheet.Range[
Do_Methods.GenerateChr(ColCt)
+ RowCt.ToString()
+ ":"
+ Do_Methods.GenerateChr(ColCt + Columns.pObj.Count)
+ (RowCt + Dt.Rows.Count - 1).ToString()];
ExRange.Value = Do_Methods.ConvertDataTo2DimArray(Dt, Columns.pFieldName);
owsheet.Range["A1;IV65536"].AutoFit();
if (SaveFileName == "")
{ SaveFileName = "Excel_File"; }
owsheet.Range["A1:A1"].Select();
owbook.SaveAs(SaveFileName, FileFormat);
}
示例10: button1_Click
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xl = new Excel.Application();
xl.Visible = true;
xl.Workbooks.Add("c:\\Pasta1.xlt");
xl.Cells[3, 4] = "oi";
object x = new object();
xl.Workbooks[0].Close(false, "", x);
xl.Quit();
}
示例11: ExcelControl
public ExcelControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
try
{
excelApp = new Excel.ApplicationClass();
excelApp.Visible = true;
Excel.Workbook book = excelApp.Workbooks.Open(@"C:\Documents and Settings\raghunandanr\Desktop\Benefit Design Modeler V3.28_ Blank.xls",Type.Missing,false,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
this.CreateGraphics().DrawString("ok ok",new System.Drawing.Font("Arial",25),Brushes.GreenYellow,10,10);
book.Close(false,Type.Missing,Type.Missing);
}
catch(Exception ex)
{
this.CreateGraphics().DrawString(ex.Message,new System.Drawing.Font("Arial",16),Brushes.Chocolate,10,10);
}
}
示例12: Upload
public void Upload()
{
string strPathnew = Server.MapPath("~/") + "UploadFile\\AcrTestfinalresult.xls";
try
{
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlWorkBook = new Excel.Application().Workbooks.Add(Missing.Value);
xlWorkBook.Application.Visible = true;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
string strPath = Server.MapPath("~/") + "UploadFile\\AcrTestfinal.xls";
StreamReader sr = new StreamReader(strPath); //Read the Excel Stream
string strTest = "";
int i = 1;
while (!sr.EndOfStream)
{
strTest = sr.ReadLine();
string[] strData = strTest.Split('\t');
int count = strData.Length;
for (int k = 1; k <= count; k++)
{
string str = strData[k - 1].Replace("\"", "");
if (k == 4) //1 based index of Column required to be changed
{
str = str.Insert(0, "'");
}
xlWorkSheet.Cells[i, k] = str;
}
i++;
}
sr.Close();
sr.Dispose();
xlWorkSheet.Columns.AutoFit();
xlWorkBook.SaveAs(strPathnew , Excel.XlFileFormat.xlExcel4, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkBook.Close(Missing.Value, strPathnew, Missing.Value);
}
catch
{ }
}
示例13: ExportToExcel
public static void ExportToExcel(DataTable dt, string type, string title, string thoiGian)
{
//Export to excel
try
{
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = dt;
excel.Cells[1, 1] = "Print Date :" + DateTime.Now.ToLongDateString();
excel.Cells[3, 1] = title;
excel.Cells[4, 1] = thoiGian;
for (int k = 0; k < table.Columns.Count; k++)
{
excel.Cells[6, k + 1] = table.Columns[k].ColumnName.ToString();
}
int ColumnIndex = 0;
int rowIndex = 5;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
((Excel.Range)excel.Cells[rowIndex + 1, ColumnIndex]).NumberFormat = "@"; //Format dạng text
//excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName]; //OLD 2016-01-04 (3)
excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName].ToString(); //2016-01-04 (3)
//((Excel.Range)excel.Cells[rowIndex + 1, ColumnIndex]).BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, 1);
}
}
excel.Visible = true;
Excel._Worksheet worksheet = (Excel._Worksheet)excel.ActiveSheet;
worksheet.Activate();
}
catch (Exception exml)
{
MessageBox.Show(exml.Message);
}
}
示例14: ToExcel
/// <summary>
/// Export DataList To Excel File
/// </summary>
/// <param name="dataList">List of Data need export</param>
/// <returns></returns>
public bool ToExcel(List<ArrayList> dataList)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(_temp_excel_file_path);
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets.get_Item(_sheet_number);
//SetValue and Fortmat base on template file
SetCells(xlWorkSheet, dataList);
//Save as new file
xlWorkSheet.SaveAs(_save_as_export_file_path);
//Dispose obj
xlWorkBook.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlWorkSheet = null;
xlWorkBook = null;
xlApp = null;
return true;
}
示例15: FinanceStatPrint
public void FinanceStatPrint(DataTable data, string className, DateTime date, string savePath)
{
DataTable dtPresents = new FinanInfoDataAccess().GetStudentPresents(date, className);
if (dtPresents == null || dtPresents.Rows.Count == 0)
{
throw new Exception("没有要使用的数据!");
}
else
{
data.Columns.AddRange(new DataColumn[]{ new DataColumn("小计", Type.GetType("System.Double")),
new DataColumn("stuPresent", Type.GetType("System.String")),
new DataColumn("stuAbsent", Type.GetType("System.String"))});
if (dtPresents.Rows.Count != data.Rows.Count)
{
throw new Exception("检测到数据完整性错误,请尝试重新生成数据!");
}
else
{
for (int i = 0; i < dtPresents.Rows.Count; i++)
{
data.Rows[i]["小计"] = 0;
data.Rows[i]["stuPresent"] = dtPresents.Rows[i]["times"];
data.Rows[i]["stuAbsent"] = dtPresents.Rows[i]["times_abs"];
}
}
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)m_objBooks.Open(excelPath + @"report\FinanceStat.xls",
m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,
m_objOpt,m_objOpt,m_objOpt);
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)m_objSheets.get_Item(1);
object[,] objData = new object[data.Rows.Count + 1, data.Columns.Count + 4];
for(int row = 0; row < data.Rows.Count; row++)
{
objData[row, 0] = row + 1;
for (int column = 0; column < data.Columns.Count - 3; column++)
{
if (column <= 2)
{
objData[row, column + 1] = data.Rows[row][column];
}
else if(column == 3)
{
objData[row, column + 1] = data.Rows[row][data.Columns.Count - 2];
objData[row, column + 2] = data.Rows[row][data.Columns.Count - 1];
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 3] = temp;
objData[data.Rows.Count, column + 3] = temp + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
}
else if (column >= 4 && column < 7)
{
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 3] = temp;
objData[data.Rows.Count, column + 3] = temp + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
}
else if (column == 7)
{
double temp1 = Convert.ToDouble(data.Rows[row][column - 4]) +
Convert.ToDouble(data.Rows[row][column - 3]) + Convert.ToDouble(data.Rows[row][column - 2]) +
Convert.ToDouble(data.Rows[row][column - 1]);
objData[row, column + 3] = temp1;
double temp2 = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 4] = temp2;
objData[data.Rows.Count, column + 3] = temp1 + (objData[data.Rows.Count, column + 3] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 3]));
objData[data.Rows.Count, column + 4] = temp2 + (objData[data.Rows.Count, column + 4] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 4]));
}
else
{
double temp = Convert.ToDouble(data.Rows[row][column]);
objData[row, column + 4] = temp;
objData[data.Rows.Count, column + 4] = temp + (objData[data.Rows.Count, column + 4] == null ? 0 :
Convert.ToDouble(objData[data.Rows.Count, column + 4]));
}
}
}
m_objRange = m_objSheet.get_Range("A6", m_objOpt);
m_objRange = m_objRange.get_Resize(data.Rows.Count + 1, data.Columns.Count + 1);
m_objRange.Value = objData;
m_objRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
m_objRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
m_objRange.WrapText = true;
m_objRange.Borders.LineStyle = System.Windows.Forms.BorderStyle.FixedSingle;
m_objRange.Font.Size = 10;
m_objRange = m_objSheet.get_Range("G3", m_objOpt);
m_objRange.Value = "各项费用";
m_objRange = m_objSheet.get_Range(m_objSheet.Cells[3, 7], m_objSheet.Cells[4, data.Columns.Count]);
m_objRange.Merge(m_objOpt);
m_objRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
m_objRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
m_objRange.WrapText = true;
//.........这里部分代码省略.........