NPOI读写Excel sheet操作
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:\Users\admin\Desktop\本周纪要\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流中 } }
以上是关于NPOI读写Excel sheet操作的主要内容,如果未能解决你的问题,请参考以下文章