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; }