[C#] 使用NPOI将Datatable保存到Excel

 using (table)
{
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow();
// handling header.
foreach (DataColumn column in table.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
// handling value.
int rowIndex = ;
foreach (DataRow row in table.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); }
rowIndex++;
} workbook.Write(ms);
FileStream file = new FileStream("E://原始动态.xls", FileMode.Create);
workbook.Write(file);
file.Close();
workbook = null;
ms.Flush();
ms.Position = ;
}
上一篇:asp.net core 如何在Controller获取配置文件的值


下一篇:HTML|CSS之CSS选择器及样式