需要引入的maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
excel数据处理方法
/**
* 导出Excel
*
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @param he_ji 尾部追加数据
* @param cow 合并行
* @return wb HSSFWorkbook对象
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb, String[] he_ji,int cow) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-MM-dd HH:mm:ss"));
// 设置字体
HSSFFont fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short) 14); //设置字体高度
//声明列对象
HSSFCell cell = null;
Map<Integer, Integer> columnWidth = new HashMap<>(); // 存储最大得列宽值
//创建标题
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
style.setFont(fontStyle);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
fontStyle.setBold(true); // 设置为粗体
columnWidth.put(i, title[i].length());
}
//创建内容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
if (null != values[i][j] && columnWidth.get(j).intValue() < values[i][j].length()) {
columnWidth.put(j, values[i][j].length());
}
}
row.setRowStyle(style);
}
// 判断是否拥有合计字段
if (null != he_ji) {
row = sheet.createRow(values.length+1);
for (int i = 0; i < he_ji.length; i++) {
row.createCell(i).setCellValue(he_ji[i]);
}
CellRangeAddress region = new CellRangeAddress(values.length+1, values.length+1, 0, cow);
sheet.addMergedRegion(region);
}
// 遍历取最大值获取列宽,实现自适应功能
for (Map.Entry<Integer, Integer> m : columnWidth.entrySet()) {
sheet.setColumnWidth(m.getKey(), (int) (m.getValue() + 0.72) * 3 * 256);
}
return wb;
}
把文件以流的形式传输到本地
HSSFWorkbook wb = StringUtil_lily.getHSSFWorkbook("测试", title, values, null, null, 0);
// 响应到客户端
StringUtil_lily.setResponseHeader(response, "测试.xls");
OutputStream os = null;
try {
os = response.getOutputStream();
wb.write(os);
wb.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
前端提交数据并将文件下载二种方式
1.form表单形式提交
$('#basicForm').validate({
errorPlacement: function (error, element) {
error.insertAfter(element.parent());
},
submitHandler: function (form) {
$('#basicForm').submit();
}
});
2.get方式提交
window.location.href = "url路径?"+ $("#queryForm").serialize();