jsp导出excel格式问题

-川川川- 2015-09-12 02:34:32
现在项目要实现导出功能,但通过网页直接下载内容,最后保存的是html格式,客户不能接受,
网上查了下好像没有办法修改格式,用jxl输出到本地再提供下载的话开发工作量又不小,
所以想问下各位大神有没有什么好的解决办法,或者有没有什么转换格式的小工具?

String filen = request.getParameter("exp_filename");
if(Convert.isNullOrEmpty(filen))
filen = "Excel.xls";
String cont = request.getParameter("exp_cont");

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="+new String(filen.getBytes(),"iso8859-1"));
%>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<%=cont%>
...全文
185 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqyg000 2015-09-14
  • 打赏
  • 举报
回复
引用 1 楼 oh_Maxy 的回复:
你把内容写入到xxx.xls ,然后直接往response里写。 我们有个项目是写的word,给你参考下:

        XWPFDocument doc = null;
        // ... 经过一系列逻辑,写入操作之后非空
        // ...
        
        // 避免空指针
        if (null != doc) {
            // 拼接文件名,下载时显示的名字
            String fileName = "MyInfo.docx";

            // response的一些头设置
            resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            resp.setContentType("application/x-download;charset=utf-8");
            resp.setCharacterEncoding("utf-8");
            // 最后文件流写入到页面响应的输出流中
            doc.write(resp.getOutputStream());
            resp.flushBuffer();
        }
excel应该类似,试试吧!
_(:з」∠)_ 这个还是得用jxl或者poi导出到本地,那个表格太复杂了,这样开发的话工作量非常大。
小雷同学 2015-09-14
  • 打赏
  • 举报
回复
给你一段我的代码,格式问题在模板李控制
小雷同学 2015-09-14
  • 打赏
  • 举报
回复
List<Asset> list = assetService.search(asset); String templateFileName = request.getServletContext().getRealPath("/") + "/template/asset.xls"; String destFileName = "资产信息表.xls"; Map<String, Object> beans = new HashMap<String, Object>(); beans.put("data", list); try { ExportExcelTool.exportExcel(request, response, destFileName, templateFileName, beans); } catch (UnsupportedEncodingException e) { response.reset(); response.setStatus(500); e.printStackTrace(); } /** * * @param request * 为获取浏览器类型 * @param response * @param fileName * 前台下载时显示的文件名 * @param templateFileName * 模板文件路径 * @param data * 封装的数据 * @throws UnsupportedEncodingException */ public static void exportExcel(HttpServletRequest request, HttpServletResponse response, String fileName, String templateFileName, Map<String, Object> data) throws UnsupportedEncodingException { XLSTransformer transformer = new XLSTransformer(); BufferedInputStream in = null; BufferedOutputStream out = null; // 设置响应 if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) { response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1")); } // IE & Other else {// response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); } response.setContentType("application/vnd.ms-excel"); try { in = new BufferedInputStream(new FileInputStream(templateFileName)); Workbook workbook = transformer.transformXLS(in, data); out = new BufferedOutputStream(response.getOutputStream()); // 将内容写入输出流并把缓存的内容全部发出去 workbook.write(out); out.flush(); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } }
oh_Maxy 2015-09-13
  • 打赏
  • 举报
回复
你把内容写入到xxx.xls ,然后直接往response里写。 我们有个项目是写的word,给你参考下:

        XWPFDocument doc = null;
        // ... 经过一系列逻辑,写入操作之后非空
        // ...
        
        // 避免空指针
        if (null != doc) {
            // 拼接文件名,下载时显示的名字
            String fileName = "MyInfo.docx";

            // response的一些头设置
            resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            resp.setContentType("application/x-download;charset=utf-8");
            resp.setCharacterEncoding("utf-8");
            // 最后文件流写入到页面响应的输出流中
            doc.write(resp.getOutputStream());
            resp.flushBuffer();
        }
excel应该类似,试试吧!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧