admin管理员组文章数量:1535100
2022年5月31日更新, 推荐easyExcel或者Crab2Died/Excel4J, 使用起来很方便
废话不多说, 直接上代码.
1. 依赖
<properties>
<poi.version>3.11</poi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
2. 代码
3.1 控制层
@RequestMapping(value = "/exportStoreOrderList")
public String exportXls(){
///创建excel代码
exportService.exportXls();
return NONE;
}
接口省略
3.2 serviceImpl
@Override
public void exportXls() {
try {
//查询分区所有数据
List<Order> listOrder = orderDao.findAll();
//当前excel创建对象
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet createSheet = hwb.createSheet();//创建sheet页
/*设置列宽*/
createSheet.setColumnWidth(0, 10000);
/*设置字体,边框,这个有心思的可以去详细找找教程,这里实在不好弄*/
HSSFCellStyle style = hwb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 靠左
// 字体
HSSFFont font = hwb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);// 12号字体
style.setFont(font);
HSSFRow titleRow = createSheet.createRow(0);//创建标题行
titleRow.createCell(0).setCellValue("订单编号");
titleRow.createCell(1).setCellValue("商品编号");
titleRow.createCell(2).setCellValue("商品名");
titleRow.createCell(3).setCellValue("商品价格");
titleRow.createCell(4).setCellValue("下单时间");
if(listOrder != null){
for (Order data: listOrder ) {
///循环在标题行后添加数据
int lastRowNum = createSheet.getLastRowNum()+1;//当前需要增加行的下标
HSSFRow dataRow = createSheet.createRow(lastRowNum);
dataRow.createCell(0).setCellValue(data.getId());
dataRow.createCell(1).setCellValue(data.getGoodId());
dataRow.createCell(2).setCellValue(data.getGoodNum());
dataRow.createCell(3).setCellValue(data.getPrice());
dataRow.createCell(4).setCellValue(data.getTime());
}
}
//边框
for (int j = 0; j <= 4 ; j++) {
dataRow.getCell(j).setCellStyle(style);
}
//一个流 两个头
//文件名称
String filename = "数据.xls";
//获取User-agent 当前是哪个浏览器
String userAgent = ServletActionContext.getRequest().getHeader("User-Agent");
// 这里使用到FileUtils工具类进行编码
String encodeFilename = FileUtils.encodeDownloadFilename(filename, userAgent);
ServletActionContext.getResponse().setHeader("content-disposition", "filename="+encodeFilename);
//文件后缀
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=UTF-8");
//输出流对象
ServletOutputStream outputStream = ServletActionContext.getResponse().getOutputStream();
hwb.write(outputStream);
hwb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
不对文件名进行编码处理的话, 有些浏览器无法识别下载文件
注意: 如果页面使用了frame框架, 点击mvc方法调用地址, 但是没有弹出下载框, 而其他的配置代码没有问题的话, 用get请求,window.open(url+?参数)试试
3.3 FileUtils
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Base64;
public class FileUtils {
/**
* 下载文件时,针对不同浏览器,进行附件名的编码
*
* @param filename
* 下载文件名
* @param agent
* 客户端浏览器
* @return 编码后的下载附件名
* @throws IOException
*/
public static String encodeDownloadFilename(String filename, String agent)
throws IOException {
// 如果是火狐浏览器
if (agent.contains("Firefox")) {
filename = "=?UTF-8?B?"
+ Base64.getEncoder().encodeToString(filename.getBytes("utf-8"))
+ "?=";
filename = filename.replaceAll("\r\n", "");
// IE及其他浏览器
} else {
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+"," ");
}
return filename;
}
}
版权声明:本文标题:如何使用POI导出excel表格,以及处理浏览器无法识别下载文件的问题 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1726897869a1089219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论