更新時(shí)間:2022-03-17 10:29:47 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3456次
JavaWeb導(dǎo)出excel文件的方法其實(shí)很簡(jiǎn)單,只需要兩步就可以啦!
1.在需要的controller里面加入下面接口代碼:
/**
* excel導(dǎo)出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里面加入下面方法:
/**
* 導(dǎo)出excel信息
*
* @param map
* @return
*/
@Override
public HSSFWorkbook exportExcel(Map<String,Object> map) {
//獲取需要生成excel數(shù)據(jù)
List<UserDto> userDtoList=exportMapper.getUserInfo(map);
//如果數(shù)據(jù)為空,則不繼續(xù)往下走,直接return
if(userDtoList==null){
return null;
}
HSSFWorkbook wb = new HSSFWorkbook();
// 創(chuàng)建一個(gè)Excel的Sheet
HSSFSheet sheet = wb.createSheet("first sheet");
//表頭屬性名
String[] propertyName={"序號(hào)","姓名","性別","年齡","地址"};
// 獲取表需要的列數(shù)
int length = propertyName.length;
// ---------------下面設(shè)置表的第一行也就是通常的title----------------------
// 設(shè)置行-下面為第一行
HSSFRow row0 = sheet.createRow(0);
// 設(shè)置列-下面為第一行的第一列
HSSFCell cell00 = row0.createCell(0);
// 設(shè)置字體
HSSFFont headfont = wb.createFont();
headfont.setFontName("黑體");
// 字體大小
headfont.setFontHeightInPoints((short) 22);
// 設(shè)置樣式
HSSFCellStyle headstyle = wb.createCellStyle();
// 使用了上面設(shè)置的字體樣式
headstyle.setFont(headfont);
headstyle.setLocked(true);
// 自動(dòng)換行
headstyle.setWrapText(false);
// 合并單元格:參數(shù)說(shuō)明:1:開(kāi)始行 2:結(jié)束行 3:開(kāi)始列 4:結(jié)束列
// 注意,在這里使用表字段名-1來(lái)作結(jié)束列,因?yàn)槲覀兪菑?開(kāi)始數(shù)的,所以要減去一個(gè)
CellRangeAddress range = new CellRangeAddress(0, 0, 0, length - 1);
// 將表的合并單元格樣式設(shè)置進(jìn)去
sheet.addMergedRegion(range);
// 設(shè)置表的第一行的第一列的樣式
cell00.setCellStyle(headstyle);
// 設(shè)置表的第一行的第一列的value
cell00.setCellValue("excel標(biāo)題");
// ---------------下面開(kāi)始設(shè)置表的第二行,通常為字段名----------------------
HSSFRow row1 = sheet.createRow(1);
// 字段名使用的字體
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋體");
// 字體大小
columnHeadFont.setFontHeightInPoints((short) 10);
// 列頭的樣式
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
// 設(shè)置上面已經(jīng)設(shè)置好的字體樣式
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(false);
// 設(shè)置第二行的行高
row1.setHeight((short) 750);
// 創(chuàng)建在這行中的列
HSSFCell cell1 = null;
for (int i = 0; i < length; i++) {
cell1 = row1.createCell(i);
// 獲取數(shù)組中的表頭字段名
cell1.setCellValue(propertyName[i]);
// 給它設(shè)置風(fēng)格
cell1.setCellStyle(columnHeadStyle);
}
// ---------------下面開(kāi)始設(shè)置表里面的內(nèi)容-----------------------------
// 設(shè)置字體
HSSFFont font = wb.createFont();
font.setFontName("宋體");
font.setFontHeightInPoints((short) 10);
// 設(shè)置其風(fēng)格
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;
}
搞定了,現(xiàn)在可以生成excel了。
以上就是關(guān)于“JavaWeb導(dǎo)出excel文件的方法”介紹,大家如果想了解更多相關(guān)知識(shí),可以關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java視頻,里面的視頻教程細(xì)致全面,從入門到精通,適合沒(méi)有基礎(chǔ)的小白學(xué)習(xí),希望對(duì)大家能夠有所幫助。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743