• NPOI读写Excel sheet操作


    QueryInfo dataInfo = new QueryInfo();
                dataInfo.CustomSQL = $@"
                                select t1.name name,t1.url url from sys_menu t1
                                start with t1.parent_id =
                                (
                                  select t2.id from sys_menu t2 where t2.name ='交易源数据'
    
                                )
                                connect by t1.parent_id=t1.id
                      ";
                var descpsInfo = new QueryInfo();
    
               
                var dataTable = Dao.ExcuteDataSet(dataInfo).Tables[0];
                foreach (DataRow row in dataTable.Rows)
                {
                    var china_name = row["name"]==null?"":row["name"].ToString();
                    var en_name = Holworth.Utility.ListAndTableExtension.ConvertToTableColumnName
                        (row["url"].ToString().Split('/')[row["url"].ToString().Split('/').Length - 1].Replace("Manage.aspx", ""));
                    descpsInfo.CustomSQL = string.Format(@"
                                select (select t.COMMENTS from all_tab_comments t where t.TABLE_NAME='{0}' AND t.OWNER='NETHRA') tComments,
                                 tt.TABLE_NAME ,
                                tt.COLUMN_NAME ,
                                 (select t2.COMMENTS cComments from all_col_comments t2 where t2.column_name=tt.column_name and t2.OWNER='NETHRA' AND T2.TABLE_NAME='{0}') cComments, 
                                tt.DATA_TYPE,tt.DATA_LENGTH,tt.DATA_PRECISION from all_tab_columns tt where tt.OWNER='NETHRA' AND TT.TABLE_NAME='{0}'                 
                    ",en_name);
    
                    //descpsInfo.CustomSQL=string.Format(descpsInfo.CustomSQL,en_name);
                    var dic = Dao.ExcuteDataSet(descpsInfo).Tables[0].AsEnumerable().Select
                   (
                   x => new
                   {
                       tableName = x["TABLE_NAME"]==null?"":x["TABLE_NAME"].ToString(),
                       tComments = x["tcomments"]==null?"":x["tcomments"].ToString(),
                       columnsName = x["COLUMN_NAME"]==null?"":x["COLUMN_NAME"].ToString(),
                       cComments = x["cComments"]==null?"":x["cComments"].ToString(),
                       dataType = x["DATA_TYPE"].ToString(),
                       columnDataLength = x["DATA_LENGTH"].ToString(),
                       columnDataPrecious = x["DATA_PRECISION"].ToString(),
    
                   }
    
                   ).ToList();
                    
                    //1.创建excel文件
                    string file = @"C:UsersadminDesktop本周纪要3.xlsx";
    
                    XSSFWorkbook workbook = null;
                    if (!File.Exists(file))
                    {
                        workbook = new XSSFWorkbook();
                    }
                    else
                    {
                        
                        workbook = new XSSFWorkbook(File.OpenRead(file));
                    }
                    
                    // 新建一个Excel页签
                    //1.1创建固定部分前两行
                    
                    var sheet = workbook.CreateSheet(china_name);
    
                    IRow row1 = sheet.CreateRow(0); //创建sheet页的第0行(索引从0開始)    
                    int start = 2;
                    //1.1.1表头
                    row1.CreateCell(0, CellType.String).SetCellValue("中文表名");
                    row1.CreateCell(1, CellType.String).SetCellValue(china_name);
                    row1.CreateCell(2, CellType.String).SetCellValue("英文表名");
                    row1.CreateCell(3, CellType.String).SetCellValue(en_name);
                    row1.CreateCell(4, CellType.String).SetCellValue("主键");  
                    row1.CreateCell(5, CellType.String).SetCellValue("备注");  //1.1.2列头
                    IRow row2 = sheet.CreateRow(1);
                    row2.CreateCell(0, CellType.String).SetCellValue("英文名称");
                    row2.CreateCell(1, CellType.String).SetCellValue("中文名称");
                    row2.CreateCell(2, CellType.String).SetCellValue("数据类型");
                    row2.CreateCell(3, CellType.String).SetCellValue("是否为空");
                    row2.CreateCell(4, CellType.String).SetCellValue("");  
                    row2.CreateCell(5, CellType.String).SetCellValue("");  
                  
                    using (Stream stream =File.OpenWrite(file))
                    {
    
                        foreach (var item in dic)
                        {
                            var columnName = item.columnsName;
                            var cComment = item.cComments;
                            var cDataType = item.dataType;
                            var cLength = item.columnDataLength;
                            var cPrecious = item.columnDataPrecious;
                            //是否为空需要人判断默认为空
                            IRow tmpRow = sheet.CreateRow(start++);
                            //上述信息写入excel文件
                            tmpRow.CreateCell(0, CellType.String).SetCellValue(columnName);
                            tmpRow.CreateCell(1, CellType.String).SetCellValue(cComment);
                            tmpRow.CreateCell(2, CellType.String).SetCellValue(cDataType);
                            tmpRow.CreateCell(3, CellType.String).SetCellValue("Y");
                            tmpRow.CreateCell(4, CellType.String).SetCellValue("");  
                            tmpRow.CreateCell(5, CellType.String).SetCellValue(""); 
    
                            
                        }
                        start = 2;
                        workbook.Write(stream); //将这个workbook文件写入到stream流中
    
                    }
    
                }
  • 相关阅读:
    python常用模块
    清除在Windows下访问共享文件夹时的登录信息
    CentOS 6.3下Samba服务器的安装与配置
    textarea定位光标
    js中apply方法的使用
    js call方法
    linux 安装svn,并设置钩子来同步更新
    mysql给root开启远程访问权限,修改root密码
    在VMware的Linux系统上安装Redis
    (转)NoSQL——Redis在win7下安装配置的学习一
  • 原文地址:https://www.cnblogs.com/kexb/p/5850418.html
Copyright © 2020-2023  润新知