您的位置:首页 > 其它

查询结果Excel的导出

2009-08-12 19:20 465 查看
import Excel导出要使用的包poi-3.2-FINAL-20081019.jar

public class ExcelAuditServlet extends HttpServlet {

/**
* @param bppl 要输出的列表
* @param sheet
*/
protected void createSheets(List<TotalAccount> tal,HSSFSheet sheet) {
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.createRow(NumberConstants.NUM_0);
cell = row.createCell(NumberConstants.NUM_0);
cell.setCellValue(new HSSFRichTextString("对账时间"));
cell = row.createCell(NumberConstants.NUM_1);
cell.setCellValue(new HSSFRichTextString("对账类型"));
cell = row.createCell(NumberConstants.NUM_2);
cell.setCellValue(new HSSFRichTextString("对账起始时间"));
cell = row.createCell(NumberConstants.NUM_3);
cell.setCellValue(new HSSFRichTextString("对账终止时间"));
cell = row.createCell(NumberConstants.NUM_4);
cell.setCellValue(new HSSFRichTextString("总笔数"));
cell = row.createCell(NumberConstants.NUM_5);
cell.setCellValue(new HSSFRichTextString("总金额(元)"));
cell = row.createCell(NumberConstants.NUM_6);
cell.setCellValue(new HSSFRichTextString("对账状态"));
cell = row.createCell(NumberConstants.NUM_7);
cell.setCellValue(new HSSFRichTextString("对账状态描述"));
cell = row.createCell(NumberConstants.NUM_8);
cell.setCellValue(new HSSFRichTextString("明细对账"));
cell = row.createCell(NumberConstants.NUM_9);
cell.setCellValue(new HSSFRichTextString("对账接口"));
if(tal!=null){
for(int i=0;i<tal.size();i++){
TotalAccount ta = tal.get(i+1);
if(ta==null){
continue;
}
row = sheet.createRow(i+1);
cell = row.createCell(NumberConstants.NUM_0);
cell.setCellValue(new HSSFRichTextString(ta.getOperTime().toString()));
cell = row.createCell(NumberConstants.NUM_1);
cell.setCellValue(new HSSFRichTextString(ta.getAuditType().getValue()));
cell = row.createCell(NumberConstants.NUM_2);
cell.setCellValue(new HSSFRichTextString(ta.getBeginTime().toString()));
cell = row.createCell(NumberConstants.NUM_3);
cell.setCellValue(new HSSFRichTextString(ta.getEndTime().toString()));
cell = row.createCell(NumberConstants.NUM_4);
cell.setCellValue(new HSSFRichTextString(ta.getTotalCount().toString()));
cell = row.createCell(NumberConstants.NUM_5);
cell.setCellValue(new HSSFRichTextString(ta.getTotalAmount().toString()));
cell = row.createCell(NumberConstants.NUM_6);
cell.setCellValue(new HSSFRichTextString(ta.getAuditType().getValue()));
cell = row.createCell(NumberConstants.NUM_7);
cell.setCellValue(new HSSFRichTextString(ta.getDescr()));
cell = row.createCell(NumberConstants.NUM_8);
cell.setCellValue(new HSSFRichTextString(ta.getAuditId()));
cell = row.createCell(NumberConstants.NUM_9);
cell.setCellValue(new HSSFRichTextString(ta.getAuditInterf().getDisplayName()));
}
}
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
//没有操作权限时调用这个方法会抛出异常
WebUtil.getMSC(request.getSession());
request.setCharacterEncoding("gbk");
String t = request.getParameter("time");
Date time;
try {
time = new SimpleDateFormat("yyyy-MM").parse(t);
} catch (ParseException e) {
throw new RuntimeException(e);
}
AuditStatus auditStatus = AuditStatus.getEnumByValue(new Byte(request.getParameter("auditStatus")));
AuditType auditType = AuditType.getEnumByValue(request.getParameter("auditType"));
BookInterface bookInterface = EliveUtilFactory.getBookInterfaceByInterf(request.getParameter("key"));
int from = 0;
int top = Utility.MAX_RETURN_ROWS;
boolean hasCount = false;
AuditQueryParam aqp=AuditFactory.getAuditQueryParam(time,auditStatus,
auditType,bookInterface,from,top,hasCount);
String filename = request.getParameter("filename");
try {
JPrincipal principal = WebUtil.getMJP(request.getSession());
List<TotalAccount> tal = MBeanFactory.getAuditManagerMBean().queryAuditRecord(principal, aqp);
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet hst = hwb.createSheet();
createSheets(tal,hst);
response.setContentType("application/vnd.ms-excel");
filename = new String(filename.getBytes(WebUtil.GBK_CHARSET),WebUtil.getDefaultCharsetName());
response.setHeader("Content-disposition", "attachment; filename="+ filename + ".xls");
OutputStream out = response.getOutputStream();
hwb.write(out);
out.close();
} catch (Exception ex) {
throw new ServletException("ExcelBackVisitMonthServlet", ex);
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: