NPOI将某个程序段耗时插入Excel

本文介绍了一个使用NPOI库在C#中生成Excel文件的方法,具体实现为将程序运行时间数据写入到指定的Excel表格中,并确保表格格式整齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NPOI将某个程序段耗时插入Excel

        public void GenCaptureRunTimeExcel(string ProStart, string ProEnd, string ProDuration)
        {
            try
            {

                /****
                 * 1.判断"./TempData/CaptureRunTime.xls"是否存在
                 * 2.如果存在,则计算有多少行
                 * 3.在创建下一行插入内容
                 * ***/
                /***
                 * 1.建表
                 * ***/
                HSSFWorkbook wk = new HSSFWorkbook();
                ISheet capture_run_time_sheet = wk.CreateSheet("CaptureRunTime");


                /***
                 * 2.定义单元格格式
                 * ***/
                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);
                }

                /***
                * 3.插入内容
                ***/
                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;

                /***
                 * 4.保存Excel
                 * ***/
                using (FileStream fs = File.OpenWrite("./TempData/CaptureRunTime.xls"))
                {
                    wk.Write(fs);
                }


            }
            catch (Exception exp)
            {
                MessageBox.Show("插入CaptureRunTime失败"+"--失败原因:"+exp.Message);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值