java通過POI解析Excel表格內容
阿新 • • 發佈:2019-01-04
為了使用SpringMVC讀取引數使用MultipartFile接收引數,但MultipartFile與File不同,傳參時經常會出現錯誤,可以通過getInputStream講其轉換為IO流做進一步處理。
public ModelAndView readExcel(MultipartFile aaa){
ModelAndView mav=new ModelAndView();
BufferedInputStream bf=null;
try {
bf=new BufferedInputStream(aaa.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
XSSFWorkbook workbook=null;
try {
workbook=new XSSFWorkbook(bf);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("___workbook__" +workbook);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
XSSFRow row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
XSSFCell cell = null ;
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要設成這個,否則可能會出現亂碼
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
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 XSSFCell.CELL_TYPE_FORMULA:
// 匯入時如果為公式生成的資料則無值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case XSSFCell.CELL_TYPE_BLANK:
break;
case XSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y" : "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 && value.trim().equals("")) {
break;
}
//excel表格中欄位順序為:使用者名稱,密碼,電話和地址,為方便起見假設欄位一一對應
if (columnIndex == 0) {
String n = value;
System.out.print(n+" ");
} else if (columnIndex == 1) {
String v = value;
System.out.print(v+" ");
} else if (columnIndex == 2) {
String v = value;
System.out.print(v+" ");
} else if (columnIndex == 3) {
String v = value;
System.out.println(v+" ");
}
}
}
mav.setViewName("wm/wmTcDaily/edit");
return mav;
}
通過測試可以把excel檔案中的內容輸入至控制檯,後續操作可將取得的欄位封入實體類,進一步存入List,最終存入資料庫
匯出excel表文件:
@RequestMapping("/download")
public ModelAndView transferExcel(HttpServletResponse response){
ModelAndView mav=new ModelAndView();
//建立HSSFWorkbook物件(excel的文件物件)
HSSFWorkbook wkb = new HSSFWorkbook();
//建立新的sheet物件(excel的表單)
HSSFSheet sheet=wkb.createSheet("成績表");
//在sheet裡建立第一行,引數為行索引(excel的行),可以是0~65535之間的任何一個
HSSFRow row1=sheet.createRow(0);
//建立單元格(excel的單元格,引數為列索引,可以是0~255之間的任何一個
HSSFCell cell=row1.createCell(0);
//設定單元格內容
cell.setCellValue("學員考試成績一覽表");
//合併單元格CellRangeAddress構造引數依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//在sheet裡建立第二行
HSSFRow row2=sheet.createRow(1);
//建立單元格並設定單元格內容
row2.createCell(0).setCellValue("姓名");
row2.createCell(1).setCellValue("班級");
row2.createCell(2).setCellValue("筆試成績");
row2.createCell(3).setCellValue("機試成績");
//在sheet裡建立第三行
HSSFRow row3=sheet.createRow(2);
row3.createCell(0).setCellValue("李明");
row3.createCell(1).setCellValue("As178");
row3.createCell(2).setCellValue(87);
row3.createCell(3).setCellValue(78);
//輸出Excel檔案
OutputStream output;
try {
output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=details.xls");
response.setContentType("application/msexcel");
wkb.write(output);
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mav.setViewName("wm/wmTcDaily/edit");
return mav;
}