Java + poi导出Excel表格
废话少说,直接上代码
1.引入pom.xml 代码片
.
<!-- POI导入导出 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
ExportExcelUtil 工具类 代码片
.
package net.ourway.common.utils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* 导出Excel公共方法
* @version 1.0
*
* @author wqb
*
*/
public class ExportExcelUtil{
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook1(String fileName, String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 设置当前sheet中默认的列宽为10个字符
sheet.setDefaultColumnWidth(15);
// 设置当前sheet中2,3,4列为12,15,20个字符
sheet.setColumnWidth(1, 25 * 256);
sheet.setColumnWidth(2, 25 * 256);
// 第三步,在第一行设置标题栏
// 定义单元格的样式
HSSFCellStyle cellStyle = wb.createCellStyle();
// 设置字体样式,13号,加粗
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 15);
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle.setFont(font1);
// 设置水平居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 标题栏所在sheet的行为第一行
HSSFRow titleRow = sheet.createRow(0);
// 设置行高
titleRow.setHeight((short) 500);
// 设置第一行的第一个单元格
HSSFCell titleCell = titleRow.createCell(0);
// 设置标题文本
titleCell.setCellValue(new HSSFRichTextString(fileName));
// 设置单元格样式
titleCell.setCellStyle(cellStyle);
// 处理单元格合并,四个参数分别是:起始行,终止行,起始行,终止列
sheet.addMergedRegion(new CellRangeAddress(0, 0, (short) 0, (short) (title.length - 1)));
// 设置合并后的单元格的样式
titleRow.createCell(title.length - 1).setCellStyle(cellStyle);
// 第四步,在sheet中添加标题行,位于表头第1行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(1);
// HSSFRow row = sheet.createRow(0);
// 创建title行cell样式
HSSFCellStyle style = wb.createCellStyle();
// 设置title样式剧中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 15);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
// 声明列对象
HSSFCell cell = null;
// 创建标题
for (int i = 0; i < title.length; i++) {
// 创建cell
cell = row.createCell(i);
// 填入内容
cell.setCellValue(title[i]);
// 设置cell内容的样式
cell.setCellStyle(style);
}
// 第五步,在sheet中添加内容,位于表头第2行开始
// 定义内容cell样式
HSSFCellStyle styleContent = wb.createCellStyle();
// 设置样式 居中,字体,边框
styleContent.setAlignment(CellStyle.ALIGN_CENTER);
font = wb.createFont();
font.setFontHeightInPoints((short) 13);
styleContent.setFont(font);
// 设置边框样式
// styleContent.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// styleContent.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// styleContent.setBorderTop(HSSFCellStyle.BORDER_THIN);
// styleContent.setBorderRight(HSSFCellStyle.BORDER_THIN);
// int index = 0;//记录额外创建的sheet数量
// 创建内容
for (int i = 0; i < values.length; i++) {
//如果行数超过65535行,一页显示不完,再创建一个sheet
// if ((i + 2) % 65535 == 0) {
// sheet = wb.createSheet("sheet" +(index+2));
// // 设置当前sheet中默认的列宽为10个字符
// sheet.setDefaultColumnWidth(15);
// // 设置当前sheet中2,3,4列为12,15,20个字符
// sheet.setColumnWidth(1, 25 * 256);
// sheet.setColumnWidth(2, 25 * 256);
// row = sheet.createRow(0);
// for (int j = 0; j < values[0].length; j++) {
// // 将内容按顺序赋给对应的列对象
// HSSFCell createCell = row.createCell(j);
// createCell.setCellValue(values[i][j]);
// createCell.setCellStyle(styleContent);
// }
// index++;
// row = sheet.createRow((i + 2) -(index * 65535) );
row = sheet.createRow(i+2);
// row = sheet.createRow(i+1);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
HSSFCell createCell = row.createCell(j);
createCell.setCellValue(values[i][j]);
createCell.setCellStyle(styleContent);
}
}
return wb;
}
}
controller 代码片
.
/**
* 实现功能描述:导出
*/
@RequestMapping(value = "/toExcel", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public void toExcel(@Param("id") String id,HttpServletRequest request,HttpServletResponse response) throws Exception {
QueryMemberParamsDTO dto=new QueryMemberParamsDTO();
dto.setProjectId(id);
_IQueryService.toExcel(dto,request,response);
}
imp实现类 代码片
.
@Override
public void toExcel(QueryMemberParamsDTO dto, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<QueryMemberParamsVO> list = sysEmployeeMapper.getListNotPending(dto);
String[] title = { "序号", "工号", "姓名" }; // 定义excel标题
String fileName = "未答复人员表"; // 定义excel文件名
String sheetName = "Sheet1"; // 定义sheet名
// 封装excel表格内容
String[][] content = new String[list.size()][title.length];
for (int i = 0; i < list.size(); i++) {
QueryMemberParamsVO stu = list.get(i);
content[i][0] = String.valueOf(i+1);
content[i][1] = stu.getEmployeeNo();
content[i][2] = stu.getRealName();
}
// 创建HSSFWorkbook,写入sheet名称,标题名称,内容等信息
HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook1(fileName, sheetName, title, content, null);
// 响应到客户端
try {
// 获取浏览器的user-agent参数,进行输出文件名称编码格式转换
String userAgent = request.getHeader("user-agent").toLowerCase();
if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
// win10 ie edge 浏览器 和其他系统的ie
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
// 非IE
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
}
// 设置返回响应为excel类型
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xls");
// 输出响应
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
前端导出按钮 代码片
.
<el-button @click="exportExcel()">导出</el-button>
前端请求方法代码片
.
//导出excel表
exportExcel() {
window.location.href="/api/ics/project/toExcel?id=" + this.excelId;
},