Java產生Excel

來源:互聯網
上載者:User

public class SimpleExcelWrite {
 public void createExcel(OutputStream os) throws WriteException,IOException{
//  建立工作薄
  WritableWorkbook workbook=Workbook.createWorkbook(os);
//  建立新的一頁
  WritableSheet sheet=workbook.createSheet("First Sheet", 0);
//  建立要顯示的具體內容
  Label xuexiao=new Label(0,0,"學校");
  sheet.addCell(xuexiao);
  Label zhuanye=new Label(1,0,"專業");
  sheet.addCell(zhuanye);
  Label jingzhengli=new Label(2,0,"競爭力");
  sheet.addCell(jingzhengli);
  
  Label qinghua=new Label(0,1,"清華大學");
  sheet.addCell(qinghua);
  Label jisuanji=new Label(1,1,"電腦專業");
  sheet.addCell(jisuanji);
  Label gao=new Label(2,1,"高");
  sheet.addCell(gao);
  
  Label beida=new Label(0,2,"北京大學");
  sheet.addCell(beida);
  Label falv=new Label(1,2,"法律專業");
  sheet.addCell(falv);
  Label zhong=new Label(2,2,"中");
  sheet.addCell(zhong);
  
  Label ligong=new Label(0,3,"北京理工大學");
  sheet.addCell(ligong);
  Label hangkong=new Label(1,3,"航空專業");
  sheet.addCell(hangkong);
  Label di=new Label(2,3,"低");
  sheet.addCell(di);
  
//  把建立的內容寫入到輸出資料流中,並關閉輸出資料流
  workbook.write();
  workbook.close();
  os.close();
  
 }
}

<%
 String fname="學校專業競爭力情況";
 OutputStream os=response.getOutputStream();
 response.reset();
 
 //下面是對中文檔案名稱的處理
 response.setCharacterEncoding("UTF-8");//設定相應內容的編碼格式
 fname=java.net.URLEncoder.encode(fname,"UTF-8");
 response.setHeader("Content-Disposition","attachment;filename="+ new String(fname.getBytes("UTF-8"),"GBK")+".xls");
 response.setContentType("application/msexel");//定義輸出類型
 SimpleExcelWrite sw=new SimpleExcelWrite();
 sw.createExcel(os);
 %>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.