导出Excel问题
我在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();
}
}