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

55 篇文章 1 订阅

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


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

解决方案:利用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 下载

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值