javapoi数据导出成excel如何才能指定文件输出路径 现在是知道E盘路径 怎么弄成弹框选择路径
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javapoi数据导出成excel如何才能指定文件输出路径 现在是知道E盘路径 怎么弄成弹框选择路径相关的知识,希望对你有一定的参考价值。
HSSFWorkbook wb = new HSSFWorkbook();
//输出数据到wb
FileOutputStream fout = new FileOutputStream("E:/order.xls");
wb.write(fout);
<%@ page contentType="application/vnd.ms-excel; charset=utf-8" %>
<%@ page language="java" import="java.util.*,com.expect.bean.Admin" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String xlsname = request.getAttribute("xlsname").toString();
String fileName = xlsname+".xls";
byte[] yte = fileName.getBytes("GB2312");
String unicoStr = new String(yte, "ISO-8859-1");
response.setHeader("Content-disposition","attachment;filename="+unicoStr);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
#tableinfo tr td
text-align: center;font-size: 13px;border: 1px solid #64ABD6;
#tableinfowidth: 100%;border-collapse: collapse;height: 30px;line-height: 30px;
</style>
</head>
<body >
<table cellspacing="0" id="tableinfo" >
<tr>
<td style="text-align: center;font-size:30px;font-weight: bold;" id="titletd" colspan="8">
<%=xlsname %>电费表<br/>
</td>
</tr>
<tr>
<td style="text-align: center;" id="titletd" colspan="2">
用户名:
</td>
<td style="text-align: center;" id="titletd">
分厂:
</td>
<td style="text-align: center;" id="titletd">
住宅区:
</td>
<td style="text-align: center;" id="titletd" >
楼层:
</td>
<td style="text-align: center;" id="titletd" >
具体住址:
</td>
<td style="text-align: center;" id="titletd" >
上次度数:
</td>
<td style="text-align: center;" id="titletd" >
本次度数:
</td>
<td style="text-align: center;" id="titletd" >
操作时间:
</td>
</tr>
</table>
<%
List list = (List)request.getAttribute("list");
if(list!=null && list.size()>0)
for(int i=0;i<list.size();i++)
Map m = (Map)list.get(i);
String userName = m.get("userName")==null || m.get("userName").equals("") ? "" : m.get("userName").toString();
String branch = m.get("branch")==null || m.get("branch").equals("") ? "" : m.get("branch").toString();
String upTown = m.get("upTown")==null || m.get("upTown").equals("") ? "" : m.get("upTown").toString();
String storey = m.get("storey")==null || m.get("storey").equals("") ? "" : m.get("storey").toString();
String home = m.get("home")==null || m.get("home").equals("") ? "" : m.get("home").toString();
String totalSum = m.get("totalSum")==null || m.get("totalSum").equals("") ? "" : m.get("totalSum").toString();
String enterTime = m.get("enterTime")==null || m.get("enterTime").equals("") ? "" : m.get("enterTime").toString();
String upMonthCount = m.get("upMonthCount")==null || m.get("upMonthCount").equals("") ? "" : m.get("upMonthCount").toString();
%>
<table cellspacing="0" id="tableinfo" >
<tr>
<td style="text-align: center;" id="titletd" colspan="2">
<%=userName %>
</td>
<td style="text-align: center;" id="titletd" >
<%=branch %>
</td>
<td style="text-align: center;" id="titletd" >
<%=upTown %>
</td>
<td style="text-align: center;" id="titletd" >
<%=storey %>
</td>
<td style="text-align: center;" id="titletd" >
<%=home %>
</td>
<td style="text-align: center;" id="titletd" >
<%=totalSum %>
</td>
<td style="text-align: center;" id="titletd" >
<%=upMonthCount %>
</td>
<td style="text-align: center;" id="titletd" >
<%=enterTime %>
</td>
</tr>
</table>
<%
%>
</body>
</html> 参考技术A response.setHeader("Content-Disposition", "attachment;filename=order.xls");// 设定输出文件头
response.setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
OutputStream out = response.getOutputStream();
wb.write(out); 参考技术B 那就在 new FileOutputStream("E:/order.xls"); 前面使用一个文件路径选择框,去一下路径呗本回答被提问者和网友采纳
Java 利用poi 导出excel表格 如何在导出时自由选择路径?
能导出成功,知识代码把路径写死了,想要实现自己选择保存路径。
导出时自由选择路径的代码如下:
1、后台输出Excel文件代码:
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + path);
response.setContentType("Content-Type:application/vnd.ms-excel ");
wb.write(output);
output.close();
2、前端代码:
window.open("getExcelList","_blank");
导出excel数据
* @param id
* @param m
* @return
*/
@RequestMapping("/exportExcel")
public void exportExcel(@RequestParam("id") Integer id, Model m,HttpServletRequest req, HttpServletResponse resp)
try
ExportExcel<P2pLoanPlanVo> ex = new ExportExcel<P2pLoanPlanVo>();
String[] headers = "最迟还款日", "还款金额","剩余几天","逾期几天", "罚息","是否垫付","状态","是否发放收益";
List<P2pLoanPlanVo> dataset = new ArrayList<P2pLoanPlanVo>();
List<P2pLoanPlan> plans = this.planService.getListByLoan(id);
for (int i = 0; i < plans.size(); i++)
P2pLoanPlanVo p2pLoanPlanVo = new P2pLoanPlanVo();
p2pLoanPlanVo.setRepayDate(plans.get(i).getRepayDate());
p2pLoanPlanVo.setRepayAmount(plans.get(i).getRepayAmount());
if(plans.get(i).getRepayDays() >= 0 && plans.get(i).getStatus() == 0)
p2pLoanPlanVo.setRepayDays(plans.get(i).getRepayDays());
else
p2pLoanPlanVo.setRepayDays(0);
if(plans.get(i).getRepayDays() < 0 && plans.get(i).getStatus() == 0)
p2pLoanPlanVo.setRepayYqDays(-plans.get(i).getRepayDays());
else
p2pLoanPlanVo.setRepayYqDays(0);
参考技术A 主要代码如下:后台输出Excel文件代码:
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + path);
response.setContentType("Content-Type:application/vnd.ms-excel ");
wb.write(output);
output.close();
前端代码:window.open("getExcelList","_blank"); 参考技术B 做个临时文件,在重定向到这个临时文件,浏览器就会选择下载追问
我不明白,没学这个多久,望耐心解释~十分感谢~
追答在servlet中,
//把上边的改成try
wb.write(response.getOutputStream());
参考技术C 可以用pageoffice导出excel. pageoffice 调用的是本地的office,导出excel可以弹出另存的对话框,用户可以选择自己想要的位置进行保存.
以上是关于javapoi数据导出成excel如何才能指定文件输出路径 现在是知道E盘路径 怎么弄成弹框选择路径的主要内容,如果未能解决你的问题,请参考以下文章
Java 利用poi 导出excel表格 如何在导出时自由选择路径?