导出Excel问题

pingdiyishenglei 2014-06-25 10:15:02
我在jsp页面导出Excel,但是没有按照我定义的格式导出,而是将jsp页面上的内容显示出来了?请大侠们看一下,究竟错在哪里?
具体代码如下:
@Override
public Map definitionExcel(HttpServletRequest request,CmsUser user) {
Map map = new HashMap();
try{
String sdfDate = new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String fileName = "TrainDemandGatherSheet" +sdfDate;

String RealPath = "D:/";
String path = RealPath + "/download//" + fileName + ".xls";
// String path = "/isoftstoneCMS/download/";
map.put("path",path);
map.put("sheetname", "sheet名称");

// String base = request.getContextPath();
// String path = base + "/isoftstoneCMS/WEB-INF/download//" + fileName + ".xls";
// map.put("path",path);
// map.put("sheetname", "sheet名称");

List list1 = new ArrayList();

String[] title1={"用户名","时间","产品知识","销售技能","心态建设","主顾开拓","契约知识","保险意义与功用","其它方面","需求描述"};
list1.add(title1);
CourseDemandPublish courseDemandPublish = courseDemandPublishDao.getCourseDemandPublish(user.getId().toString());
String[] arr = convertValue(courseDemandPublish);
String[] checkOut={user.getUsername(),courseDemandPublish.getUpdateDate().toString(),
arr[0],arr[1],
arr[2],arr[3],
arr[4],
arr[5],
arr[6],courseDemandPublish.getRequirementDescription()};
list1.add(checkOut);
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dt1 = sdf1.format(date);
String[] s1={"",""};
String[] s2={"制表人:"+"123"};
String[] s3={"制表时间:"+dt1};
String[] fileNameArr ={fileName};
list1.add(s1);
list1.add(s2);
list1.add(s3);
list1.add(fileNameArr);
map.put("list", list1);
}catch (Exception e) {
e.printStackTrace();
}
return map;
}



/**
* 写Excel
*/
public void writeExcel(HttpServletResponse response,Map map){

String outputFile = (String) map.get("path");

HSSFWorkbook workbook; //实例化一个工作簿
//文件名称
String fileName = "";



try {
response.reset();

workbook = new HSSFWorkbook();

HSSFCellStyle style1 = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style1.setFont(font);

HSSFCellStyle style2 = workbook.createCellStyle();
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中

HSSFSheet sheet = workbook.createSheet((String) map.get("sheetname"));//建立一张表格
List<String[]> l = new ArrayList<String[]>();
l=(List<String[]>) map.get("list");
for(int i=0;i<l.size();i++){
HSSFRow row=sheet.createRow(i);
String[] str = l.get(i);
if(i<l.size()-3){
for(int j=0;j<str.length;j++){
HSSFCell cell=row.createCell(j);
cell.setCellValue(str[j]);
if(i<1){
cell.setCellStyle(style1);
}else{
cell.setCellStyle(style2);
}
if(str[j]!=""&&str[j]!=null){
int value = str[j].getBytes().length;
sheet.setColumnWidth(j, 20*250);
}
}
}
if(i == l.size()-1){
fileName = str[0];
}
}

FileOutputStream fout=new FileOutputStream(outputFile);
// 这个是弹出下载对话框的关键代码
String str = fileName.trim()+".xls";
response.setHeader ( "Content-Disposition" ,"attachment;filename="+new String(str.getBytes(),"UTF-8"));
response
.setContentType("application/x-download; charset=utf-8");

workbook.write(fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
...全文
112 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiabiaoli 2014-06-25
  • 打赏
  • 举报
回复
我之前是 先将 xls文件保存在本地,,, 然后 FileInputStream fis=new FileInputStream(new FIle("path")); OutputStream os=response.getOutputStream(); int len = 0; byte[] bt = new byte[1024 * 2]; while ((len = fis.read(bt)) != -1) { os.write(bt, 0, len); } os.flush(); 大概就是这个样子. 个别错误单词 请无视.
jiabiaoli 2014-06-25
  • 打赏
  • 举报
回复
引用 1 楼 jiabiaoli 的回复:
workbook.write(fout); 应该改为 workbook.write(response.getOutputStream());吧
写反了,...
jiabiaoli 2014-06-25
  • 打赏
  • 举报
回复
workbook.write(fout); 应该改为 workbook.write(response.getOutputStream());吧
中华雪碧 2014-06-25
  • 打赏
  • 举报
回复
response.setContentType("application/ms-excel");//头设置成excel的 response.setHeader("Content-disposition", "attachment;filename=test.xls");

81,092

社区成员

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

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