查询结果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);
}
}
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);
}
}
相关文章推荐
- sql server2000查询分析器中如何将查询结果导出到Excel且带表头
- atitit.导出excel的设计----查询结果 导出为excel的实现java .net php 总结
- 将数据在[Excel]和[任何数据源]之间导入导出_可临时保存查询结果
- python查询mysql导出结果至Excel并发送邮件
- Navicat导出mysql的查询结果为Excel文档
- mysql 查询结果 导出 excel格式并解决乱码问题
- Mysql查询结果导出为Excel的几种方法
- Java导出数据库查询结果为excel和csv的对比
- MS SQL根据条件查询结果导出excel,Access
- atitit.导出excel的设计----查询结果 导出为excel的实现java .net php 总结
- Access 2007/2010 VBA查询结果导出为Excel 2007文件
- mysql 查询结果 导出 excel格式并解决乱码问题
- mysql 查询结果 导出 excel格式并解决乱码问题
- 用存储过程将查询结果导出到EXCEL
- java通过查询结果导出Excel
- 将数据库查询结果导出为Excel
- 将查询结果导出到Excel的另一个方法
- 从数据库中将SQL查询结果导出到Excel中中午乱码及数字问题解决
- mysql 查询结果 导出 excel格式并解决乱码问题
- 如何将SQL Developer中的查询结果导出为Excel表格