Struts1.1通过模板导出Excel (jxl.jar)
??
import java.io.File;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.List;import jxl.Workbook;import jxl.write.Label;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;public class MssExcelBean {public MssExcelBean() {}public void expordExcel(OutputStream os,String beginDate,String endDate) throws Exception{int row = 2; //从第三行开始写int col = 0; //从第一列开始写Workbook wb = Workbook.getWorkbook(new File("E:\\MSSTemplate.xls"));WritableWorkbook wwb = Workbook.createWorkbook(os, wb);WritableSheet ws = wwb.getSheet(0);System.out.println("sheet页的名称:" + ws.getName());MssServiceAction mssService = new MssServiceAction();List list = mssService.getMssInfoByDate(beginDate, endDate);if(list != null && list.size() > 0 ) {for(int i=0;i<list.size();i++) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");MssEntity mss = (MssEntity)list.get(i);ws.addCell(new Label(col++,row,mss.getMssType())); // 写工单类型ws.addCell(new Label(col++,row,mss.getMssId())); // 写工单编号ws.addCell(new Label(col++,row,mss.getMssRequestPerson())); // 写请求人ws.addCell(new Label(col++,row,mss.getMssStatuts())); // 写工单状态ws.addCell(new Label(col++,row,mss.getMssCreatePerson())); // 写创建人ws.addCell(new Label(col++,row,sdf.format(mss.getMssCreateTime()))); // 写创建时间ws.addCell(new Label(col++,row,mss.getMssPriority())); // 写优先级ws.addCell(new Label(col++,row,mss.getMssAcceptGroup())); // 写受理组ws.addCell(new Label(col++,row,mss.getMssAcceptPerson())); // 写受理人ws.addCell(new Label(col++,row,mss.getMssTitle())); // 写标题ws.addCell(new Label(col++,row,mss.getMssContractPerson())); // 写联系人ws.addCell(new Label(col++,row,mss.getMssCompany())); // 写所在公司ws.addCell(new Label(col++,row,mss.getMssContractNum())); // 写联系电话ws.addCell(new Label(col++,row,mss.getMssSystem())); // 写专业类别ws.addCell(new Label(col++,row,mss.getMssDesc())); // 写详述ws.addCell(new Label(col++,row,sdf.format(mss.getMssDealTime()))); // 写反馈时间ws.addCell(new Label(col++,row,mss.getMssDealProcess())); // 写处理过程row++;} }wwb.write();wwb.close();wb.close();os.close();}}
?
?
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {request.setCharacterEncoding("UTF-8");String beginDate = request.getParameter("beginDate");String endDate = request.getParameter("endDate");String fname = beginDate + "到"+ endDate + "工单" + ".xls";//导出的excel名字System.out.println(fname);System.out.println("ResponseEncoding:" + response.getCharacterEncoding());try {//PrintWriter out = response.getWriter();OutputStream os = response.getOutputStream(); //取得输出流response.reset(); //清空输出流response.setHeader("Content-disposition", "attachment;filename=" + new String(fname.getBytes("GBK"), "ISO8859-1"));response.setContentType("application/msexcel;charset=UTF-8");//定义输出类型MssExcelBean mssExcel = new MssExcelBean();mssExcel.expordExcel(os, beginDate, endDate);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally{}return null;}
?
?
?