简单的Excel导入(上传、解析、持久化)
/** * excel导入 * @param req * @param resp * @return */ public void excelImport(){ //先将要上传的Excel文件上传到项目下的excel_Temp临时文件夹 String path= request.getSession().getServletContext().getRealPath("/excel_Temp/"); String fileName = UUID.randomUUID().toString().replace("-", "")+excelFileName; FileOutputStream fos = null; FileInputStream fis = null; try { fis = new FileInputStream(excel); fos = new FileOutputStream(new File(path,fileName)); int len=0; byte[] buffer = new byte[1024]; while((len = fis.read(buffer))!= -1){ fos.write(buffer, 0, len); } fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } //读取excel文件并获取其值 try { List<List<Object>> list= Read_Excel.readExcel(new File(path,fileName)); //将获取到的值赋给要导入的对象 if(!(list.size()<1) ){ for (int i = 1; i < list.size(); i++) { if(list.get(i).size() != 0){ String pk1 = pk.generateStringPk(); arcGongcInnerfile.setId(pk1); arcGongcInnerfile.setCreDeptId(userDeptID); arcGongcInnerfile.setCreTime(JDateToolkit.getNowDate4()); arcGongcInnerfile.setCreUserId(userID); arcGongcInnerfile.setOrgId(orgId); arcGongcInnerfile.setStatus("1"); arcGongcInnerfile.setInnerfileSeq((String) list.get(i).get(0)); arcGongcInnerfile.setDutyMan((String) list.get(i).get(1)); arcGongcInnerfile.setDocTitle((String) list.get(i).get(2)); arcGongcInnerfile.setDocCode((String) list.get(i).get(3)); arcGongcInnerfile.setSecurity(arcGongcInnerfileService.getIdByName((String) list.get(i).get(4)==" "?"无":(String) list.get(i).get(4))); arcGongcInnerfile.setDocDate((String) list.get(i).get(5)); arcGongcInnerfile.setPageNum((String) list.get(i).get(6)); arcGongcInnerfile.setArchiveLink((String) list.get(i).get(7)); arcGongcInnerfile.setMemo((String) list.get(i).get(8)); try { //将对象设值后持久化到数据库 arcGongcInnerfileService.save(arcGongcInnerfile); } catch (Exception e) { e.printStackTrace(); } } } } response.setCharacterEncoding("utf-8"); response.getWriter().print("导入成功!"); }catch(Exception e){ e.printStackTrace(); } //将上传到excel_Temp文件夹里的文件清空 File file = new File(path); File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { File delfile = fileList[i]; delfile.delete(); } }
package com.sinosoft.module.arc.common; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCell; public class Read_Excel { /** * 读取 office excel * @throws IOException * @throws FileNotFoundException */ public static List<List<Object>> readExcel(File file) throws IOException{ List<List<Object>> list = new LinkedList<List<Object>>(); HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet sheet = hwb.getSheetAt(0); Object value = null; HSSFRow row = null; HSSFCell cell = null; for(int i = sheet.getFirstRowNum();i<= sheet.getPhysicalNumberOfRows();i++){ row = sheet.getRow(i); if (row == null) { continue; } List<Object> linked = new LinkedList<Object>(); int count = 0; for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { continue; } DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化日期字符串 DecimalFormat nf = new DecimalFormat("0");// 格式化数字 switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if("@".equals(cell.getCellStyle().getDataFormatString())){ value = df.format(cell.getNumericCellValue()); } else if("General".equals(cell.getCellStyle().getDataFormatString())){ value = nf.format(cell.getNumericCellValue()); }else{ value = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: value = " "; count ++; break; default: value = cell.toString(); } if (value == null || "".equals(value)) { value = ""; continue; } linked.add(value); } if(count<9){ list.add(linked); } } return list; } }