什么是npoi
该项目是位于http://poi.apache.org/的poi java项目的.net版本。poi是一个开源项目,可以帮助您读取/写入xls,doc,ppt文件。它有着广泛的应用。本文给大家介绍asp.net mvc利用npoi导入导出excel的问题。
因近期项目遇到所以记录一下:
首先导出excel:
首先引用npoi包
(action一定要用fileresult)
/// <summary> /// 批量导出本校第一批派位学生 /// </summary> /// <returns></returns> public fileresult exportstu2() { string schoolname = "401"; //创建excel文件的对象 npoi.hssf.usermodel.hssfworkbook book = new npoi.hssf.usermodel.hssfworkbook(); //添加一个sheet npoi.ss.usermodel.isheet sheet1 = book.createsheet("sheet1"); //获取list数据 list<tb_studentinfomodel> listraininfo = m_bll.getschoollistaatq(schoolname); //给sheet1添加第一行的头部标题 npoi.ss.usermodel.irow row1 = sheet1.createrow(0); row1.createcell(0).setcellvalue("电脑号"); row1.createcell(1).setcellvalue("姓名"); //将数据逐步写入sheet1各个行 for (int i = 0; i < listraininfo.count; i++) { npoi.ss.usermodel.irow rowtemp = sheet1.createrow(i + 1); rowtemp.createcell(0).setcellvalue(listraininfo[i].st_code.tostring()); rowtemp.createcell(1).setcellvalue(listraininfo[i].st_name.tostring()); } // 写入到客户端 system.io.memorystream ms = new system.io.memorystream(); book.write(ms); ms.seek(0, seekorigin.begin); return file(ms, "application/vnd.ms-excel", "第一批电脑派位生名册.xls"); }
前台直接写就可实现:
@html.actionlink("点击导出名册", "exportstu2")
下面说一下导入:
首先说一些前台吧,mvc上传注意必须加new { enctype = "multipart/form-data" }:
<td> 2、@using(@html.beginform("importstu", "proschool", formmethod.post, new { enctype = "multipart/form-data" })) { <text>选择上传文件:(工作表名为“sheet1”,“电脑号”在a1单元格。)</text> <input name="file" type="file" id="file" /> <input type="submit" name="upload" value="批量导入第一批电脑派位名册" /> } </td>
后台实现:只传路径得出datatable:
/// <summary> /// excel导入 /// </summary> /// <param name="filepath"></param> /// <returns></returns> public datatable importexcelfile(string filepath) { hssfworkbook hssfworkbook; #region//初始化信息 try { using (filestream file = new filestream(filepath, filemode.open, fileaccess.read)) { hssfworkbook = new hssfworkbook(file); } } catch (exception e) { throw e; } #endregion using (npoi.ss.usermodel.isheet sheet = hssfworkbook.getsheetat(0)) { datatable table = new datatable(); irow headerrow = sheet.getrow(0);//第一行为标题行 int cellcount = headerrow.lastcellnum;//lastcellnum = physicalnumberofcells int rowcount = sheet.lastrownum;//lastrownum = physicalnumberofrows - 1 //handling header. for (int i = headerrow.firstcellnum; i < cellcount; i++) { datacolumn column = new datacolumn(headerrow.getcell(i).stringcellvalue); table.columns.add(column); } for (int i = (sheet.firstrownum + 1); i <= rowcount; i++) { irow row = sheet.getrow(i); datarow datarow = table.newrow(); if (row != null) { for (int j = row.firstcellnum; j < cellcount; j++) { if (row.getcell(j) != null) datarow[j] = getcellvalue(row.getcell(j)); } } table.rows.add(datarow); } return table; } }
补充一个类
/// <summary> /// 根据excel列类型获取列的值 /// </summary> /// <param name="cell">excel列</param> /// <returns></returns> private static string getcellvalue(icell cell) { if (cell == null) return string.empty; switch (cell.celltype) { case celltype.blank: return string.empty; case celltype.boolean: return cell.booleancellvalue.tostring(); case celltype.error: return cell.errorcellvalue.tostring(); case celltype.numeric: case celltype.unknown: default: return cell.tostring();//this is a trick to get the correct value of the cell. numericcellvalue will return a numeric value no matter the cell value is a date or a number case celltype.string: return cell.stringcellvalue; case celltype.formula: try { hssfformulaevaluator e = new hssfformulaevaluator(cell.sheet.workbook); e.evaluateincell(cell); return cell.tostring(); } catch { return cell.numericcellvalue.tostring(); } } }
得到datatable后,就想怎么操作就怎么操作了
到此这篇关于asp.net mvc利用npoi导入导出excel的示例代码的文章就介绍到这了,更多相关asp.net mvc导入导出excel内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论