java實現的Excel批量導入


   1.導入 Maven

 <dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>2.3.1</version>
</dependency>

2.Controller代碼

@RequestMapping(value = "importPsd")
public String importPsd(MultipartFile file,HttpServletRequest request, HttpServletResponse response, Model model ,RedirectAttributes redirectAttributes) throws Exception {
syPsyhPsdService.importPsd(file);
return "導入成功跳轉的頁面";
}

3.Service代碼

   /**
     * 批量導入(批量添加)
     */

   private static final String XLS = "xls";
   private static final String XLSK = "xlsx";  //定義全局的常量值

  @Transactional(readOnly = false)
  public void importPsd(MultipartFile file) throws Exception {
    List<SyPsyhPsd> list = new ArrayList<SyPsyhPsd>();
    Map<String, Object> rsultMap = new HashMap<String, Object>();
    AutoCode auto =new AutoCode();
    Workbook workbook = null;
    String fileName = file.getOriginalFilename();
    if(fileName.endsWith("xls")) {
    //2003
    try {
    workbook = new HSSFWorkbook(file.getInputStream());
    } catch (Exception e) {
    e.printStackTrace( );
    }
    }else if(fileName.endsWith("xlsx")) {
    try {
    //2007
    workbook = new XSSFWorkbook(file.getInputStream());
    } catch (Exception e) {
    e.printStackTrace( );
    }
    }else {
    try {
    throw new Exception("文件不是Excel文件");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    Sheet sheet = workbook.getSheet("export");
    int rows = sheet.getLastRowNum();//指定行數。一共多少+
    if(rows==0) {
    try {
    throw new Exception("請填寫行數");
    } catch (Exception e) {
    e.printStackTrace();
      }
    }
    for (int i = 2; i < rows+1; i++) {
    //讀取左上端單元格
    Row row = sheet.getRow(i);
    //行不為空
    if(row != null) {
    //創建實體類
    SyPsyhPsd syPsyhPsd = new SyPsyhPsd();
    //讀取第一個內容
    String rq = getCellValue(row.getCell(0));
    syPsyhPsd.setRq(rq);
    //讀取第二個內容
    /*String khpk = getCellValue(row.getCell(1));
    syPsyhPsd.setKhpk(khpk);;*/
    //讀取第三個內容
    String zzsj = getCellValue(row.getCell(1));
    syPsyhPsd.setZzsj(zzsj);
    //讀取第四個內容
    String zwsj = getCellValue(row.getCell(2));
    syPsyhPsd.setZwsj(zwsj);
    //讀取第五個內容
    String hwsx = getCellValue(row.getCell(3));
    syPsyhPsd.setHwsx(hwsx);

    //判斷實體是否為空
    if (syPsyhPsd!=null) {

    //如果不為空,調用增加方法
    super.save(syPsyhPsd);
       }
       }
   }
  }

 

    

 

/**
*獲取Cell內容

*/

  private String getCellValue(Cell cell) {
    String value = "";
    if(cell != null) {
    //以下是判斷數據的類型
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC://數字
    value = cell.getNumericCellValue() + "";
    if(HSSFDateUtil.isCellDateFormatted(cell)) {
    Date date = cell.getDateCellValue();
    if(date != null) {
    value = new SimpleDateFormat("yyyy-MM-dd").format(date);
    }else {
    value = "";
    }
    }else {
    value = new DecimalFormat("0").format(cell.getNumericCellValue());
    }
    break;
    case HSSFCell.CELL_TYPE_STRING: //字符串
    value = cell.getStringCellValue();
    break;
    case HSSFCell.CELL_TYPE_BOOLEAN: //boolean
    value = cell.getBooleanCellValue() + "";
    break;
    case HSSFCell.CELL_TYPE_FORMULA: //公式
    value = cell.getCellFormula() + "";
    break;
    case HSSFCell.CELL_TYPE_BLANK: //空值
    value = "";
    break;
    case HSSFCell.CELL_TYPE_ERROR: //故障
    value = "非法字符";
    break;
    default:
    value = "未知類型";
    break;
      }
  }
    return value.trim();
  }

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM