写excel文件
/// <summary>
/// Use NPO to write a datatable to the excel file
/// </summary>
/// <param name="tmpDataTable"></param>
/// <param name="strFileName"></param>
private static void DataTabletoExcel(DataTable dataTable, string strFileName)
{
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
int index = 0;
IRow titleRow = sheet.CreateRow(index++);
for (int i = 0; i < dataTable.Columns.Count; i++)
{
titleRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);
}
foreach (DataRow dr in dataTable.Rows)
{
IRow row = sheet.CreateRow(index++);
for (int i = 0; i < dataTable.Columns.Count; i++)
{
row.CreateCell(i).SetCellValue(dr[i].ToString());
}
}
FileStream fileStream = new FileStream(strFileName, FileMode.Create);
workbook.Write(fileStream);
fileStream.Close();
workbook = null;
}