Java导出Excel表

最近写的一个小程序:

需求:遍历数据库的数据,然后生成Excel表 ,导出数据(利用jxl.jar包)

有兴趣的同学可以参考一下(其他同事介绍使用poi.jar,但没试过,听说比jxl.jar好)

  1. 生成excel表
  2. @RequestMapping(value="/xixi/xuan/createExcel",method={RequestMethod.GET,RequestMethod.POST})
  3. publicStringcreateExcel(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException,RowsExceededException,WriteException{
  4. OutputStreamos=response.getOutputStream();
  5. ParamUtilpu=newParamUtil(request);
  6. Stringqid=pu.getString("qid");
  7. StringfileName="VisitorData";
  8. response.setContentType("application/vnd.ms-excel");
  9. response.addHeader("Content-Disposition","attachment;filename=\""+fileName+".xls"+"\"");
  10. List<QuserInfoRecord>list=quserInfoRecordService.findQuserInfoRecord(qid);
  11. if(list.size()>0){
  12. /*StringexcelPath="D:\\data.xls";
  13. FileexcelFile=newFile(excelPath);
  14. if(!excelFile.exists()){
  15. excelFile.createNewFile();
  16. }*/
  17. System.out.println("=====kaisi");
  18. ///打开文件
  19. WritableWorkbookbook=Workbook.createWorkbook(os);
  20. ///生成一个名为"用户信息"的工作表
  21. WritableSheetws=book.createSheet(fileName,0);//新建一个sheet
  22. //在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
  23. //以及单元格内容为test
  24. //Labellabel=newLabel(0,0,"test");
  25. //往表中添加内容
  26. //将定义好的单元格添加到工作表中
  27. JSONArrayjsonArray=JSONArray.fromObject(list.get(0).getExt());
  28. intlenght=jsonArray.size();
  29. WritableCellFormatwcf=null;
  30. //字体样式
  31. WritableFontwf=newWritableFont(WritableFont.TIMES,12,WritableFont.BOLD,false);//最后一个为是否italic
  32. wf.setColour(Colour.BLACK);
  33. wcf=newWritableCellFormat(wf);
  34. //对齐方式
  35. wcf.setAlignment(Alignment.CENTRE);
  36. wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
  37. //边框
  38. wcf.setBorder(Border.ALL,BorderLineStyle.THIN);
  39. for(inti=0;i<list.size();i++){
  40. JSONArrayjsonlist=JSONArray.fromObject(list.get(i).getExt());
  41. Labellabel11=newLabel(0,0,"日期",wcf);
  42. ws.setColumnView(0,20);
  43. ws.addCell(label11);
  44. Labellabel1=newLabel(0,i+1,list.get(i).getDate());
  45. ws.addCell(label1);
  46. Labellabel12=newLabel(1,0,"来源标题",wcf);
  47. ws.setColumnView(1,30);
  48. ws.addCell(label12);
  49. Labellabel2=newLabel(1,i+1,list.get(i).getSourcetitle());
  50. ws.addCell(label2);
  51. Labellabel13=newLabel(2,0,"页面链接",wcf);
  52. ws.setColumnView(2,50);
  53. ws.addCell(label13);
  54. Labellabel3=newLabel(2,i+1,list.get(i).getUserhref());
  55. ws.addCell(label3);
  56. Labellabel14=newLabel(3,0,"IP来源",wcf);
  57. ws.setColumnView(3,20);
  58. ws.addCell(label14);
  59. Labellabel4=newLabel(3,i+1,list.get(i).getIp());
  60. ws.addCell(label4);
  61. for(intj=0;j<lenght;j++){
  62. JSONObjectjsonObject=JSONObject.fromObject(jsonlist.getString(j));
  63. Labellabel15=newLabel(j+4,0,jsonObject.getString("name"),wcf);
  64. ws.setColumnView(j+4,20);
  65. ws.addCell(label15);
  66. Labellabel=newLabel(j+4,i+1,jsonObject.getString("value"));
  67. ws.addCell(label);
  68. }
  69. }
  70. //写入数据并关闭文件
  71. //将数据写入所建的excel
  72. book.write();
  73. book.close();
  74. os.close();
  75. response.flushBuffer();
  76. }
  77. returnnull;
  78. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值