在ERP系統中,經常要將系統數據到出到EXCEL,再加以編輯,下面提供一段C#導出到EXCEL 的方法:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
// 將數據導出到EXCEL
namespace LBridgeAssistSystem.Class
{
class Excel
{
//法一 導出excel EXCEL中不包含表頭信息。
// DataSet 需要導出的數據集合 isShowExcle 導出EXCEL 后是否打開
public static bool DataSetToExcel(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;
int columnNumber = dataTable.Columns.Count;
if (rowNumber == 0)
{
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;//是否打开该Excel文件
//填充数据
for (int c = 0; c < rowNumber; c++)
{
for (int j = 0; j < columnNumber; j++)
{
excel.Cells[c + 1, j + 1] = dataTable.Rows[c].ItemArray[j];
}
}
return true;
}
// 導出excel EXCEL中包含表頭信息。
// DataSet 需要導出的數據集合 isShowExcle 導出EXCEL 后是否打開
public static bool DataSetToExcel_WithHead(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;
int rowIndex = 1;
int colIndex = 0;
if (rowNumber == 0)
{
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
//填充数据
foreach (DataRow row in dataTable.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName];
}
}
return true;
}
//導出EXCEL 速度較快
// DataSet 需要導出的數據集合 isShowExcle 導出EXCEL 后是否打開
public static bool DataSetToExcel_GaoSu(DataSet dataSet, bool isShowExcle)
{
DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;//不包括字段名
int columnNumber = dataTable.Columns.Count;
int colIndex = 0;
if (rowNumber == 0)
{
return false;
}
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//excel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
excel.Visible = isShowExcle;
//Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Worksheets[1];
Microsoft.Office.Interop.Excel.Range range;
//生成字段名称
foreach (DataColumn col in dataTable.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
object[,] objData = new object[rowNumber, columnNumber];
for (int r = 0; r < rowNumber; r++)
{
for (int c = 0; c < columnNumber; c++)
{
objData[r, c] = dataTable.Rows[r][c];
}
//Application.DoEvents();
}
// 写入Excel
range = worksheet.Range[excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]];
//range.NumberFormat = "@";//设置单元格为文本格式
range.Value2 = objData;
//設置區域格式
worksheet.Range[excel.Cells[2, 1], excel.Cells[rowNumber + 1, 1]].NumberFormat = "@";
return true;
}
}
}