Spring 如何导出当前页面的表格为Excel
答案:2 悬赏:70 手机版
解决时间 2021-02-03 05:37
- 提问者网友:孤凫
- 2021-02-02 19:41
现在页面上有一个按钮,写着导出,可以导出当前页面的数据为Excel,导出按钮在jsp里写着,请问,js和后台controller该怎么写?
最佳答案
- 五星知识达人网友:末日狂欢
- 2021-02-02 19:57
1.前台页面
poi.jar gson2.2.2.jar
< a href ="hello/download.htm">下载导入模板 </a>
2.后台操作
@RequestMapping("download.htm" )
public void download(HttpServletResponse response){
String methodName = "dowloadCmmRange";
LOGGER.info("download_start" , methodName);
response.setContentType( "application/vnd.ms-excel");
String fileName = "";
try {
fileName = new String("商品范围导入.csv" .getBytes("GBK"), "iso8859-1");
} catch (UnsupportedEncodingException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
BufferedWriter bw = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(outputStream, "GBK" ));
bw.append( "商品编码" ).append(",").append( "品牌编码" ).append(",")
.append( "品类编码" ).append(",").append( "事业部编码" )
.append( ",").append("供应商编码" ).append(",")
.append( "操作(包含I/排除E)" );
bw.flush();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
}
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
}
}
LOGGER.info("download_end" , methodName);
}
poi.jar gson2.2.2.jar
< a href ="hello/download.htm">下载导入模板 </a>
2.后台操作
@RequestMapping("download.htm" )
public void download(HttpServletResponse response){
String methodName = "dowloadCmmRange";
LOGGER.info("download_start" , methodName);
response.setContentType( "application/vnd.ms-excel");
String fileName = "";
try {
fileName = new String("商品范围导入.csv" .getBytes("GBK"), "iso8859-1");
} catch (UnsupportedEncodingException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
BufferedWriter bw = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(outputStream, "GBK" ));
bw.append( "商品编码" ).append(",").append( "品牌编码" ).append(",")
.append( "品类编码" ).append(",").append( "事业部编码" )
.append( ",").append("供应商编码" ).append(",")
.append( "操作(包含I/排除E)" );
bw.flush();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
}
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
throw new BaseException("SP-CTR-CouponRuleCommonController.dowloadCmmRange" , e);
}
}
}
LOGGER.info("download_end" , methodName);
}
全部回答
- 1楼网友:山君与见山
- 2021-02-02 20:52
后台把数据再查出来一遍,然后调用poi,或jxl生成excel,返回给页面就行了。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯