EXCEL导出简洁版
程序员文章站
2024-03-20 18:03:10
...
@RequestMapping(value="/exportExpertList")
public String exportExpertList(@RequestParam(name = "name") String Name,
@RequestParam(name = "researchDomain")String ResearchDomain,
HttpServletResponse response) throws IOException {
//处理前端参数为汉字时乱码问题
String name=new String(Name.trim().getBytes("ISO-8859-1"), "UTF-8");
String researchDomain=new String(ResearchDomain.getBytes("ISO-8859-1"), "UTF-8");
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook book = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = book.createSheet("专家导出EXCEL文档");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
sheet.setColumnWidth(2, 256*20); //设置某一列宽度
sheet.setColumnWidth(3, 256*20);
sheet.setColumnWidth(4, 256*20);
sheet.setColumnWidth(5, 256*20);
sheet.setColumnWidth(6, 256*20);
sheet.setColumnWidth(7, 256*20);
HSSFCellStyle cellStyle2 = book.createCellStyle();
HSSFDataFormat format = book.createDataFormat();
cellStyle2.setDataFormat(format.getFormat("@"));// 设置单元格格式为文本
HSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyle2);
cell.setCellValue(new HSSFRichTextString("姓名"));
cell = row.createCell(1);
cell.setCellStyle(cellStyle2);
cell.setCellValue(new HSSFRichTextString("学历"));
cell = row.createCell(2);
cell.setCellStyle(cellStyle2);
cell.setCellValue(new HSSFRichTextString("工作单位"));
cell = row.createCell(3);
cell.setCellStyle(cellStyle2);
cell.setCellValue(new HSSFRichTextString("研究领域"));
cell = row.createCell(4);
cell.setCellStyle(cellStyle2);
cell.setCellValue(new HSSFRichTextString("专家类别"));
Map<String,String> parameters=new HashMap<>();
parameters.put("name",name );
parameters.put("researchDomain",researchDomain );
List<ExpertInfo> expertInfoList = new ArrayList<ExpertInfo>();
expertInfoList = expertInfoService.queryExpertList(parameters);
for(int i=0;i<expertInfoList.size();i++){
ExpertInfo ExpertInfo = expertInfoList.get(i);
row = sheet.createRow((int) i + 1);
int a=0;
String name1=(String) ExpertInfo.getName();
cell = row.createCell(a);
cell.setCellValue(new HSSFRichTextString(name1));
String education=(String) ExpertInfo.getEducation();
cell = row.createCell(++a);
cell.setCellValue(new HSSFRichTextString(education));
String talentcategory=(String) ExpertInfo.getCompany();
cell = row.createCell(++a);
cell.setCellValue(new HSSFRichTextString(talentcategory));
String researchDomain1=(String) ExpertInfo.getResearchDomain();
cell = row.createCell(++a);
cell.setCellValue(new HSSFRichTextString(researchDomain1));
String expertCategory=(String) ExpertInfo.getExpertCategory();
cell = row.createCell(++a);
cell.setCellValue(new HSSFRichTextString(expertCategory));
}
OutputStream os = null;
// 取得输出流
try {
os = response.getOutputStream();
} catch (IOException e2) {
e2.printStackTrace();
}
response.reset();// 清空输出
// 第五步 见文件存储到指定位置
String fileName = "专家导出EXCEL文档.xls";
//9.设置信息头
response.setContentType(fileName);
try {
fileName = new String (fileName.getBytes ("utf-8"),"ISO8859-1");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
response.setHeader("Content-Disposition","attachment;filename="+fileName);
//10.写出文件,关闭流
try {
book.write(os);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
book.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
上一篇: 怎么实现前后端分离mybatisPlus的修改功能?
下一篇: VUE脚手架项目中导出EXCEl文件