请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
前言在上篇中介绍到使用poi导入数据到数据库,本篇中介绍如何把数据导出生成Excel文件
步骤详解如下:
1.查询需要导出数据,封装成List集合
//获取内容数据
List<ChildQuestion2> childQuestion2List = paperMapper.exportChildQuestion(parentId);
//获取问卷信息名称
String fileName = paperMapper.selectPaperNameById(parentId);
2.随后创建Workbook工作簿对象:
创建Workbook有三种方式这里使用SXSSFWorkbook对象具体差异:此文详解
//创建工作簿
Workbook workbook = new SXSSFWorkbook();
3.创建Sheet表单进行操作:
//创建工作表
Sheet sheet = workbook.createSheet();
//设置工作簿名称
workbook.setSheetName(0, "sheet1");
4.数据迭代进行填充,设置单元格样式:
//创建表头数据
String[] headers = {"问题名称", "子问题名称", "条件", "主话术"};
//创建表头
Row head = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
Cell cell = head.createCell(i);
cell.setCellStyle(getCellStyle(workbook, false));
cell.setCellValue(headers[i]);
}
//设置列宽
sheet.setColumnWidth(0, 3500);
sheet.setColumnWidth(1, 5500);
sheet.setColumnWidth(2, 14000);
sheet.setColumnWidth(3, 14000);
//内容数据填充
int i = 0;
String falstName = "";//父问题名称
CellStyle oneStyle = getCellStyle(workbook, true);
CellStyle twoStyle = getCellStyle(workbook, false);
for (ChildQuestion2 child : childQuestion2List) {
i += 1;
//创建行
Row borderRow = sheet.createRow(i);
//父问题
if (!StringUtils.isEmpty(child.getQuestionName())) {
//第0列
Cell borderCell = borderRow.createCell(0);
borderCell.setCellStyle(oneStyle);
if (!child.getQuestionName().equals(falstName)) {
falstName = child.getQuestionName();
borderCell.setCellValue(falstName);
} else {
// 合并单元格:起始行号,终止行号, 起始列号,终止列号
CellRangeAddress cellRangeAddress = new CellRangeAddress(i - 1, i, 0, 0);
sheet.addMergedRegion(cellRangeAddress);
}
}
//子问题
if (!StringUtils.isEmpty(child.getChildName())) {
//第1列
Cell borderCell = borderRow.createCell(1);
borderCell.setCellStyle(twoStyle);
borderCell.setCellValue(child.getChildName());
}
//条件
if (!StringUtils.isEmpty(child.getCondition())) {
//第2列
Cell borderCell = borderRow.createCell(2);
borderCell.setCellStyle(twoStyle);
borderCell.setCellValue(child.getCondition());
}
//主话术
if (!StringUtils.isEmpty(child.getSubjectivity())) {
//第3列
Cell borderCell = borderRow.createCell(3);
borderCell.setCellStyle(twoStyle);
borderCell.setCellValue(child.getSubjectivity());
}
}
outExcelStream(response,workbook, fileName);//调用文件输出方法
/**
* 设置单元格样式
*
* @param workbook 工作对象
* @param boolCell 判断条件
* @return cellStyle 样式
*/
public CellStyle getCellStyle(final Workbook workbook, boolean boolCell) {
// 创建单元格样式对象
CellStyle cellStyle = workbook.createCellStyle();
if (boolCell) {
// 设置单元格内容水平对其方式 :居中对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
} else {
// 设置单元格内容水平对其方式 :左对齐
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
// 设置单元格内容是否自动换行
cellStyle.setWrapText(true);
}
// 设置单元格内容垂直对其方式:中对齐
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
// 创建字体对象
Font ztFont = workbook.createFont();
// 将字体大小设置为12px
ztFont.setFontHeightInPoints((short) 12);
cellStyle.setFont(ztFont);
return cellStyle;
}
5.传入Workbook、HttpServletResponse、文件名称参数,通过流的方式输出excle到页面:
/**
* 通过流的方式输出excle到页面
* @param response 响应
* @param workbook 工作空间
* @param fileName 文件名
*/
public void outExcelStream(HttpServletResponse response, Workbook workbook, String fileName){
OutputStream os = null;
try {
os = response.getOutputStream();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");
workbook.write(os);
os.flush();
}catch (Exception e) {
e.printStackTrace();
}finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//删除单个文件夹
public void deleteFileDir(String fileName) {
File file = new File(fileName);
DeleteAll(file);
}
public void DeleteAll(File dir) {
if (dir.isFile()) {
dir.delete();
return;
} else {
File[] files = dir.listFiles();
for (File file : files) {
DeleteAll(file);
}
}
dir.delete();
}
6.Excel导出效果如下:
参考链接:https://www.cnblogs.com/wlxslsb/p/10931130.html
个人总结:
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!