看看这篇文章,它解释了如何使用 OpenXML SDK 将 DataTable 导出到 Excel。
http://lateral8.com/articles/2010/3/5/openxml-sdk-20-export-a-datatable-to-excel.aspx
以下是文章的主要内容:
创建 ExcelExport 类
现在在您的项目中创建一个名为 ExcelExport.cs 的新类文件。在文件开头添加以下引用:
使用系统;
使用 System.Data;
使用 System.Linq;
使用 DocumentFormat.OpenXml.Packaging;
使用 DocumentFormat.OpenXml.Spreadsheet;
接下来,将以下方法添加到文件中:
public void ExportDataTable(DataTable table, string exportFile)
{
//create the empty spreadsheet template and save the file
//using the class generated by the Productivity tool ExcelDocument excelDocument = new ExcelDocument();
excelDocument.CreatePackage(exportFile);
//populate the data into the spreadsheet using (SpreadsheetDocument spreadsheet =
SpreadsheetDocument.Open(exportFile, true))
{
WorkbookPart workbook = spreadsheet.WorkbookPart;
//create a reference to Sheet1 WorksheetPart worksheet = workbook.WorksheetParts.Last();
SheetData data = worksheet.Worksheet.GetFirstChild<SheetData>();
//add column names to the first row Row header = new Row();
header.RowIndex = (UInt32)1;
foreach (DataColumn column in table.Columns)
{
Cell headerCell = createTextCell(table.Columns.IndexOf(column) + 1, 1, column.ColumnName);
header.AppendChild(headerCell);
}
data.AppendChild(header);
//loop through each data row DataRow contentRow;
for (int i = 0;i < table.Rows.Count; i++)
{
contentRow = table.Rows[i];
data.AppendChild(createContentRow(contentRow, i + 2));
}
}
}
上述方法首先使用之前创建的 ExcelDocument 类创建一个新文件,该文件保存到 exportFile 参数中指定的位置。创建文件后,会发生两个主要循环。第一个循环遍历 DataTable 对象的列,并使用 createTextCell 方法为每个列名创建一个 Cell 对象:
private Cell createTextCell(int columnIndex, int rowIndex, object cellValue)
{
Cell cell = new Cell();
cell.DataType = CellValues.InlineString;
cell.CellReference = getColumnName(columnIndex) + rowIndex;
InlineString inlineString = new InlineString();
Text t = new Text();
t.Text = cellValue.ToString();
inlineString.AppendChild(t);
cell.AppendChild(inlineString);
return cell;
}
接下来,使用 createContentRow 方法将每一行附加到工作表:
private Row createContentRow(DataRow dataRow, int rowIndex)
{
Row row = new Row {RowIndex = (UInt32)rowIndex };
for (int i = 0; i < dataRow.Table.Columns.Count; i++)
{
Cell dataCell = createTextCell(i + 1, rowIndex, dataRow[i]);
row.AppendChild(dataCell);
}
return row;
}
createTextCell 方法使用另一种支持方法来完成将行和列映射到正确的单元格引用的工作,方法 getColumnName:
private string getColumnName(int columnIndex)
{
int dividend = columnIndex;
string columnName = String.Empty;
int modifier;
while (dividend > 0)
{
modifier = (dividend - 1) % 26;
columnName =
Convert.ToChar(65 + modifier).ToString() + columnName;
dividend = (int)((dividend - modifier) / 26);
}
return columnName;
}
此方法提供了一种将列索引号映射到 Excel 列名称 (A-Z) 的快速简便方法。 OpenXML SDK 中的 Cell 对象需要指定有效的 Excel 单元格引用(例如 A1、C2),因此此方法与行索引引用相结合来创建单元格引用。需要注意的是,这里的索引不是从零开始的。
最后,要实现上面的类,使用下面的代码:
//create DataTable from sample data DataSet sampleDataSet = new DataSet();
sampleDataSet.ReadXml(context.Server.MapPath("~/sampleData.xml"));
DataTable productsTable = sampleDataSet.Tables[0];
string exportFile = context.Server.MapPath("~/excelExport.xslx");
ExcelExport export = new ExcelExport();
export.ExportDataTable(productsTable, exportFile);