zoukankan      html  css  js  c++  java
  • NPOI 菜鸟实践行之根据指定的模板生成Excel 2003格式的文件 (一)

    感谢群里的各位朋友的指导和教学,感谢Tony Qu的热心指导,感谢阿修罗兄提供的Excelhelper类

    谈谈个人对Excel的理解,结合NPOI,个人水平一般般,菜鸟一只

    Excel 打开后,分为sheet-文件簿,Rows-行,Cell-单元格

    要基于模板生成EXCEL,首先我们要明白的是我们要填充的数据在整个Excel的sheet中是处于头,还是尾部,还是中间


    我是这样理解的,如果模板是含有风格的,比如说我这种含有三行内容的,计算的话,需要的模板列应该只包含下面这个表头(模板XLS)

    系統  XXXX系统 数据库表名 BC(XX表)
    序号 字  段  名  称 数 据 属 性 逻 辑 条 件  
      中   文 英    文 类型 长度 小数 单位 NULL (数 据 值 范 围) (备  注)
                       
                           

    而我们要生成的xls最终需要是这样BC.7z

    确定表头无需动态生成(固定的行和列),内容(行和列不确定),表尾格式(表尾头固定的行列,表尾内容行和列不确定)之后,我们就可以将代码分为生成表头,生成内容,生成表尾,接下来我就将生成内容的代码公布出来,给大家看看,这个代码是已经进行过重构的,是可以通用的哦,代码里你可以清晰的看到我生成索引内容的时候也是动态的,其他代码都是参考阿修罗兄提供的excelhelper类,如果需要查看,请到189925337(NPOI超级群)群里下载即可。

    #region 单元格数据生成通用类
            private static int CreateDataMethod(ISheet sheet1, ICellStyle style, DataTable dtGetRowTable, int rowIndex, HSSFCellStyle dateStyle)
            {
                foreach (DataRow row in dtGetRowTable.Rows)
                {
                    #region 填充内容
                    HSSFRow dataRow = sheet1.CreateRow(rowIndex) as HSSFRow;
                    foreach (DataColumn column in dtGetRowTable.Columns)
                    {
                        HSSFCell newCell = dataRow.CreateCell(column.Ordinal) as HSSFCell;
                        string drValue = row[column].ToString();
                        #region 字段类型处理
                        switch (column.DataType.ToString())
                        {
                            case "System.String": //字符串类型
                                newCell.SetCellValue(drValue);
                                newCell.CellStyle = style;
                                break;
                            case "System.DateTime": //日期类型
                                DateTime dateV;
                                DateTime.TryParse(drValue, out dateV);
                                newCell.SetCellValue(dateV);
                                newCell.CellStyle = dateStyle; //格式化显示
                                break;
                            case "System.Boolean": //布尔型
                                bool boolV = false;
                                bool.TryParse(drValue, out boolV);
                                newCell.SetCellValue(boolV);
                                newCell.CellStyle = style;
                                break;
                            case "System.Int16": //整型
                            case "System.Int32":
                            case "System.Int64":
                            case "System.Byte":
                                int intV = 0;
                                int.TryParse(drValue, out intV);
                                newCell.SetCellValue(intV);
                                newCell.CellStyle = style;
                                break;
                            case "System.Decimal": //浮点型
                            case "System.Double":
                                double doubV = 0;
                                double.TryParse(drValue, out doubV);
                                newCell.SetCellValue(doubV);
                                newCell.CellStyle = style;
                                break;
                            case "System.DBNull": //空值处理
                                newCell.SetCellValue("");
                                newCell.CellStyle = style;
                                break;
                            default:
                                newCell.SetCellValue(drValue);
                                newCell.CellStyle = style;
                                break;
                        }
                        #endregion
                        #region 插入留余空白列
                        int iLastCell = sheet1.GetRow(4).LastCellNum + 1 - dtGetRowTable.Columns.Count;
                        for (int i = 1; i < iLastCell; i++)
                        {
                            HSSFCell newNullCell = dataRow.CreateCell(newCell.ColumnIndex + i) as HSSFCell;
                            newNullCell.SetCellValue("");
                            newNullCell.CellStyle = style;
                        }
                        #endregion
    
                    }
    
                    #endregion
                    rowIndex++;
                }
                return rowIndex;
            } 
            #endregion

    int iLastCell = sheet1.GetRow(4).LastCellNum + 1 - dtGetRowTable.Columns.Count;//这个地方是根据模板表头所占数据计算的,这个是通用的留余做法。

    源文件提供下载:Form1.7z

    模板(带表头):表头(模板XLS)

    xls:BC.7z

    其实这就是一种简单的报表做法,下图所示是在实际项目当中使用的数据字典功能模块的效果图。

  • 相关阅读:
    修改centos7 DNS
    group by
    Oracle 删除表空间
    Oralce查看sid 、service_name
    Mysql修改lower_case_table_names
    Oralce静默安装
    Linux安装Mysql
    dbvisualier破解及使用
    Oracle扩容表空间
    lvm方式挂载盘及扩容
  • 原文地址:https://www.cnblogs.com/yzb305070/p/2483433.html
Copyright © 2011-2022 走看看