Excel模版建议把需要添加数据行的样式设置好

模版样式NPOI的使用Excel模板导出  可插入到指定行,导出后效果NPOI的使用Excel模板导出  可插入到指定行

 

【2017-11-22  对获取需插入数据的首行样式有时为空报错修改】

        /// <summary>
        /// 根据模版导出Excel
        /// </summary>
        /// <param name="templateFile">模版路径(包含后缀)  例:"~/Template/Exceltest.xls"</param>
        /// <param name="strFileName">文件名称(不包含后缀)  例:"Excel测试"</param>
        /// <param name="source">源DataTable</param>
        /// <param name="cellKes">需要导出的对应的列字段  例:string[] cellKes = { "Date","Remarks" };</param>
        /// <param name="rowIndex">从第几行开始创建数据行,第一行为0</param>
        /// <returns>是否导出成功</returns>
        public static string ExportScMeeting(string templateFile, string strFileName, DataTable source, string[] cellKes, int rowIndex)
        {
            templateFile = HttpContext.Current.Server.MapPath(templateFile);
            int cellCount = cellKes.Length;//总列数,第一列为0
            IWorkbook workbook = null;
            try
            {
                using (FileStream file = new FileStream(templateFile, FileMode.Open, FileAccess.Read))
                {
                    if (Path.GetExtension(templateFile) == ".xls")
                        workbook = new HSSFWorkbook(file);
                    else if (Path.GetExtension(templateFile) == ".xlsx")
                        workbook = new XSSFWorkbook(file);
                }
                ISheet sheet = workbook.GetSheetAt(0);
                if (sheet != null && source != null && source.Rows.Count > 0)
                {
                    IRow row; ICell cell;
                    //获取需插入数据的首行样式
                    IRow styleRow = sheet.GetRow(rowIndex);
                    if (styleRow == null)
                    {
                        for (int i = 0, len = source.Rows.Count; i < len; i++)
                        {
                            row = sheet.CreateRow(rowIndex);
                            //创建列并插入数据
                            for (int index = 0; index < cellCount; index++)
                            {
                                row.CreateCell(index)
                                    .SetCellValue(!(source.Rows[i][cellKes[index]] is DBNull) ? source.Rows[i][cellKes[index]].ToString() : string.Empty);
                            }
                            rowIndex++;
                        }
                    }
                    else
                    {
                        for (int i = 0, len = source.Rows.Count; i < len; i++)
                        {
                            row = sheet.CreateRow(rowIndex);
                            row.HeightInPoints = styleRow.HeightInPoints;
                            row.Height = styleRow.Height;
                            //创建列并插入数据
                            for (int index = 0; index < cellCount; index++)
                            {
                                cell = row.CreateCell(index, styleRow.GetCell(index).CellType);
                                cell.CellStyle = styleRow.GetCell(index).CellStyle;
                                cell.SetCellValue(!(source.Rows[i][cellKes[index]] is DBNull) ? source.Rows[i][cellKes[index]].ToString() : string.Empty);
                            }
                            rowIndex++;
                        }
                    }
                }
                return NPOIExport(strFileName + "." + templateFile.Split('.')[templateFile.Split('.').Length - 1], workbook);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
View Code

相关文章:

  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-04-27
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
相关资源
相似解决方案