后台代码
POI
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("客户信息表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
// 创建一个居中格式
style.setAlignment(HorizontalAlignment.CENTER);
//设置标题栏
String [] cellValue = {"客户姓名","服务方向","性别","联系方式","负责人","导入时间","来源"};
HSSFCell cell = row.createCell((short) 0);
for (int i = 0;i<cellValue.length;i++){
cell.setCellValue(cellValue[i]);
cell.setCellStyle(style);
//设置单元格宽度
sheet.setColumnWidth(i,20*256);
//设置居中样式
sheet.setDefaultColumnStyle(i,style);
cell = row.createCell((short) i+1);
}
// 第五步,写入实体数据 数据从数据库得到,
List<CustomerDO> list = customerService.findAll(customerDO);
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow( i + 1);
CustomerDO customerDOExcel = list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(customerDOExcel.getName());
row.createCell((short) 1).setCellValue(customerDOExcel.getService());
row.createCell((short) 2).setCellValue(customerDOExcel.getSex());
row.createCell((short) 3).setCellValue(customerDOExcel.getPhone());
row.createCell((short) 4).setCellValue(customerDOExcel.getResponsible());
row.createCell((short) 5).setCellValue(new SimpleDateFormat("yyyy-mm-dd HH:MM:ss").format(customerDOExcel.getCreateDate()));
row.createCell((short) 6).setCellValue(customerDOExcel.getSource());
}
// 第六步,将文件存到指定位置
OutputStream out = null;
try {
out = response.getOutputStream();
// 文件名
String fileName = "客户信息表.xls";
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
wb.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面是设置列宽参考:来自连接https://blog.csdn.net/u012794845/article/details/81080171
在使用NPOI技术开发自动操作EXCEL软件时遇到不能精确设置列宽的问题。
如
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
sheet1.SetColumnWidth(0, 50 * 256); // 在EXCEL文档中实际列宽为49.29
sheet1.SetColumnWidth(1, 100 * 256); // 在EXCEL文档中实际列宽为99.29
sheet1.SetColumnWidth(2, 150 * 256); // 在EXCEL文档中实际列宽为149.29
到此一般人应该知道问题出在哪了,解决方法如下:
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
sheet1.SetColumnWidth(0, (int)((50 + 0.72) * 256)); // 在EXCEL文档中实际列宽为50
sheet1.SetColumnWidth(1, (int)((100 + 0.72) * 256)); // 在EXCEL文档中实际列宽为100
sheet1.SetColumnWidth(2, (int)((150 + 0.72) * 256)); // 在EXCEL文档中实际列宽为150
既在要设置的实际列宽中加上列宽基数:0.72
前台vue页面下载
var url = config.oaUrl + 'customer/exportCustomer';
fetch(url,{
method: 'POST',
headers: new Headers({
'token':dataUtils.getData(config.key.tokenKey) // 指定提交方式为表单提交
}),
}).then(res => res.blob().then(blob => {
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
var filename = '客户信息表.xlsx';
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}));
效果如下: