zoukankan      html  css  js  c++  java
  • 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流中
    
                    }
    
                }
  • 相关阅读:
    JS获取URL中参数值(QueryString)的4种方法
    js 字符串转换成数字的 三种方法
    jquery.cookie 介绍 和 用法
    JS的document.all函数使用 示例
    JS按回车键实现登录的三种方法
    js 字符串转换成数字的三种方法
    GitHub访问速度慢的一种优化方法
    git commit 提交的时候报错husky > pre-commit hook failed (add --no-verify to bypass)
    git回退
    git commit 提交的时候报错husky > pre-commit hook failed (add --no-verify to bypass)(解决办法)
  • 原文地址:https://www.cnblogs.com/kexb/p/5850418.html
Copyright © 2011-2022 走看看