springMVC從上傳的Excel檔案中讀取資料
阿新 • • 發佈:2019-01-31
package com.jun.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; 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 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; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import com.jun.service.CustomerService; import com.jun.vo.Customer; public class ReadExcel { //總行數 private int totalRows = 0; //總條數 private int totalCells = 0; //錯誤資訊接收器 private String errorMsg; //構造方法 public ReadExcel(){} //獲取總行數 public int getTotalRows() { return totalRows;} //獲取總列數 public int getTotalCells() { return totalCells;} //獲取錯誤資訊 public String getErrorInfo() { return errorMsg; } /** * 驗證EXCEL檔案 * @param filePath * @return */ public boolean validateExcel(String filePath){ if (filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath))){ errorMsg = "檔名不是excel格式"; return false; } return true; } /** * 讀EXCEL檔案,獲取客戶資訊集合 * @param fielName * @return */ public List<Customer> getExcelInfo(String fileName,MultipartFile Mfile){ //把spring檔案上傳的MultipartFile轉換成CommonsMultipartFile型別 CommonsMultipartFile cf= (CommonsMultipartFile)Mfile; //獲取本地儲存路徑 File file = new File("D:\\fileupload"); //建立一個目錄 (它的路徑名由當前 File 物件指定,包括任一必須的父路徑。) if (!file.exists()) file.mkdirs(); //新建一個檔案 File file1 = new File("D:\\fileupload" + new Date().getTime() + ".xlsx"); //將上傳的檔案寫入新建的檔案中 try { cf.getFileItem().write(file1); } catch (Exception e) { e.printStackTrace(); } //初始化客戶資訊的集合 List<Customer> customerList=new ArrayList<Customer>(); //初始化輸入流 InputStream is = null; try{ //驗證檔名是否合格 if(!validateExcel(fileName)){ return null; } //根據檔名判斷檔案是2003版本還是2007版本 boolean isExcel2003 = true; if(WDWUtil.isExcel2007(fileName)){ isExcel2003 = false; } //根據新建的檔案例項化輸入流 is = new FileInputStream(file1); //根據excel裡面的內容讀取客戶資訊 customerList = getExcelInfo(is, isExcel2003); is.close(); }catch(Exception e){ e.printStackTrace(); } finally{ if(is !=null) { try{ is.close(); }catch(IOException e){ is = null; e.printStackTrace(); } } } return customerList; } /** * 根據excel裡面的內容讀取客戶資訊 * @param is 輸入流 * @param isExcel2003 excel是2003還是2007版本 * @return * @throws IOException */ public List<Customer> getExcelInfo(InputStream is,boolean isExcel2003){ List<Customer> customerList=null; try{ /** 根據版本選擇建立Workbook的方式 */ Workbook wb = null; //當excel是2003時 if(isExcel2003){ wb = new HSSFWorkbook(is); } else{//當excel是2007時 wb = new XSSFWorkbook(is); } //讀取Excel裡面客戶的資訊 customerList=readExcelValue(wb); } catch (IOException e) { e.printStackTrace(); } return customerList; } /** * 讀取Excel裡面客戶的資訊 * @param wb * @return */ private List<Customer> readExcelValue(Workbook wb){ //得到第一個shell Sheet sheet=wb.getSheetAt(0); //得到Excel的行數 this.totalRows=sheet.getPhysicalNumberOfRows(); //得到Excel的列數(前提是有行數) if(totalRows>=1 && sheet.getRow(0) != null){ this.totalCells=sheet.getRow(0).getPhysicalNumberOfCells(); } List<Customer> customerList=new ArrayList<Customer>(); Customer customer; //迴圈Excel行數,從第二行開始。標題不入庫 for(int r=1;r<totalRows;r++){ Row row = sheet.getRow(r); if (row == null) continue; customer = new Customer(); //迴圈Excel的列 for(int c = 0; c <this.totalCells; c++){ Cell cell = row.getCell(c); if (null != cell){ if(c==0){//第一列不讀 }else if(c==1){ customer.setcName(cell.getStringCellValue());//客戶名稱 }else if(c==2){ customer.setSimpleName(cell.getStringCellValue());//客戶簡稱 }else if(c==3){ customer.setTrade(cell.getStringCellValue());//行業 }else if(c==4){ customer.setSource(cell.getStringCellValue());//客戶來源 }else if(c==5){ customer.setAddress(cell.getStringCellValue());//地址 }else if(c==6){ customer.setRemark(cell.getStringCellValue());//備註資訊 } } } //新增客戶 customerList.add(customer); } return customerList; } }