首页 c npoi读写excel(C读写 NPOI Excel)

c npoi读写excel(C读写 NPOI Excel)

举报
开通vip

c npoi读写excel(C读写 NPOI Excel)c npoi读写excel(C读写 NPOI Excel) c# npoi读写excel(C读写# NPOI Excel) 使用系统; 使用system.collections.generic; 使用系统; 使用NPOI; 使用npoi.hssf; 使用npoi.hssf.util; 使用npoi.hssf.usermodel; 使用System.IO; 使用系统数据; 使用系统文本; / / / operatorexcel的摘要说明 // / / /简单的导出,导入Excel ...

c  npoi读写excel(C读写  NPOI Excel)
c npoi读写excel(C读写 NPOI Excel) c# npoi读写excel(C读写# NPOI Excel) 使用系统; 使用system.collections.generic; 使用系统; 使用NPOI; 使用npoi.hssf; 使用npoi.hssf.util; 使用npoi.hssf.usermodel; 使用System.IO; 使用系统数据; 使用系统文本; < <摘要> / / / operatorexcel的摘要说明 // < <摘要> / / /简单的导出,导入Excel // operatorexcel公共类 { 公共operatorexcel() { } < <摘要> / / /读取Excel到DataTable // / / / <参数名=“excelpath”> Excel路径< /参数> / / / <参数名=“theaderidx”> Excel表头行号< /参数> / / / <返回> > < /返回DataTable DataTable InputExcel(excelpath公共静态字符串,字符串sheetname,int theaderidx) { 使用(流=新的FILESTREAM(excelpath,我不知道。打开,读)) { 尝试 { npoi.hssf.usermodel.hssfworkbook书=新hssfworkbook(S); 返回InputExcel(书、sheetname,theaderidx); } 抓住 { 返回null; } } } < <摘要> / / /导入Excel到DataTable // / / / <参数名=“> Excel路径< /参数> / / / <参数名=“theaderidx”> Excel表头行号< /参数> / / / <返回> > < /返回DataTable 公共静态DataTable InputExcel(流、字符串sheetname,int theaderidx) { npoi.hssf.usermodel.hssfworkbook书=新hssfworkbook(S); 返回InputExcel(书、sheetname,theaderidx); } < <摘要> / / /读取Excel到DataTable // / / / <参数名=“书”> Excel工作薄< /参数> / / / <参数名=“sheetname”>表名称< /参数> / / / <参数名=“theaderidx”> Excel中对应的表头行号(从0开始,要求都是字符串格式)< /参数> / / / <返回> > < /返回DataTable返回 私有静态DataTable InputExcel(hssfworkbook书,串sheetname,int theaderidx) { system.text.regularexpressions.regex Re =新系统。文字。使用正则表达式正则表达式(@“\n”); npoi.ss.usermodel.sheet表=书。getsheet(sheetname); system.collections.ienumerator行=表。getrowenumerator(); / / /创建DataTable DataTable dt =新datatable(); npoi.ss.usermodel.row rowheader =表。GetRow(theaderidx); 为(int i = 0;i < rowheader.lastcellnum;i++) { 字符串标题= rowheader。GetCell StringCellValue(我); 列列=新的数据列(标题。trim()); 列(添加); } 而(行。movenext()) { npoi.ss.usermodel.row行=(NPOI。SS。用户模型。rows.current 行); 如果(row.rownum <= theaderidx) 继续; DataRow博士= newrow() DT; beginedit()博士; 为(int i = 0;i < row.lastcellnum;i++) { npoi.ss.usermodel.cell细胞=行。GetCell(我); 如果(单元格= NULL) 继续; 开关(细胞。CellType) { 案例 全员育人导师制案例信息技术应用案例心得信息技术教学案例综合实践活动案例我余额宝案例 NPOI.SS.UserModel.CellType.BLANK: “博士”; 打破; 案例NPOI.SS.UserModel.CellType.BOOLEAN: [我] = cell.booleancellvalue博士; 打破; 案例NPOI.SS.UserModel.CellType.NUMERIC: 博士[我] =重新更换(电池。tostring(),trim()”); 打破; 案例NPOI.SS.UserModel.CellType.STRING: 博士[我] =(重新更换(cell.stringcellvalue,”)。取代(“”, “”))。trim(); 打破; 案例NPOI.SS.UserModel.CellType.ERROR: [我] = cell.errorcellvalue博士; 打破; 案例NPOI.SS.UserModel.CellType.FORMULA: [我] = cell.numericcellvalue博士; 打破; 违约: [我] = cell.cellformula博士; 打破; } } endedit()博士; 行添加(DR); } 返回DT; } < <摘要> / / /创建excelbook // / / / <参数名=“DT”> > < /参数表 / / / <参数名=“sheetname”>表名称< /参数> / / / <参数名=“firsttitle”>表标题< /参数> / / / <返回> > < /返回hssfworkbook 公共静态HSSFWorkbook CreateBook(DataTable dt,sheetname firsttitle字符串,字符串) { npoi.hssf.usermodel.hssfworkbook书=新hssfworkbook(); npoi.ss.usermodel.sheet表=书。CreateSheet(sheetname); / / / /样式 npoi.ss.usermodel.cellstyle壳型=书。createcellstyle(); 对齐= npoi.ss.usermodel.horizontalalignment.center壳型; cellstyle.fillbackgroundcolor = npoi.hssf.util.hssfcolor.red.index; 壳型。 Verticalalignment = npoi.ss.usermodel.verticalalignment.center; Npoi.ss.usermodel.font font = book.createfont (); Font.fontheightinpoints = 12; Cellstyle.fillbackgroundcolor = npoi.hssf.util.hssfcolor.sky _ blue.index; Cellstyle.fillforegroundcolor = npoi.hssf.util.hssfcolor.sky _ blue.index; Cellstyle.setfont (font); Cellstyle.borderbottom = npoi.ss.usermodel.cellbordertype.thin; Cellstyle.borderleft = npoi.ss.usermodel.cellbordertype.thin; Cellstyle.borderright = npoi.ss.usermodel.cellbordertype.thin; Cellstyle.bordertop = npoi.ss.usermodel.cellbordertype.thin; Cellstyle.bottombordercolor = npoi.hssf.util.hssfcolor.black.index; Cellstyle.topbordercolor = npoi.hssf.util.hssfcolor.black.index; Cellstyle.rightbordercolor = npoi.hssf.util.hssfcolor.black.index; Cellstyle.leftbordercolor = npoi.hssf.util.hssfcolor.black.index; / / / 创建一级表头 Npoi.ss.usermodel.row firstrow = sheet.createrow (0); Firstrow.heightinpoints = 16; Npoi.ss.usermodel.cell cell = firstrow.createcell (0); Cell.setcellvalue (firsttitle); Cell.cellstyle = cellstyle; Sheet.addmergedregion (New npoi.ss.util.cellrangeaddress (0, 0, 0, dt.columns.count - 1)); / / / 创建二级表头 Npoi.ss.usermodel.row secondrow = sheet.createrow (1); Secondrow.heightinpoints = 16; Npoi.ss.usermodel.cell tmpcell; For (int i = 0; I < dt.columns.count; I + +) { Tmpcell = secondrow.createcell (I); String colname = dt.columns [i].Columnname; Tmpcell.cellstyle = cellstyle; Tmpcell.setcellvalue (colname); / / 设置列宽度 Sheet.setcolumnwidth (I, (colname.length + 4) * 600); } / / / 填充数据 Int j = 0; Npoi.ss.usermodel.font font1 = book.createfont (); Font1.fontheightinpoints = 10; Npoi.ss.usermodel.cellstyle cellstyle1 = book.createcellstyle (); Cellstyle1.clonestylefrom (cellstyle); Cellstyle1.setfont (font1); / / cellstyle1.fillbackgroundcolor = npoi.hssf.util.hssfcolor.white.index; / / cellstyle1.fillforegroundcolor = npoi.hssf.util.hssfcolor.white.index; Foreach (DataRow DR in dt.rows) { Npoi.ss.usermodel.row sheet.createrow tmprow = (2 + (J); J + +; For (int i = 0; I < DT. 列。计数;i + +) { 字符串值= DR [ i ]~=零,博士[我]。tostring():“[零]”; 如果(值)~=“[空]) { npoi.ss.usermodel.cell tmpcell = tmprow。CreateCell(我); tmpcell.cellstyle = cellstyle1; tmpcell setcellvalue(价值); } } } 还书; } < <摘要> / / /根据传入路径创建一个Excel // / / / <参数名=“DT”> DataTable数据< /参数> / / / <参数名=“文件路径”>路径(含文件名)< /参数> public static void outputexcel(DataTable dt,sheetname字符串字符串,firsttitle,字符串的文件路径) { npoi.hssf.usermodel.hssfworkbook书= createbook(DT,sheetname,firsttitle); 使用(FileStream FS =新的FILESTREAM(文件路径,我不知道。创造。写)) { 书写作(FS); } } < <摘要> / / /将DataTable创建为Excel,以内存流的形式返回 // / / / <参数名=“DT”> > < /参数表 / / / <参数名=“sheetname”>表名称< /参数> / / / <参数名=“firsttitle”>表标题< /参数> / / / <返回> MemoryStream < /返回> 公共静态MemoryStream outputexcelonstream(DataTable dt,sheetname字符串,字符串firsttitle) { MemoryStream Mstream =新memorystream(); npoi.hssf.usermodel.hssfworkbook书= createbook(DT,sheetname, firsttitle); 本书的编写(Mstream); 返回Mstream; } }
本文档为【c npoi读写excel(C读写 NPOI Excel)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_574951
暂无简介~
格式:doc
大小:29KB
软件:Word
页数:11
分类:生活休闲
上传时间:2018-04-03
浏览量:61