第一次使用这个东西,借鉴了网上某个大神的demo,可是没成功,请问哪里有问题
控制器:
@org.springframework.stereotype.Controller(value = "interviewLogController")
@Scope(value = "prototype")
public class InterviewLogController implements Controller {
@Resource(name="InterviewLogBiz")
private InterviewLogBiz interviewLogBiz;
@RequestMapping(value = "ExportInterviewLogQuery2")
public void exportInterviewExcel2(HttpServletRequest req,
HttpServletResponse resp) throws Exception {
Interview interview = new Interview();
//File file = new File(getServletContext().getRealPath("WEB-INF/book.jpg"));
resp.setContentType("octets/stream");
resp.addHeader("Content-Disposition",
"attachment;filename=学员访谈记录表.xls");
ExportExcel<Interview> ex = new ExportExcel<Interview>();
//ExportExcel<T> ex = new ExportExcel<T>();
//创建列
String[] headers = { "姓名", "访谈时间", "电话或当面访谈内容", "备注" };
//从数据库中读取数据
List<Interview> dataset = new ArrayList<Interview>();
String csid = req.getParameter("csid");
interview.setCsid(csid);
if(csid!=null && csid!=""){
dataset = interviewLogBiz.query(interview.getCsid());
interview.setName(dataset.get(0).getName());
interview.setIinterviewTime(dataset.get(0).getIinterviewTime());
interview.setIcontent(dataset.get(0).getIcontent());
interview.setInterviewWay(dataset.get(0).getInterviewWay());
}else{
return ;
}
try {
dataset.add(new Interview(interview.getName(), interview.getIinterviewTime(),
interview.getIcontent(), interview.getInterviewWay()));
OutputStream out = resp.getOutputStream();
ex.exportExcel(headers, dataset, out);
out.close();
System.out.println("excel导出成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
ExportExcel.java:
package com.it94hao.util;
public class ExportExcel<T> {
public void exportExcel(String[] headers, Collection<T> dataset,
OutputStream out) {
exportExcel("测试POI导出EXCEL文档", headers, dataset, out, "yyyy-MM-dd");
}
@SuppressWarnings("unchecked")
public void exportExcel(String title, String[] headers,
Collection<T> dataset, OutputStream out, String pattern) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);