package com.hfpmp.common;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class ImportExcelUtil {
/**
* 描述:获取IO流中的数据,组装成List<List<Object>>对象
*
* @param in,fileName
* @return
* @throws IOException
*/
public List<List<Object>> getListByExcel(InputStream in, String fileName) throws Exception {
List<List<Object>> list = null;
//创建Excel工作薄
Workbook work = WorkbookFactory.create(in);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
list = new ArrayList<List<Object>>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}
//遍历当前sheet中的所有行
for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum()+1; j++) {
row = sheet.getRow(j);
if (row == null || row.getFirstCellNum() == j) {
continue;
}
//遍历所有的列
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
li.add(this.getCellValue(cell));
}
list.add(li);
}
}
work.close();
return list;
}
/**
* 描述:对表格中数值进行格式化
*
* @param cell
* @return
*/
public Object getCellValue(Cell cell) {
Object value = null;
/**格式化number String字符 */
DecimalFormat df = new DecimalFormat("0");
/**日期格式化 */
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");
/**格式化数字 */
DecimalFormat df2 = new DecimalFormat("0.00");
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if ("General".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
value = sdf.format(cell.getDateCellValue());
} else {
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
}
}
使用
/**
* 描述:通过 jquery.form.js 插件提供的ajax方式上传文件
*
* @param request
* @param response
* @throws Exception
*/
@ResponseBody
@RequestMapping(value = "ajaxUpload", produces = {"application/text;charset=UTF-8"})
public void ajaxUploadExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
String excelId = request.getParameter("excelId");//导入记录id
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
InputStream in = null;
List<List<Object>> listob = null;
MultipartFile file = multipartRequest.getFile("upfile");
if (file.isEmpty()) {
throw new Exception("文件不存在!");
}
in = file.getInputStream();
listob = new ImportExcelUtil().getListByExcel(in, file.getOriginalFilename());
//该处可调用service相应方法进行数据保存到数据库中,现只对数据输出
for (int i = 0; i < listob.size(); i++) {
List<Object> lo = listob.get(i);
SaleDruglist bean = new SaleDruglist();
bean.setsId(UUID.randomUUID().toString().replace("-", ""));
bean.setsExcelId(excelId);
bean.setsState("2");
bean.setsDrugCode(String.valueOf(lo.get(0)));
bean.setsDrugName(String.valueOf(lo.get(1)));
bean.setiNumber(Integer.valueOf(String.valueOf(lo.get(2))));
bean.setiBuy(Integer.valueOf(String.valueOf(lo.get(3))));
newSaleService.saveDrugList(bean);
}
PrintWriter out = null;
response.setCharacterEncoding("utf-8"); //防止ajax接受到的中文信息乱码
out = response.getWriter();
out.print("文件导入成功!");
out.flush();
out.close();
}
参考:
http://blog.csdn.net/onepersontz/article/details/49891405