NPOI创建Excel并导出数据列表
DataSet dsExReport = ds;//数据源
//创建NPOI对象
HSSFWorkbook book = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
//初始化 行和列
int rowIndex = 0;
int cellIndex = 0;
Row row;
Cell cell;
//创建sheet
Sheet sheet1 = book.CreateSheet("组织推荐职务变更申请");
//创建列名
row = sheet1.CreateRow(rowIndex++);
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("申请部门");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("申请人");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("申请时间");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("所属部门");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("现有职务");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("申请职务");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("申请理由");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("人事部门意见");
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue("状态");
if (dsExReport != null)
{
//循环读取数据并填充到行中
for (int i = 0; i
{
DataRow dr = dsExReport.Tables[0].Rows[i];
cellIndex = 0;
row = sheet1.CreateRow(rowIndex++);
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["ApplyDepName"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["ApplyUserName"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(string.Format(dr["ApplyDate"].ToString(), "yyyy-MM-dd"));
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["depName"].ToString() == "" ? "暂无关联部门" : dr["depName"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["Posname"].ToString() == "" ? "暂无关联职务" : dr["Posname"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["ChangePosName"].ToString() == "" ? "暂无关联职务" : dr["ChangePosName"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["changeContent"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["isCheckContent"].ToString());
cell = row.CreateCell(cellIndex++, CellType.STRING);
cell.SetCellValue(dr["CheckStatus"].ToString());
}
}
book.Write(ms);
Response.AddHeader("Content-Disposition", string.Format("p_w_upload;filename=PositionChangeZlist.xls"));
Response.ContentType = "application/ms-excel";
Response.BinaryWrite(ms.ToArray());
book = null;
ms.Close();
ms.Dispose();