在EasyJWeb中使用Java Excel API 處理試算表

來源:互聯網
上載者:User
  在J2EE應用開發中,由於各種各樣的原因,經常會需要處理一些舊的Excel格式試算表資料,或者是產生試算表。  處理Excel試算表的方法比如多,比如可以使用jdbc來像讀資料庫中的資料一樣來讀取試算表的內容。這裡示範的是使用開源的試算表處理工具jxl,即Java Excel API來進行處理。關於jxl及相關使用,可以在網上搜尋到很多資料。這裡只是簡單示範在EasyJWeb中的使用方法,這是前段時間公司一個項目中的應用,需要產生一特定的Excel表格,供用戶端下載。  WEB MVC中Action中的代碼如下:public class PersonChangeAction extends BaseCrudAction {private ExcelReportService excelReportService=ExcelReportService.getInstance();public Page doDownloadMenu(WebForm form, Module module)throws Exception {  
String town=CommUtil.null2String(form.get("queryTown"));
  String village=CommUtil.null2String(form.get("queryVillage"));
  String team=CommUtil.null2String(form.get("queryTeam"));
  String belongDept="";
  ActiveUser user=(ActiveUser)this.getCurrentUser(form);  
  if(!userService.checkAdmin(user))
  {
    belongDept=user.getDept();
  }
    ActionContext.getContext().getResponse().setContentType("application/x-msdownload");
  String fileName="XXXXX人員名單";
  if(!"".equals(town))fileName+="-"+town;
  if(!"".equals(village))fileName+="-"+village;
  if(!"".equals(team))fileName+="-"+team;
  fileName+=".xls";
  ActionContext.getContext().getResponse().setHeader("Content-Disposition","attachment; filename=/""+new String(fileName.getBytes("gbk"),"iso8859-1")+"/"");
  java.io.OutputStream out=com.easyjf.web.ActionContext.getContext().getResponse().getOutputStream();
  excelReportService.personChangeExcelList(out, belongDept, town, village, team);
  return null;
 }..} Action中的doDownloadMenu對應downloadMenu命令,這段代碼注要實現了向用戶端返回二進位流檔案的功能,注意一些細節。其中流中的內容,即Excel表格的內容,通過ExcelReportService的personChangeExcelList方法來處理,該方法屬於商務邏輯層,主要功能是把合格對象,添加到Excel表格中,這個方法內容大致如下: public synchronized void personChangeExcelList(java.io.OutputStream out,String belongDept,String town,String village,String team)
 { 
  String scope = "1=1"; 
  String scope1="1=1";
  Collection paras = new ArrayList();   
 //省略部分查詢代碼  if(!"1=1".equals(scope))scope1+=" and personId in (select cid as personId from person where "+scope+") ";
  DbPageList pList = new DbPageList(PersonChange.class, scope1, paras);
  pList.doList(1, 500);
  try{
  jxl.write.WritableWorkbook book=jxl.Workbook.createWorkbook(out);
  jxl.write.WritableSheet sheet=book.createSheet("XXXX人員名單", 0);
  int pages=pList.getPages();
  String[] lables={"姓名","鎮(鄉、街道)","村(居委)","組","戶主","就業方式","就業轉移時間","就業轉移地點","就業單位","就業工資","就業工種","聯絡電話"};
  for(int l=0;l<lables.length;l++)
   sheet.addCell(new jxl.write.Label(l,0,lables[l]));
  int row=1;  
  for(int p=1;p<=pages;p++)
  {
   pList = new DbPageList(PersonChange.class, scope1, paras);
   pList.doList(p, 500);
   List list=pList.getResult();   
   for(int i=0;i<list.size();i++)
   {
   PersonChange change=(PersonChange)list.get(i);  
   Person person=(Person)this.dao.get(Person.class,change.getPersonId()); 
   sheet.addCell(new jxl.write.Label(0,row,person.getTrueName()));
   sheet.addCell(new jxl.write.Label(1,row,person.getTown()));
   sheet.addCell(new jxl.write.Label(2,row,person.getVillage()));
   sheet.addCell(new jxl.write.Label(3,row,person.getTeam()));
   sheet.addCell(new jxl.write.Label(4,row,person.getHouseholderName()));
   sheet.addCell(new jxl.write.Label(5,row,logic.showTitle("getWorkType", change.getWorkType())));
   sheet.addCell(new jxl.write.Label(6,row,CommUtil.format(change.getTransferTime())));
   String workPlace=logic.showTitle("workplace", change.getWorkPlace());
   if("".equals(workPlace))workPlace= change.getWorkPlace();
   sheet.addCell(new jxl.write.Label(7,row,workPlace));
   sheet.addCell(new jxl.write.Label(8,row,change.getWorkDept()));
   sheet.addCell(new jxl.write.Label(9,row,logic.showTitle("workSalary", change.getWorkSalary())));
   sheet.addCell(new jxl.write.Label(10,row,logic.showTitle("skills", change.getWorkSubject())));
   sheet.addCell(new jxl.write.Label(11,row,change.getTel()));   
   row++;
   }   
  }  
  book.write();
  book.close();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
 }  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.