java excel读写组件jxl使用

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

  1. package com.yinbo.satisfy.web.struts;
  2. import javax.servlet.ServletOutputStream;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.struts.action.Action;
  6. import org.apache.struts.action.ActionForm;
  7. import org.apache.struts.action.ActionForward;
  8. import org.apache.struts.action.ActionMapping;
  9. //import com.lowagie.text.List;
  10. import java.util.List;
  11. import com.yinbo.satisfy.service.satisfy.SatisfyManage;
  12. import com.yinbo.satisfy.service.sysmanage.BranchManage;
  13. import com.yinbo.satisfy.service.sysmanage.ParamManage;
  14. import com.yinbo.satisfy.util.CommonUtils;
  15. import com.yinbo.satisfy.vo.Branch;
  16. import com.yinbo.satisfy.vo.Suggestion;
  17. import com.yinbo.satisfy.vo.SysParam;
  18. import java.io.*;
  19. import jxl.Workbook;
  20. import jxl.write.Label;
  21. import jxl.write.WritableSheet;
  22. import jxl.write.WritableWorkbook;
  23. import jxl.write.WriteException;
  24. public class QuestionAction extends Action {
  25.     private SatisfyManage satisfyManage;
  26.     private BranchManage branchManage;
  27.     private ParamManage paramManage;
  28.     
  29.     /** 
  30.      * Method execute
  31.      * @param mapping
  32.      * @param form
  33.      * @param request
  34.      * @param response
  35.      * @return ActionForward
  36.      */
  37.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  38.             HttpServletRequest request, HttpServletResponse response) {
  39.     
  40.         String excelFileName = "c://testaaa.xls";
  41.         
  42.          //////////////////////
  43.              String branchId = request.getParameter("branchId");
  44.         String level = request.getParameter("level");
  45.         List list = satisfyManage.getQuestionData(branchId, level);
  46.         String result[][] = new String[list.size()][5];
  47.         for(int i=0; i<list.size(); i++) {
  48.             Suggestion sug = (Suggestion)list.get(i);
  49.             result[i][0]=sug.getSugContent();
  50.             ///System.out.println(sug.getSugContent());
  51.         }
  52.         
  53.         //添加所在单位
  54.         Branch branch = branchManage.getBranchById(branchId);
  55.         String branchName = branch.getBranchName();
  56.         if(CommonUtils.isEmpty(branchName)) branchName = "所有机构";
  57.         
  58.         //添加级别
  59.         SysParam param = paramManage.getParam("3", level);
  60.         String paramName = param.getParamName();
  61.         if(CommonUtils.isEmpty(paramName)) paramName = "所有级别";
  62.         
  63.         try {
  64.             String strFileName = "question.xls";
  65.             strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8");//处理中文文件名的问题
  66.             strFileName = new String(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题
  67.             response.reset();
  68.             response.setCharacterEncoding("UTF-8");
  69.             response.setContentType("application/vnd.ms-excel");
  70.             response.setHeader("Content-Disposition","attachment; filename=" + strFileName);
  71.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  72.             
  73.           //创建可写入的Excel工作薄
  74.           //WritableWorkbook book = Workbook.createWorkbook(new File(excelFileName));
  75.             WritableWorkbook book = Workbook.createWorkbook(baos);
  76.           
  77.           // 写工作表名字
  78.           WritableSheet sheet = book.createSheet("XXX建议"0);
  79.           //此处可以设置列的宽度
  80.           int column = 0;
  81.           sheet.setColumnView(column++, 30);
  82.           sheet.setColumnView(column++, 30);
  83.           sheet.setColumnView(column++, 30);
  84.           sheet.setColumnView(column++, 30);
  85.           sheet.addCell(new Label(00"建议(" + branchName + ")(" + paramName + ")")); // 增加excel单元格
  86.           //sheet.addCell(new Label(1, 0, "部门名称")); // 增加excel单元格
  87.           //sheet.addCell(new Label(2, 0, "部门类型")); // 增加excel单元格
  88.           //sheet.addCell(new Label(3, 0, "综合成绩")); // 增加excel单元格
  89.           // result = getSelectResultWithStringArray(ds, sql);
  90.             int rows = result.length;
  91.             if (rows != 0) {
  92.                 int cols = result[0].length;
  93.                 for (int i = 0; i < rows; i++) {
  94.                     for (int j = 0; j < cols; j++) {
  95.                         sheet.addCell(new Label(j, i + 1, result[i][j]));
  96.                     }
  97.                 }
  98.             }
  99.           book.write(); // 写入byte输出流数组
  100.           book.close(); // jxl对象关闭
  101.           ServletOutputStream out = response.getOutputStream();
  102.         baos.writeTo(out);
  103.          out.flush();
  104.             
  105.         } catch (IOException e) {
  106.           e.printStackTrace();
  107.         } catch (WriteException e) {
  108.           e.printStackTrace();
  109.         }
  110.        
  111.         return null;
  112.     }
  113.     public SatisfyManage getSatisfyManage() {
  114.         return satisfyManage;
  115.     }
  116.     public void setSatisfyManage(SatisfyManage satisfyManage) {
  117.         this.satisfyManage = satisfyManage;
  118.     }
  119.     public BranchManage getBranchManage() {
  120.         return branchManage;
  121.     }
  122.     public void setBranchManage(BranchManage branchManage) {
  123.         this.branchManage = branchManage;
  124.     }
  125.     public ParamManage getParamManage() {
  126.         return paramManage;
  127.     }
  128.     public void setParamManage(ParamManage paramManage) {
  129.         this.paramManage = paramManage;
  130.     }
  131. }

你可能感兴趣的:(java excel读写组件jxl使用)