servlet 通过poi 实现Excel 下载问题
代码完全可以实现下载后从数据库获取的数据也没问题。问题是下载下来的文件名字却是类名在自动获取字符,还有就是文件下载之后不是.xls格式。求解决方法poi类privateD...
代码完全可以实现 下载后从数据库获取的数据也没问题。 问题是 下载下来的文件 名字却是类名在自动获取字符,还有就是文件下载之后不是.xls格式。 求解决方法
poi类
private DBConntion con;
public ExcelDownLoad() {
con=new DBConntion();
}
//@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public void exportExcel(String name,OutputStream out){
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(name);
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setCellValue("姓名");
cell=row.createCell((short)1);
cell.setCellValue("电子邮件");
String sql="select name,email from db_user";
int nRow=1;
try{
ResultSet rs=con.execQuery(sql);
while(rs.next()){
row=sheet.createRow(nRow++);
cell=row.createCell((short)0);
cell.setCellValue(rs.getString("name"));
cell=row.createCell((short)1);
cell.setCellValue(rs.getString("email"));
}
rs.close();
}catch(SQLException e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
try{
wb.write(out);
}catch(Exception e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
}
servlet类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
String name=req.getParameter("user");
resp.setContentType("application/vnd.ms-excel");
ExcelDownLoad excel=new ExcelDownLoad();
excel.exportExcel(name,resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
this.doGet(req, resp);
}
前台
<body>
<form action="POITest" method="post">
<input type="submit" value="下载">
<input type="hidden" name="user" value="电子邮件">
</form>
</body> 展开
poi类
private DBConntion con;
public ExcelDownLoad() {
con=new DBConntion();
}
//@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public void exportExcel(String name,OutputStream out){
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(name);
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setCellValue("姓名");
cell=row.createCell((short)1);
cell.setCellValue("电子邮件");
String sql="select name,email from db_user";
int nRow=1;
try{
ResultSet rs=con.execQuery(sql);
while(rs.next()){
row=sheet.createRow(nRow++);
cell=row.createCell((short)0);
cell.setCellValue(rs.getString("name"));
cell=row.createCell((short)1);
cell.setCellValue(rs.getString("email"));
}
rs.close();
}catch(SQLException e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
try{
wb.write(out);
}catch(Exception e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
}
servlet类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
String name=req.getParameter("user");
resp.setContentType("application/vnd.ms-excel");
ExcelDownLoad excel=new ExcelDownLoad();
excel.exportExcel(name,resp.getOutputStream());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doPost(req, resp);
this.doGet(req, resp);
}
前台
<body>
<form action="POITest" method="post">
<input type="submit" value="下载">
<input type="hidden" name="user" value="电子邮件">
</form>
</body> 展开
若以下回答无法解决问题,邀请你更新回答
展开全部
用户自己选择保存路径
那就是弹出一个IE的下载对话框,代码非常简单!代码如下:
protected void downloadExcel(HSSFWorkbook workbook, HttpServletResponse response, String filename) throws IOException {
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);
out.close();
}
HSSFWorkbook:如果你用的是POI导出Excel,那么这个对象就不用我说了,你懂的!
HttpServletResponse:这个更不用说,你懂的!
String:这个你也懂的,接受的是文件名,请注意不要带任何路径,只是一个文件名而已!这个传过来的目的是在弹出的IE下载对话框中可以看见文件名、文件类型!肯定是要带后缀名的,否则无法识别。
那就是弹出一个IE的下载对话框,代码非常简单!代码如下:
protected void downloadExcel(HSSFWorkbook workbook, HttpServletResponse response, String filename) throws IOException {
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);
out.close();
}
HSSFWorkbook:如果你用的是POI导出Excel,那么这个对象就不用我说了,你懂的!
HttpServletResponse:这个更不用说,你懂的!
String:这个你也懂的,接受的是文件名,请注意不要带任何路径,只是一个文件名而已!这个传过来的目的是在弹出的IE下载对话框中可以看见文件名、文件类型!肯定是要带后缀名的,否则无法识别。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询