1.jsp部分:
<form action = "price/excelUpload" method = "post" enctype="multipart/form-data">
<input class="fileFrom" type="file" name="filename" ><br/>
<input class="formSubmit" type="submit" value="立即添加" name="">
</form>
2.Controller部分:
@RequestMapping("/excelUpload")
public String excelUpload(@RequestParam(value="filename") MultipartFile file,HttpServletRequest request,HttpServletResponse response){
if(file==null) return null;
//获取文件名
String name=file.getOriginalFilename();
//进一步判断文件是否为空(即判断其大小是否为0或其名称是否为null)
long size=file.getSize();
if(name==null || ("").equals(name) && size==0) return null;
//批量导入。参数:文件名,文件。
boolean b = ps.excelUpload(name, file);
if(b){
String Msg ="批量导入EXCEL成功!";
request.getSession().setAttribute("msg",Msg);
}else{
String Msg ="批量导入EXCEL失败!";
request.getSession().setAttribute("msg",Msg);
}
return "***";
}
3.serviceImpl部分
@Override
public boolean excelUpload(String name, MultipartFile file) {
boolean b = false;
//创建处理EXCEL
ReadExcelMaterial readExcel=new ReadExcelMaterial();
//解析excel,获取客户信息集合。
List<Price> priceList= readExcel.getExcelInfo(name, file);
if(materialList != null){
b = true;
}
//迭代添加客户信息(注:实际上这里也可以直接将customerList集合作为参数,在Mybatis的相应映射文件中使用foreach标签进行批量添加。)
for(Price price:priceList){
pm.addPrice(price);
}
return b;
}
4.util
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import