java學習

          java學習

           

          jxl導出excel表格


          先要導入jxl需要的jar包,然后在觸發導出excel時傳入集合對象,例如:
          public class ToExcel {

              public static void excel(List<WenShi> l,HttpServletResponse response) {
                  ServletOutputStream out = null;
                  WritableWorkbook wwb = null;
                  try {
                      // 導出Excel路徑
                      Date d = new Date();
                      String s = new SimpleDateFormat("yyyyMMddHHmmss").format(d);
                      
                      response.setCharacterEncoding("utf-8");
                        response.reset();
                        response.setContentType("application/vnd.ms-excel;charset=utf-8");
                        response.setHeader("Content-Disposition", "attachment;filename="
                                + new String("歷史數據.xls".getBytes(),"iso-8859-1"));
                         out = response.getOutputStream();
                      
                      WritableSheet ws = null;
                      wwb = Workbook.createWorkbook(out);
                      ws = wwb.createSheet("sheet1", 0);
                      // 文字樣式
                      WritableFont wf = new WritableFont(WritableFont.TIMES, 10,
                              WritableFont.BOLD, false);
                      WritableCellFormat wcff = new WritableCellFormat(wf);
                      // 標題
                      // 第一列第1行(0,0)
                      Label label1 = new Label(0, 0, "編號", wcff);
                      // 第一列第2行(0,1)
                      Label label2 = new Label(1, 0, "環境溫度", wcff);
                      // 第一列第3行(0,2)
                      Label label3 = new Label(2, 0, "環境濕度", wcff);
                      Label label4 = new Label(3, 0, "光照強度", wcff);
                      Label label5 = new Label(4, 0, "土壤溫度", wcff);
                      Label label6 = new Label(5, 0, "土壤濕度", wcff);
                      Label label7 = new Label(6, 0, "co2濃度", wcff);
                      // 第一列第4行(0,3)
                      Label label8 = new Label(7, 0, "時間", wcff);
                      // 第一列第5行(0,4)
                      Label label9 = new Label(8, 0, "星期幾", wcff);
                      ws.addCell(label1);
                      ws.addCell(label2);
                      ws.addCell(label3);
                      ws.addCell(label4);
                      ws.addCell(label5);
                      ws.addCell(label6);
                      ws.addCell(label7);
                      ws.addCell(label8);
                      ws.addCell(label9);
                      for (int i = 0; i < l.size(); i++) {
                          Label l1 = new Label(0, i + 1, l.get(i).getStr("node"));
                          Label l2 = new Label(1, i + 1, l.get(i).getStr("wen"));
                          Label l3 = new Label(2, i + 1, l.get(i).getStr("shi"));
                          Label l4 = new Label(3, i + 1, l.get(i).getStr("sun"));
                          Label l5 = new Label(4, i + 1, l.get(i).getStr("tuwen"));
                          Label l6 = new Label(5, i + 1, l.get(i).getStr("tushi"));
                          Label l7 = new Label(6, i + 1, l.get(i).getStr("co"));
                          Label l8 = new Label(7, i + 1, l.get(i)
                                  .getTimestamp("timetext").toString());
                          Label l9 = new Label(8, i + 1, l.get(i).getStr("week"));
                          ws.addCell(l1);
                          ws.addCell(l2);
                          ws.addCell(l3);
                          ws.addCell(l4);
                          ws.addCell(l5);
                          ws.addCell(l6);
                          ws.addCell(l7);
                          ws.addCell(l8);
                          ws.addCell(l9);
                      }
                      wwb.write();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }finally{
                  
                      try {
                          wwb.close();
                      } catch (WriteException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                  
                      
                  }
              }

          }

          posted on 2013-03-19 17:05 楊軍威 閱讀(740) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阆中市| 凯里市| 定州市| 宜城市| 建宁县| 湘西| 岐山县| 延津县| 博客| 磐安县| 丹凤县| 康保县| 临沂市| 绵竹市| 红河县| 高陵县| 丹凤县| 青神县| 定陶县| 鄄城县| 九江市| 阜宁县| 宿州市| 滨州市| 常州市| 新宾| 西林县| 莒南县| 额敏县| 城口县| 颍上县| 工布江达县| 安国市| 巴南区| 柳林县| 永州市| 峨边| 根河市| 龙井市| 无棣县| 唐河县|