NPOI 通过excel模板写入数据并导出

     HSSFWorkbook workbook = null;
                    string FileName = @"D:\XX\CN\" + datetime + ".xls";
                    using (FileStream fs = File.Open(FileName ,FileMode.Open, FileAccess.Read))
                    {
                        workbook = new HSSFWorkbook(fs);

                        ISheet sheet = null;
                        sheet = workbook.GetSheetAt(0);
                        
                        IRow row = null;
                        int rowCount = sheet.LastRowNum + 1;
                        foreach (var item in records)
                        {
                            row = sheet.CreateRow(rowCount);
                            row.CreateCell(0).SetCellValue(item.StationId);
                            row.CreateCell(1).SetCellValue(item.DataBatchId);
                            row.CreateCell(2).SetCellValue(item.CustomerId);
                            row.CreateCell(3).SetCellValue(item.SKU);
                            row.CreateCell(4).SetCellValue(item.TraceCode);
                            row.CreateCell(5).SetCellValue(item.DataKey);
                            row.CreateCell(6).SetCellValue(item.DataValue);
                            row.CreateCell(7).SetCellValue(item.CreatedDate);
                            rowCount++;
                        }
                        sheet.ForceFormulaRecalculation = true;
                        using (FileStream filess = File.OpenWrite(FileName))
                        {
                            workbook.Write(filess);
                        }
                        
                        MessageBox.Show("汇出成功!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    }  

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wsx5edc/article/details/79156364
个人分类: C#
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭