C#操作Excel
2009-08-11 23:59
357 查看
刚好用到C#操作Excel,goole了一些资料,总结了一下,主要流程如下所示:
一、添加引用 添加com组件 Microsoft Office 11.0 Object Library 或者添加Excel.exe,引用默认路径为C:/Program Files/Microsoft Office/OFFICE11/Excel.exe
二、代码
public static void ExportToExcel(DataSet dataSet, string outputPath)
{
// Create the Excel Application object
ApplicationClass excelApp = new ApplicationClass();
// Create a new Excel Workbook
Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
int sheetIndex = 0;
// Copy each DataTable as a new Sheet
foreach (System.Data.DataTable dt in dataSet.Tables)
{
// Create a new Sheet
Worksheet excelSheet = (Worksheet) excelWorkbook.Sheets.Add(
excelWorkbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, XlSheetType.xlWorksheet);
excelSheet.Name = dt.TableName;
// Copy the column names (cell-by-cell)
for (int col = 0; col < dt.Columns.Count; col++) {
excelSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
}
((Range) excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
// Copy the values (cell-by-cell)
for (int col = 0; col < dt.Columns.Count; col++)
{
for (int row = 0; row < dt.Rows.Count; row++)
{
excelSheet.Cells[row + 2, col + 1] = dt.Rows[row].ItemArray[col];
}
}
}
// Save and Close the Workbook
excelWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelWorkbook.Close(true, Type.Missing, Type.Missing);
excelWorkbook = null;
// Release the Application object
excelApp.Quit();
excelApp = null;
// Collect the unreferenced objects
GC.Collect();
GC.WaitForPendingFinalizers();
}
一、添加引用 添加com组件 Microsoft Office 11.0 Object Library 或者添加Excel.exe,引用默认路径为C:/Program Files/Microsoft Office/OFFICE11/Excel.exe
二、代码
public static void ExportToExcel(DataSet dataSet, string outputPath)
{
// Create the Excel Application object
ApplicationClass excelApp = new ApplicationClass();
// Create a new Excel Workbook
Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
int sheetIndex = 0;
// Copy each DataTable as a new Sheet
foreach (System.Data.DataTable dt in dataSet.Tables)
{
// Create a new Sheet
Worksheet excelSheet = (Worksheet) excelWorkbook.Sheets.Add(
excelWorkbook.Sheets.get_Item(++sheetIndex),
Type.Missing, 1, XlSheetType.xlWorksheet);
excelSheet.Name = dt.TableName;
// Copy the column names (cell-by-cell)
for (int col = 0; col < dt.Columns.Count; col++) {
excelSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName;
}
((Range) excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
// Copy the values (cell-by-cell)
for (int col = 0; col < dt.Columns.Count; col++)
{
for (int row = 0; row < dt.Rows.Count; row++)
{
excelSheet.Cells[row + 2, col + 1] = dt.Rows[row].ItemArray[col];
}
}
}
// Save and Close the Workbook
excelWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelWorkbook.Close(true, Type.Missing, Type.Missing);
excelWorkbook = null;
// Release the Application object
excelApp.Quit();
excelApp = null;
// Collect the unreferenced objects
GC.Collect();
GC.WaitForPendingFinalizers();
}
相关文章推荐
- c# 操作excel生成pdf
- C#操作Excel---技巧篇(上)
- MyXls组件操作Excel报表(C#)
- C#合并excel单元格,操作合并后的单元格 (winform导出excel)
- C#操作Excel数据导入和dataGridView重绘
- C# 操作Excel图形——绘制、读取、隐藏、删除图形
- C#对Excel的一些操作【一】
- C#操作Excel
- c#操作excel方式二:采用OleDB操作Excel文件
- C# 操作 Excel 文件(.xls 或 .xlsx)
- C#操作Excel总结
- C#操作Excel文件(读取Excel,写入Excel)
- c#操作wps中的excel
- C#操作excel
- C#对Excel的样式操作
- C#操作excel(多种方法比较)
- C#操作Excel:不存在类型或命名空间名称“Interop”
- C#与excel互操作的错误无法将类型为“Excel.ApplicationClass”的COM 对象强制转换为接口类型“Excel._Application”
- c# 处理EXCEL 操作包括查询以及增改
- C#在客户端和服务端操作Excel文件