NPOI将某个程序段耗时插入Excel
public void GenCaptureRunTimeExcel(string ProStart, string ProEnd, string ProDuration)
{
try
{
HSSFWorkbook wk = new HSSFWorkbook();
ISheet capture_run_time_sheet = wk.CreateSheet("CaptureRunTime");
ICellStyle style_1 = wk.CreateCellStyle();
style_1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
style_1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
for (int i = 0; i < 100; i++)
{
capture_run_time_sheet.SetColumnWidth(i, 23 * 256);
}
IRow dataRow;
ICell cell;
dataRow = capture_run_time_sheet.CreateRow(0);
cell = dataRow.CreateCell(0);
cell.SetCellValue("ProStart");
cell.CellStyle = style_1;
cell = dataRow.CreateCell(1);
cell.SetCellValue("ProEnd");
cell.CellStyle = style_1;
cell = dataRow.CreateCell(2);
cell.SetCellValue("ProDuration");
cell.CellStyle = style_1;
dataRow = capture_run_time_sheet.CreateRow(1);
cell = dataRow.CreateCell(0);
cell.SetCellValue(ProStart);
cell.CellStyle = style_1;
cell = dataRow.CreateCell(1);
cell.SetCellValue(ProEnd);
cell.CellStyle = style_1;
cell = dataRow.CreateCell(2);
cell.SetCellValue(ProDuration);
cell.CellStyle = style_1;
using (FileStream fs = File.OpenWrite("./TempData/CaptureRunTime.xls"))
{
wk.Write(fs);
}
}
catch (Exception exp)
{
MessageBox.Show("插入CaptureRunTime失败"+"--失败原因:"+exp.Message);
}
}