Excel的批量导入相信是很多软件绕不过去的功能吧。使用poi实现Execl模板下载和批量导入的功能特此记录一下。
先上结果界面的代码如下。
<fieldset class="layui-elem-field">
<div class="layui-field-box">
<form action="uploadSubjectExcel.action" method="post" enctype="multipart/form-data">
<div>
<table>
<thead>
<tr>
<td>下载模版:</td>
<td><button type="button" class="layui-btn layui-btn-small"
onclick="downloadTemplate()">点击下载导入模版</button></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>上传文件:</td>
<td><input type="file" id = "myfile" name="myfile"></td>
</tr>
</thead>
</table>
</div>
<div class="modal-footer" style="text-align:center">
<input type="submit" value="上传" class="layui-btn layui-btn-big" />
</div>
</form>
</div>
</fieldset>
<script type="text/javascript">
function downloadTemplate() {
window.location.href = "downloadSubjectExecl.action";
}
</script>
这里使用的是layui,这个界面是通过一个点击这个文件按钮弹出一个界面实现的。
弹出js如下。
下载模板功能具体实现。
// 学生 模板下载
@RequestMapping("downloadStudentExecl.action")
public void downloadStudentExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {
String excelPath = request.getSession().getServletContext().getRealPath("/template/" + "student.xls");
String fileName = "student.xls".toString(); // 文件的默认保存名
// 读到流中
InputStream inStream;
try {
inStream = new FileInputStream(excelPath);// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode("student.xls", "UTF-8"));
// 循环取出流中的数据
byte[] b = new byte[200];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
上传文件功能实现。
// 学生 文件上传
@RequestMapping("uploadStudentExcel.action")
public ModelAndView uploadExcelStudent(@RequestParam(value = "myfile", required = false) MultipartFile myfile,
HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
String errorMsg;
if (myfile == null || myfile.isEmpty()) {
errorMsg = "您上传的文件为空";
session.setAttribute("errorMsg", errorMsg);
return new ModelAndView("admin/error.jsp");
}
String fileNameTemp = myfile.getOriginalFilename();
if (!fileNameTemp.endsWith("xls") && !fileNameTemp.endsWith("xlsx")) {
errorMsg = "您上传的文件不是Excel 文件";
session.setAttribute("errorMsg", errorMsg);
return new ModelAndView("admin/error.jsp");
}
String sid;
String sname;
String sgrade;
InputStream is = myfile.getInputStream();
Workbook wk = WorkbookFactory.create(is);
// Workbook wk = new XSSFWorkbook(is);
// 获取第一张
Sheet sheet = wk.getSheetAt(0);
// 遍历行Row
for (Row row : sheet) {
if (row.getRowNum() == 0) {
continue;
} else {
sid = row.getCell(0).getStringCellValue();
sname = row.getCell(1).getStringCellValue();
sgrade = row.getCell(2).getStringCellValue();
asi.insertStudent(sid, sname, sgrade);
}
}
return new ModelAndView("admin/success.jsp");
}
模板文件存放路径
在这里需要注意的是文件的上传功能。
form上一定加 enctype=“multipart/form-data” 。这个的意思是enctype就是encodetype就是编码类型的意思。
multipart/form-data是指表单数据有多部分构成,既有文本数据,又有文件等二进制数据的意思。
需要注意的是:默认情况下,enctype的值是application/x-www-form-urlencoded,不能用于文件上传,只有使用了multipart/form-data,才能完整的传递文件数据。
application/x-www-form-urlencoded不是不能上传文件,是只能上传文本格式的文件,multipart/form-data是将文件以二进制的形式上传,这样可以实现多种类型的文件上传。
后面接收需要这样定义@RequestParam(value = “myfile”, required = false) MultipartFile myfile。
还需要注意一点就是要是数字格式的话就用subjectid = (int) row.getCell(0).getNumericCellValue();
是String的话就用subjecttype = row.getCell(1).getStringCellValue();
poi的maven。
<!-- poi3.9:导出excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
希望对有需要的猿猿有所帮助。