1. 程式人生 > >Web使用Apache poi對EXCEL操作(匯入)

Web使用Apache poi對EXCEL操作(匯入)

第一次嘗試Web專案上傳Excel資料進行匯入

如果有哪裡做的不對不好或者有更好的方法請聯絡我,謝謝

pom.xml中新增

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->         <dependency>             <groupId>org.apache.poi</groupId>             <artifactId>poi</artifactId>                <version>3.17</version>         </dependency>         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->         <dependency>             <groupId>org.apache.poi</groupId>             <artifactId>poi-ooxml</artifactId>             <version>3.17</version>         </dependency>         <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->         <dependency>             <groupId>org.apache.poi</groupId>             <artifactId>poi-ooxml-schemas</artifactId>             <version>3.17</version>         </dependency>

只是做了一個學習測試,所以並沒有插入資料庫

Controller的程式碼

@RequestMapping("/upload")
	public String upload(@RequestParam("uploadFile") MultipartFile uploadFile) {
		if (MultipartFileWaf.checkSize(uploadFile)) {
			String excelType = MultipartFileWaf.checkIsExcel(uploadFile);
			try {
				InputStream fileInputStream = uploadFile.getInputStream();
				if ("xls".equals(excelType)) {
					HSSFWorkbook hwb = new HSSFWorkbook(fileInputStream);
					HSSFSheet hsheet = hwb.getSheetAt(0);
					medicalService.saveHSSFSheetMessage(hsheet);
					hwb.close();
				} else if ("xlsx".equals(excelType)) {
					XSSFWorkbook xwb = new XSSFWorkbook(fileInputStream);
					XSSFSheet xsheet = xwb.getSheetAt(0);
					medicalService.saveXSSFSheetMessage(xsheet);
					xwb.close();
				} else {
					System.out.println("上傳失敗");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			System.out.println("上傳失敗");
		}
		return "success";
	}

檢驗檔案大小的方法我就不發出來了,xls和xlsx的類不同,但是操作相同,一直在找這裡有沒有辦法優化。


	private DecimalFormat df = new DecimalFormat("0");   
	@Override
	public int saveHSSFSheetMessage(HSSFSheet sheet) {
		// TODO Auto-generated method stub
		for(int i = 1;i <= sheet.getLastRowNum();i++) {
			Row row = sheet.getRow(i);
			Cell sid = row.getCell(0);
			Cell sname = row.getCell(1);
			Cell stature = row.getCell(2);
			Cell bp = row.getCell(3);
			Cell vision = row.getCell(4);
			String cellText = "";
			if("NUMERIC".equals(sid.getCellTypeEnum().toString())) {
				cellText = df.format(sid.getNumericCellValue()); 
			}
			else {
				cellText = sid.toString();
			}
			System.out.println(cellText+" "+sname+" "+stature+" "+bp+" "+vision+" ");
		}
		return 0;
	}

	@Override
	public int saveXSSFSheetMessage(XSSFSheet sheet) {
		// TODO Auto-generated method stub
		for(int i = 1;i <= sheet.getLastRowNum();i++) {
			Row row = sheet.getRow(i);
			Cell sid = row.getCell(0);
			Cell sname = row.getCell(1);
			Cell stature = row.getCell(2);
			Cell bp = row.getCell(3);
			Cell vision = row.getCell(4);
			String cellText = "";
			if("NUMERIC".equals(sid.getCellTypeEnum().toString())) {
				cellText = df.format(sid.getNumericCellValue()); 
			}
			else {
				cellText = sid.toString();
			}
			System.out.println(cellText+" "+sname+" "+stature+" "+bp+" "+vision+" ");
		}
		return 0;
	}
	

這裡有個對excel資料型別的判定,POI3.15後就放棄getCellType進而使用getCellTypeEnum