閱讀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 馬士兵 正則表達式的學習(上)