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);
                    }  

你可能感兴趣的:(C#)