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[];
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 - ].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[].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(); //创建sheet页的第0行(索引从0開始)
int start = ;
//1.1.1表头
row1.CreateCell(, CellType.String).SetCellValue("中文表名");
row1.CreateCell(, CellType.String).SetCellValue(china_name);
row1.CreateCell(, CellType.String).SetCellValue("英文表名");
row1.CreateCell(, CellType.String).SetCellValue(en_name);
row1.CreateCell(, CellType.String).SetCellValue("主键");
row1.CreateCell(, CellType.String).SetCellValue("备注"); //1.1.2列头
IRow row2 = sheet.CreateRow();
row2.CreateCell(, CellType.String).SetCellValue("英文名称");
row2.CreateCell(, CellType.String).SetCellValue("中文名称");
row2.CreateCell(, CellType.String).SetCellValue("数据类型");
row2.CreateCell(, CellType.String).SetCellValue("是否为空");
row2.CreateCell(, CellType.String).SetCellValue("");
row2.CreateCell(, 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(, CellType.String).SetCellValue(columnName);
tmpRow.CreateCell(, CellType.String).SetCellValue(cComment);
tmpRow.CreateCell(, CellType.String).SetCellValue(cDataType);
tmpRow.CreateCell(, CellType.String).SetCellValue("Y");
tmpRow.CreateCell(, CellType.String).SetCellValue("");
tmpRow.CreateCell(, CellType.String).SetCellValue(""); }
start = ;
workbook.Write(stream); //将这个workbook文件写入到stream流中 } }
上一篇:su鉴定故障


下一篇:java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并