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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 阿巴嘎旗| 贺州市| 论坛| 富宁县| 铅山县| 高尔夫| 乐安县| 古田县| 惠州市| 进贤县| 利川市| 平湖市| 巩义市| 永川市| 江孜县| 昌吉市| 合水县| 武定县| 道孚县| 繁峙县| 长治县| 新乐市| 土默特左旗| 吴江市| 周宁县| 兴隆县| 沙雅县| 嘉鱼县| 任丘市| 孟津县| 尉犁县| 宿松县| 米泉市| 宝山区| 平定县| 泰兴市| 文登市| 蓝山县| 邻水| 庄浪县| 虎林市|