更新時間:2022-03-17 10:29:47 來源:動力節點 瀏覽3549次
JavaWeb導出excel文件的方法其實很簡單,只需要兩步就可以啦!
1.在需要的controller里面加入下面接口代碼:
/**
* excel導出controller層代碼
*
* @param params
* @return
*/
@RequestMapping("exportExcel")
@ResponseBody
public String exportExcel(HttpServletResponse response, Map<String, Object> params) {
HSSFWorkbook workbook = excelService.exportExcel(map);
try {
if (response != null) {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=\"" + new String(("excel名稱" + ".xls").getBytes("gb2312"), "ISO8859-1"));
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
pom.xml文件加上poi的依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
2.在serviceImpl里面加入下面方法:
/**
* 導出excel信息
*
* @param map
* @return
*/
@Override
public HSSFWorkbook exportExcel(Map<String,Object> map) {
//獲取需要生成excel數據
List<UserDto> userDtoList=exportMapper.getUserInfo(map);
//如果數據為空,則不繼續往下走,直接return
if(userDtoList==null){
return null;
}
HSSFWorkbook wb = new HSSFWorkbook();
// 創建一個Excel的Sheet
HSSFSheet sheet = wb.createSheet("first sheet");
//表頭屬性名
String[] propertyName={"序號","姓名","性別","年齡","地址"};
// 獲取表需要的列數
int length = propertyName.length;
// ---------------下面設置表的第一行也就是通常的title----------------------
// 設置行-下面為第一行
HSSFRow row0 = sheet.createRow(0);
// 設置列-下面為第一行的第一列
HSSFCell cell00 = row0.createCell(0);
// 設置字體
HSSFFont headfont = wb.createFont();
headfont.setFontName("黑體");
// 字體大小
headfont.setFontHeightInPoints((short) 22);
// 設置樣式
HSSFCellStyle headstyle = wb.createCellStyle();
// 使用了上面設置的字體樣式
headstyle.setFont(headfont);
headstyle.setLocked(true);
// 自動換行
headstyle.setWrapText(false);
// 合并單元格:參數說明:1:開始行 2:結束行 3:開始列 4:結束列
// 注意,在這里使用表字段名-1來作結束列,因為我們是從0開始數的,所以要減去一個
CellRangeAddress range = new CellRangeAddress(0, 0, 0, length - 1);
// 將表的合并單元格樣式設置進去
sheet.addMergedRegion(range);
// 設置表的第一行的第一列的樣式
cell00.setCellStyle(headstyle);
// 設置表的第一行的第一列的value
cell00.setCellValue("excel標題");
// ---------------下面開始設置表的第二行,通常為字段名----------------------
HSSFRow row1 = sheet.createRow(1);
// 字段名使用的字體
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋體");
// 字體大小
columnHeadFont.setFontHeightInPoints((short) 10);
// 列頭的樣式
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
// 設置上面已經設置好的字體樣式
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(false);
// 設置第二行的行高
row1.setHeight((short) 750);
// 創建在這行中的列
HSSFCell cell1 = null;
for (int i = 0; i < length; i++) {
cell1 = row1.createCell(i);
// 獲取數組中的表頭字段名
cell1.setCellValue(propertyName[i]);
// 給它設置風格
cell1.setCellStyle(columnHeadStyle);
}
// ---------------下面開始設置表里面的內容-----------------------------
// 設置字體
HSSFFont font = wb.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 10);
// 設置其風格
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
style.setWrapText(false);
HSSFRow row = null;
HSSFCell cell = null;
int valueStartRow = 2;
// 賦值
for (UserDto userDto : userDtoList) {
row = sheet.createRow(valueStartRow);
cell = row.createCell(0);
cell.setCellValue(userDto.getIndex());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(userDto.getName());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(userDto.getGender());
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(userDto.getAge());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(userDto.getAddress());
cell.setCellStyle(style);
valueStartRow++;
}
return wb;
}
搞定了,現在可以生成excel了。
以上就是關于“JavaWeb導出excel文件的方法”介紹,大家如果想了解更多相關知識,可以關注一下動力節點的Java視頻,里面的視頻教程細致全面,從入門到精通,適合沒有基礎的小白學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習