前面咱們在JFianl項目中把Excel導出去了,因此咱們要想辦法把它導回來。前端
jar
包poi-ooxml-3.8-20120326.jar
和 xmlbeans-2.3.0.jar
java
Excel
咱們導入前端插件仍是咱們以前使用過的uploadify
。git
當咱們接收了Excel
文件。咱們首先要判斷一下是否爲Excel
文件以及Excel
的版本。github
添加Excel
文件檢測類,CEVUtil
。sql
package util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.POIXMLDocument; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class CEVUtil { /** * 依據後綴名判斷讀取的是否爲Excel文件 * @param filePath * @return */ public static boolean isExcel(String filePath){ if(filePath.matches("^.+\\.(?i)(xls)$")||filePath.matches("^.+\\.(?i)(xlsx)$")){ return true; } return false; } /** * 檢查文件是否存在 */ public static boolean fileExist(String filePath){ if(filePath == null || filePath.trim().equals("")) return false; File file = new File(filePath); if (file == null || !file.exists()){ return false; } return true; } /** * 依據內容判斷是否爲excel2003及如下 */ public static boolean isExcel2003(String filePath){ try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); if(POIFSFileSystem.hasPOIFSHeader(bis)) { System.out.println("Excel版本爲excel2003及如下"); return true; } } catch (IOException e) { e.printStackTrace(); return false; } return false; } /** * 依據內容判斷是否爲excel2007及以上 */ public static boolean isExcel2007(String filePath){ try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath)); if(POIXMLDocument.hasOOXMLHeader(bis)) { System.out.println("Excel版本爲excel2007及以上"); return true; } } catch (IOException e) { e.printStackTrace(); return false; } return false; } }
添加Excel
讀取類:數據庫
package util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.xssf.usermodel.XSSFWorkbook; /** * 有判斷性的讀取 * @author Administrator * */ public class ReadExcel { /** 錯誤信息 */ private String errorInfo; /** * 驗證EXCEL文件是否合法 */ public boolean validateExcel(String filePath){ /**判斷文件名是否爲空或者文件是否存在 */ if(!CEVUtil.fileExist(filePath)){ errorInfo = "文件不存在"; return false; } /**檢查文件是不是Excel格式的文件 */ if (!CEVUtil.isExcel(filePath)) { errorInfo = "文件名不是excel格式"; return false; } return true; } /** * @描述:根據文件名讀取excel文件 */ public Map<String,Object> read(String filePath){ Map<String,Object> map = new HashMap<String,Object>(); InputStream is = null; try{ /** 驗證文件是否合法 */ if (!validateExcel(filePath)){ map.put("error", errorInfo); return map; } /** 判斷文件的類型,是2003仍是2007 */ boolean isExcel2003 = true; if (CEVUtil.isExcel2007(filePath)){ isExcel2003 = false; } /** 調用本類提供的根據流讀取的方法 */ is = new FileInputStream(new File(filePath)); Workbook wb = null; if (isExcel2003){ wb = new HSSFWorkbook(is); }else{ wb = new XSSFWorkbook(is); } map.put("wb", wb); is.close(); }catch (IOException e){ e.printStackTrace(); }catch (Exception ex){ ex.printStackTrace(); }finally{ if (is != null){ try{ is.close(); }catch (IOException e){ is = null; e.printStackTrace(); } } } return map; } /** * @描述:讀取數據 */ public List<List<Object>> readExcel(Workbook workbook) throws FileNotFoundException, IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException{ //Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(excelPath))); //String values=""; Sheet sheet = workbook.getSheetAt(0); int startRowNum = sheet.getFirstRowNum(); int endRowNum = sheet.getLastRowNum(); List<List<Object>> dataLst = new ArrayList<List<Object>>(); for(int rowNum = startRowNum;rowNum<=endRowNum;rowNum++){ List<Object> rowLst = new ArrayList<Object>(); //values += "("; Row row = sheet.getRow(rowNum); if(row == null) continue; int startCellNum = row.getFirstCellNum(); int endCellNum = row.getLastCellNum(); for(int cellNum = startCellNum;cellNum<endCellNum;cellNum++){ Cell cell = row.getCell(cellNum); if(cell == null) continue; int type = cell.getCellType(); switch (type) { case Cell.CELL_TYPE_NUMERIC://數值、日期類型 double d = cell.getNumericCellValue(); if (HSSFDateUtil.isCellDateFormatted(cell)) {//日期類型 // Date date = cell.getDateCellValue(); Date date = HSSFDateUtil.getJavaDate(d); rowLst.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)); //values +="'"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)+"',"; }else{//數值類型 System.out.print(" "+d+" "); rowLst.add(d); } break; case Cell.CELL_TYPE_BLANK://空白單元格 System.out.print(" null "); rowLst.add(""); break; case Cell.CELL_TYPE_STRING://字符類型 System.out.print(" "+cell.getStringCellValue()+" "); rowLst.add(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN://布爾類型 System.out.println(cell.getBooleanCellValue()); rowLst.add(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: // 故障 System.err.println("非法字符");//非法字符; rowLst.add("非法字符"); break; default: System.err.println("error");//未知類型 rowLst.add("error"); break; } } dataLst.add(rowLst); System.out.println(); } return dataLst; } }
對應的Controller
類,ExportExcelController
apache
爲了防止用戶上傳的Excel
格式不爭取,額外添加了一個導出Excel
模板方法。dom
package controller; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import common.util.XLSFileKit; import service.ExportExcelService; import com.jfinal.core.Controller; import com.jfinal.plugin.activerecord.Record; public class ExportExcelController extends Controller{ ExportExcelService exportExcelService = new ExportExcelService(); /** * 導出表格 */ public void exportOutExcel(){ String sheetName = getPara("sheetName"); String fileName = new Date().getTime() + "_" + UUID.randomUUID().toString() + ".xls"; String filePath = getRequest().getRealPath("/") + "/file/export/"; File file = new File(filePath); if(!file.exists()){ file.mkdirs(); } String relativePath = "/file/export/" + fileName; filePath += fileName; XLSFileKit xlsFileKit = new XLSFileKit(filePath); List<List<Object>> content = new ArrayList<List<Object>>(); List<String> title = new ArrayList<String>(); List<Record> datas = exportExcelService.getList(); title.add("序號"); title.add("id"); title.add("caseId"); title.add("imgId"); int i = 0; OK: while(true){ if(datas.size() < (i + 1)){ break OK; } int index = i + 1; List<Object> row = new ArrayList<Object>(); row.add(index + ""); row.add(null==datas.get(i).get("id")?"":datas.get(i).get("id")); row.add(null==datas.get(i).get("caseId")?"":datas.get(i).get("caseId")); row.add(null==datas.get(i).get("imgId")?"":datas.get(i).get("imgId")); content.add(row); i ++; } xlsFileKit.addSheet(content, sheetName, title); xlsFileKit.save(); renderJson(new Record().set("relativePath", relativePath)); } /** * 導出表格模板 */ public void exportOutExcelStandard(){ String sheetName = getPara("sheetName"); String fileName = new Date().getTime() + "_" + UUID.randomUUID().toString() + ".xls"; String filePath = getRequest().getRealPath("/") + "/file/export/"; File file = new File(filePath); if(!file.exists()){ file.mkdirs(); } String relativePath = "/file/export/" + fileName; filePath += fileName; XLSFileKit xlsFileKit = new XLSFileKit(filePath); List<List<Object>> content = new ArrayList<List<Object>>(); List<String> title = new ArrayList<String>(); title.add("序號"); title.add("id"); title.add("caseId"); title.add("imgId"); xlsFileKit.addSheet(content, sheetName, title); xlsFileKit.save(); renderJson(new Record().set("relativePath", relativePath)); } }
對應的Services
類,InportExcelService
xss
package service; import com.jfinal.plugin.activerecord.Db; public class InportExcelService { public static void delAll(){ Db.update("delete from zt_case_img"); } public static void addSql(String sqlValues){ String sql = "insert into zt_case_img" + " (caseId,imgId)" + " values " + sqlValues; Db.update(sql); } }
而後前端相應的方法調用。post
//導出`Excel`模板 $("#exportS").click(function() { $.post("excel/exportOutExcelStandard", { sheetName: "報表" }, function(data) { var relativePath = data.relativePath; console.log(relativePath); window.location.href = "." + relativePath; }); }); $('#inport').uploadify({ 'swf': 'js/uploadify/uploadify.swf', 'buttonText': '導入Excel', 'auto': false, 'buttonClass': 'crud-btn import-btn', 'width': '78', 'height': '26', 'fileTypeExts': '*.xls;*.xlsx', 'uploader': 'inexcel/exportInExcel', 'removeCompleted': true, 'onSelect': function(file) { layer.confirm('請選擇清空數據庫插入,仍是追加插入?', { btn: ['清空插入', '追加插入'] //按鈕 }, function() { $("#inport").uploadify("settings", "uploader", "../../inexcel/exportInExcel?flag=1"); $('#inport').uploadify('upload', '*'); }, function() { $("#inport").uploadify("settings", "uploader", "../../inexcel/exportInExcel?flag=0"); $('#inport').uploadify('upload', '*'); }); }, 'onUploadStart': function(file) { }, 'onUploadSuccess': function(file, data, respone) { var img = $.parseJSON(data); if(img.flag != "1") { alert(img.flag); } else { alert("導入成功!", function() { window.location .reload(); }); } $('#pic_upload').uploadify( 'stop'); } }); });
導入Excel
:
導入Excel
成功:
導出Excel
模板: