java excel读写组件jxl使用

使用起来很简单,就不多说了,只把代码贴出来

  1. packagecom.yinbo.satisfy.web.struts;
  2. importjavax.servlet.ServletOutputStream;
  3. importjavax.servlet.http.HttpServletRequest;
  4. importjavax.servlet.http.HttpServletResponse;
  5. importorg.apache.struts.action.Action;
  6. importorg.apache.struts.action.ActionForm;
  7. importorg.apache.struts.action.ActionForward;
  8. importorg.apache.struts.action.ActionMapping;
  9. //importcom.lowagie.text.List;
  10. importjava.util.List;
  11. importcom.yinbo.satisfy.service.satisfy.SatisfyManage;
  12. importcom.yinbo.satisfy.service.sysmanage.BranchManage;
  13. importcom.yinbo.satisfy.service.sysmanage.ParamManage;
  14. importcom.yinbo.satisfy.util.CommonUtils;
  15. importcom.yinbo.satisfy.vo.Branch;
  16. importcom.yinbo.satisfy.vo.Suggestion;
  17. importcom.yinbo.satisfy.vo.SysParam;
  18. importjava.io.*;
  19. importjxl.Workbook;
  20. importjxl.write.Label;
  21. importjxl.write.WritableSheet;
  22. importjxl.write.WritableWorkbook;
  23. importjxl.write.WriteException;
  24. publicclassQuestionActionextendsAction{
  25. privateSatisfyManagesatisfyManage;
  26. privateBranchManagebranchManage;
  27. privateParamManageparamManage;
  28. /**
  29. *Methodexecute
  30. *@parammapping
  31. *@paramform
  32. *@paramrequest
  33. *@paramresponse
  34. *@returnActionForward
  35. */
  36. publicActionForwardexecute(ActionMappingmapping,ActionFormform,
  37. HttpServletRequestrequest,HttpServletResponseresponse){
  38. StringexcelFileName="c:\\testaaa.xls";
  39. //////////////////////
  40. StringbranchId=request.getParameter("branchId");
  41. Stringlevel=request.getParameter("level");
  42. Listlist=satisfyManage.getQuestionData(branchId,level);
  43. Stringresult[][]=newString[list.size()][5];
  44. for(inti=0;i<list.size();i++){
  45. Suggestionsug=(Suggestion)list.get(i);
  46. result[i][0]=sug.getSugContent();
  47. ///System.out.println(sug.getSugContent());
  48. }
  49. //添加所在单位
  50. Branchbranch=branchManage.getBranchById(branchId);
  51. StringbranchName=branch.getBranchName();
  52. if(CommonUtils.isEmpty(branchName))branchName="所有机构";
  53. //添加级别
  54. SysParamparam=paramManage.getParam("3",level);
  55. StringparamName=param.getParamName();
  56. if(CommonUtils.isEmpty(paramName))paramName="所有级别";
  57. try{
  58. StringstrFileName="question.xls";
  59. strFileName=java.net.URLEncoder.encode(strFileName,"UTF-8");//处理中文文件名的问题
  60. strFileName=newString(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题
  61. response.reset();
  62. response.setCharacterEncoding("UTF-8");
  63. response.setContentType("application/vnd.ms-excel");
  64. response.setHeader("Content-Disposition","attachment;filename="+strFileName);
  65. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  66. //创建可写入的Excel工作薄
  67. //WritableWorkbookbook=Workbook.createWorkbook(newFile(excelFileName));
  68. WritableWorkbookbook=Workbook.createWorkbook(baos);
  69. //写工作表名字
  70. WritableSheetsheet=book.createSheet("XXX建议",0);
  71. //此处可以设置列的宽度
  72. intcolumn=0;
  73. sheet.setColumnView(column++,30);
  74. sheet.setColumnView(column++,30);
  75. sheet.setColumnView(column++,30);
  76. sheet.setColumnView(column++,30);
  77. sheet.addCell(newLabel(0,0,"建议("+branchName+")("+paramName+")"));//增加excel单元格
  78. //sheet.addCell(newLabel(1,0,"部门名称"));//增加excel单元格
  79. //sheet.addCell(newLabel(2,0,"部门类型"));//增加excel单元格
  80. //sheet.addCell(newLabel(3,0,"综合成绩"));//增加excel单元格
  81. //result=getSelectResultWithStringArray(ds,sql);
  82. introws=result.length;
  83. if(rows!=0){
  84. intcols=result[0].length;
  85. for(inti=0;i<rows;i++){
  86. for(intj=0;j<cols;j++){
  87. sheet.addCell(newLabel(j,i+1,result[i][j]));
  88. }
  89. }
  90. }
  91. book.write();//写入byte输出流数组
  92. book.close();//jxl对象关闭
  93. ServletOutputStreamout=response.getOutputStream();
  94. baos.writeTo(out);
  95. out.flush();
  96. }catch(IOExceptione){
  97. e.printStackTrace();
  98. }catch(WriteExceptione){
  99. e.printStackTrace();
  100. }
  101. returnnull;
  102. }
  103. publicSatisfyManagegetSatisfyManage(){
  104. returnsatisfyManage;
  105. }
  106. publicvoidsetSatisfyManage(SatisfyManagesatisfyManage){
  107. this.satisfyManage=satisfyManage;
  108. }
  109. publicBranchManagegetBranchManage(){
  110. returnbranchManage;
  111. }
  112. publicvoidsetBranchManage(BranchManagebranchManage){
  113. this.branchManage=branchManage;
  114. }
  115. publicParamManagegetParamManage(){
  116. returnparamManage;
  117. }
  118. publicvoidsetParamManage(ParamManageparamManage){
  119. this.paramManage=paramManage;
  120. }
  121. }

你可能感兴趣的:(java,apache,struts,servlet,Excel)