阅读301 返回首页    go 阿里云 go 技术社区[云栖]


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

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


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

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

最后更新:2017-04-02 06:52:14

  上一篇:go myeclipse优化方案 myeclipse 10 优化
  下一篇:go 马士兵 正则表达式的学习(上)