使用poi生成excel通常包含一下幾個步驟
- 建立一個工作簿
- 建立一個sheet
- 建立一個Row物件
- 建立一個cell物件(1個row+1個cell構成一個單元格)
- 設定單元格內容
- 設定單元格樣式. 字型 字型大小 是否加粗
- 儲存
- 關閉流物件
生成一個工作簿
2010以上格式使用XSSFWorkBook物件, 2003格式使用HSSFWorkBook物件, 其他物件操作基本一樣.
生成2003格式
public void test1() {
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
HSSFSheet sheet = workbook.createSheet("Sheet1");
//設定單元格寬度
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 30 * 256);
sheet.setColumnWidth(2, 30 * 256);
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序號");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成績");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\測試生成2003.xls");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
生成2010以上格式
@Test
public void test2() {
XSSFWorkbook workbook = new XSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
XSSFSheet sheet = workbook.createSheet("Sheet1");
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序號");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成績");
OutputStream os = null;
try {
os = new FileOutputStream("d:\\測試生成2010.xlsx");
workbook.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
合併單元格
合併單元格在生成excel中算常見的一個場景, 通常先合併單元, 單元格內容居中,並設定單元格邊框.
poi合併單元格使用CellRangeAddress類, 建構函式包括4個引數firstRow, lastRow, firstCol, lastCol根據自己需要傳入行和列.
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
}
合併單元格後設定邊框poi已提供了RegionUtil靜態類, 可直接使用.
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
設定單元格樣式
左右居中 上下居中 自動換行
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setWrapText(true);
使用SpringMVC/SpringBoot匯出excel
@Controller
@GetMapping("/excel2003")
public void excel2003(HttpServletResponse httpServletResponse){
try {
//2010格式設定
//response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//2003格式設定
response.setContentType("application/vnd.ms-excel");
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學生成績單.xls", "utf-8"));
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
Font font = workbook.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
HSSFSheet sheet = workbook.createSheet("Sheet1");
Row row0 = sheet.createRow(0);
Cell cell0 = row0.createCell(0);
cell0.setCellValue("序號");
cell0.setCellStyle(cellStyle);
Cell cell1 = row0.createCell(1);
cell1.setCellValue("姓名");
Cell cell2 = row0.createCell(2);
cell2.setCellValue("成績");
workbook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
總結
如果你在web專案中匯出excel後,開啟excel檔案時提示檔案已損壞,但是檔案還可以開啟, 則需要在HttpServletResponse上設定響應頭, 2003和2010設定方式不同
2003
response.setContentType("application/vnd.ms-excel");
2010
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
下載檔名如果包含中文的話需要編碼
httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學生成績單.xls", "utf-8"));
這種設定在瀏覽器裡下載檔案中文是沒問題的, 只是如果你使用Swagger或者Postman測試的話,檔名還是經過編碼的, 這個沒問題說明檔案下載已經沒問題.