Excel模板导出(针对复杂报表的一种解决方式)

来源:互联网 发布:淘宝直播权限怎么开通 编辑:程序博客网 时间:2024/05/15 00:07

比如需导出如下形式的报表:


里面数据字段分类很多,又含公式统计等情况。

解决方案:利用NPOI组件,制作导出模板,对单元格精确控制,通过读取单元格里的模板字段,进行数据匹配替换;模板形式如下:


核心方法

    /// <summary>        /// 根据Excel模板单元格内容,找出单元格,并设置单元格的值        /// </summary>        /// <param name="sheet">ExcelSheet</param>        /// <param name="rowIndex">行索引</param>        /// <param name="cellTemplateValue">模板内容</param>        /// <param name="cellFillValue">单元格值</param>        ///  <param name="conNextRow">是否承接下一行,即:填充下一行单元格模板内容</param>        public static void SetCellValueByTemplateStr(HSSFSheet sheet, int rowIndex, string cellTemplateValue, object cellFillValue, bool conNextRow = true)        {            int cellStartIndex = sheet.GetRow(rowIndex).FirstCellNum;            int cellEndIndex = sheet.GetRow(rowIndex).LastCellNum;            bool find = false;            for (int i = cellStartIndex; i < cellEndIndex; i++)            {                if (find)                    break;                else                    if (i < (cellEndIndex - cellStartIndex) / 2)                    {                        #region 折半查找:前半段                        for (int j = i; j < (cellEndIndex - cellStartIndex) / 2; j++)                        {                            if (find)                                break;                            else                            {                                HSSFCell cell = sheet.GetRow(rowIndex).GetCell(j);                                if (cell != null)                                {                                    if (cell.CellType == 1)                                    {                                        string strCellValue = sheet.GetRow(rowIndex).GetCell(j).StringCellValue;                                        if (string.Compare(strCellValue, cellTemplateValue, true) == 0)                                        {                                            find = true;                                            Type type = cellFillValue.GetType();                                            switch (type.ToString())                                            {                                                case "System.String":                                                    string strValue = cellFillValue.ToString();                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(strValue);                                                    break;                                                case "System.Int32":                                                    int intValue = Convert.ToInt32(cellFillValue.ToString());                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(intValue);                                                    break;                                                case "System.Decimal":                                                    double decimalValue = Convert.ToDouble(cellFillValue.ToString());                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(decimalValue);                                                    break;                                            }                                            if (conNextRow)//如果承接下一行,则设置下一行单元格里的模板内容                                            {                                                sheet.GetRow(rowIndex + 1).GetCell(j).SetCellValue(cellTemplateValue);                                                sheet.GetRow(rowIndex + 1).GetCell(j).SetCellType(1);                                            }                                        }                                    }                                }                            }                        }                        #endregion                    }                    else                    {                        #region 折半查找:后半段                        for (int j = (cellEndIndex - cellStartIndex) / 2; j < cellEndIndex; j++)                        {                            if (find)                                break;                            else                            {                                HSSFCell cell = sheet.GetRow(rowIndex).GetCell(j);                                if (cell != null)                                {                                    if (cell.CellType == 1)                                    {                                        string strCellValue = sheet.GetRow(rowIndex).GetCell(j).StringCellValue;                                        if (string.Compare(strCellValue, cellTemplateValue, true) == 0)                                        {                                            find = true;                                            Type type = cellFillValue.GetType();                                            switch (type.ToString())                                            {                                                case "System.String":                                                    string strValue = cellFillValue.ToString();                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(strValue);                                                    break;                                                case "System.Int32":                                                    int intValue = Convert.ToInt32(cellFillValue.ToString());                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(intValue);                                                    break;                                                case "System.Decimal":                                                    double decimalValue = Convert.ToDouble(cellFillValue.ToString());                                                    sheet.GetRow(rowIndex).GetCell(j).SetCellValue(decimalValue);                                                    break;                                            }                                            if (conNextRow)//如果承接下一行,则设置下一行单元格里的模板内容                                            {                                                sheet.GetRow(rowIndex + 1).GetCell(j).SetCellValue(cellTemplateValue);                                                sheet.GetRow(rowIndex + 1).GetCell(j).SetCellType(1);                                            }                                        }                                    }                                }                            }                        }                        #endregion                    }            }        }
Demo 下载

原创粉丝点击