我的漫漫程序之旅

          專注于JavaWeb開發
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數據加載中……

          jexcelapi使用小記

          jexcelapi是一個開源項目,主要用來操作excel.
          主頁地址:
          http://www.andykhan.com/jexcelapi/

          現在做一個項目用到了它,根據不同的公司生成不同的文件夾,
          在相應的文件夾下生成對應的xls. 這里只帖出生成xls部分核心代碼:
              public void generateXls()
              
          {
                  
          try
                  
          {
                      
          /** **********創建工作簿************ */
                      WritableWorkbook workbook 
          = Workbook.createWorkbook(new File("d:/test.xls"));
                      
          /** **********創建工作表************ */
                      WritableSheet sheet 
          = workbook.createSheet("工作表名稱"0);

                      
          /** *********設置列寬**************** */
                      sheet.setColumnView(
          015); // 第1列
                      sheet.setColumnView(118); // 第2列
                      sheet.setColumnView(213);
                      sheet.setColumnView(
          313);
                      sheet.setColumnView(
          415);
                      sheet.setColumnView(
          515);
                      
          //設置行高
                      sheet.setRowView(0600false);
                      sheet.setRowView(
          1400false);
                      sheet.setRowView(
          7400false);
                      
          //設置頁邊距
                      sheet.getSettings().setRightMargin(0.5);
                      
          //設置頁腳
                      sheet.setFooter("""""測試頁腳");
                      
          /** ************設置單元格字體************** */
                      
          //字體
                      WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
                      WritableFont BoldFont 
          = new WritableFont(WritableFont.ARIAL, 14,
                              WritableFont.BOLD);
                      WritableFont tableFont 
          = new WritableFont(WritableFont.ARIAL, 12,
                              WritableFont.NO_BOLD);
                      WritableFont baodanFont 
          = new WritableFont(WritableFont.ARIAL, 10,
                              WritableFont.BOLD);

                      
          /** ************以下設置幾種格式的單元格************ */
                      
          // 用于標題
                      WritableCellFormat wcf_title = new WritableCellFormat(BoldFont);
                      wcf_title.setBorder(Border.NONE, BorderLineStyle.THIN); 
          // 線條
                      wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_title.setAlignment(Alignment.CENTRE); // 水平對齊
                      wcf_title.setWrap(true); // 是否換行

                      
          // 用于表格標題
                      WritableCellFormat wcf_tabletitle = new WritableCellFormat(
                              tableFont);
                      wcf_tabletitle.setBorder(Border.NONE, BorderLineStyle.THIN); 
          // 線條
                      wcf_tabletitle.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_tabletitle.setAlignment(Alignment.CENTRE); // 水平對齊
                      wcf_tabletitle.setWrap(true); // 是否換行

                      
          // 用于正文左
                      WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
                      wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); 
          // 線條
                      wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_left.setAlignment(Alignment.LEFT);
                      wcf_left.setWrap(
          true); // 是否換行

                      
          // 用于正文左
                      WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
                      wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); 
          // 線條
                      wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_center.setAlignment(Alignment.CENTRE);
                      wcf_center.setWrap(
          true); // 是否換行

                      
          // 用于正文右
                      WritableCellFormat wcf_right = new WritableCellFormat(NormalFont);
                      wcf_right.setBorder(Border.ALL, BorderLineStyle.THIN); 
          // 線條
                      wcf_right.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_right.setAlignment(Alignment.RIGHT);
                      wcf_right.setWrap(
          false); // 是否換行

                      
          // 用于跨行
                      WritableCellFormat wcf_merge = new WritableCellFormat(NormalFont);
                      wcf_merge.setBorder(Border.ALL, BorderLineStyle.THIN); 
          // 線條
                      wcf_merge.setVerticalAlignment(VerticalAlignment.TOP); // 垂直對齊
                      wcf_merge.setAlignment(Alignment.LEFT);
                      wcf_merge.setWrap(
          true); // 是否換行

                      WritableCellFormat wcf_table 
          = new WritableCellFormat(NormalFont);
                      wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN); 
          // 線條
                      wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對齊
                      wcf_table.setAlignment(Alignment.CENTRE);
                      wcf_table.setBackground(Colour.GRAY_25);
                      wcf_table.setWrap(
          true); // 是否換行

                      
          /** ************單元格格式設置完成****************** */
                      
          //合并單元格,注意mergeCells(col0,row0,col1,row1) --列從0開始,col1為你要合并到第幾列,行也一樣
                      sheet.mergeCells(0050);

                      sheet.addCell(
          new Label(00"這里是大標題,自定義格式",
                              wcf_title));

                      sheet.mergeCells(
          0111);
                      sheet.mergeCells(
          2151);

                      sheet.addCell(
          new Label(01"", wcf_center));
                      sheet.addCell(
          new Label(21"姓名:" + "supercrsky",
                              wcf_center));

                      sheet.mergeCells(
          0212);
                      sheet.mergeCells(
          2232);

                      sheet.addCell(
          new Label(02"單位:", wcf_center));
                      sheet.addCell(
          new Label(22"ChinaLong", wcf_center));
                      sheet.addCell(
          new Label(42"薪水", wcf_center));
                      sheet.addCell(
          new Label(52"5000", wcf_center));

                      sheet.mergeCells(
          0313);
                      sheet.mergeCells(
          2333);

                      sheet.addCell(
          new Label(03"性別:", wcf_center));
                      sheet.addCell(
          new Label(23"", wcf_center));
                      sheet.addCell(
          new Label(43"婚否:", wcf_center));
                      sheet.addCell(
          new Label(53"", wcf_center));

                      sheet.mergeCells(
          0414);
                      sheet.mergeCells(
          2434);

                      sheet.addCell(
          new Label(04"是否在職:", wcf_center));
                      sheet.addCell(
          new Label(24,"",
                              wcf_center));
                      sheet.addCell(
          new Label(44,"工作經驗:", wcf_center));
                      sheet.addCell(
          new Label(54"4",wcf_center));

                      sheet.mergeCells(
          0515);
                      sheet.mergeCells(
          2535);

                      sheet.addCell(
          new Label(05"保險費:", wcf_center));
                      sheet.addCell(
          new Label(25,"50",
                              wcf_center));
                      sheet.addCell(
          new Label(45"保險金額:", wcf_center));
                      sheet.addCell(
          new Label(55"50000",
                              wcf_center));

                      sheet.mergeCells(
          0616);
                      sheet.mergeCells(
          2636);

                      sheet.addCell(
          new Label(06"工作地點:", wcf_center));
                      sheet.addCell(
          new Label(26"北京", wcf_center));
                      sheet.addCell(
          new Label(46"開心度:", wcf_center));
                      sheet.addCell(
          new Label(56"一般", wcf_center));

                      
          // 另起一table
                      sheet.mergeCells(0757);
                      sheet.addCell(
          new Label(07"詳細數據", wcf_tabletitle));
                      
          // table標題
                      sheet.addCell(new Label(08"序號", wcf_table));
                      sheet.addCell(
          new Label(18"姓名", wcf_table));
                      sheet.addCell(
          new Label(28"年齡", wcf_table));
                      sheet.addCell(
          new Label(38"性別", wcf_table));
                      sheet.addCell(
          new Label(48"婚否", wcf_table));
                      sheet.addCell(
          new Label(58"在職", wcf_table));
                      
          // table內容
                      
          //這里用你的dao
                      TestDAO dao = new TestDAO();
                      List list 
          = dao.findBy(user.getUserId());
                      System.out.println(
          "此保單擁有防疫碼數量:" + list.size());
                      
          for (int i = 0; i < list.size(); i++)
                      
          {
                          
          //對應你的vo類
                          User data = (User) list.get(i);

                          sheet.addCell(
          new Label(09 + i, String.valueOf(i + 1),
                                  wcf_center));
                          sheet.addCell(
          new Label(19 + i, data.getDlEPCode(),
                                  wcf_center));
                          sheet
                                  .addCell(
          new Label(29 + i, data.getDlType(),
                                          wcf_center));
                          sheet.addCell(
          new Label(39 + i, String.valueOf(data
                                  .getDlPigAge()), wcf_center));
                          sheet.addCell(
          new Label(49 + i, "", wcf_center));
                          sheet.addCell(
          new Label(59 + i, "", wcf_center));
                      }

                      
          /** **********以上所寫的內容都是寫在緩存中的,下一句將緩存的內容寫到文件中******** */
                      workbook.write();
                      
          /** *********關閉文件************* */
                      workbook.close();
                      System.out.println(
          "導出成功");
                      
          // 存放url地址
                  }
           catch (Exception e)
                  
          {
                      System.out.println(
          "在輸出到EXCEL的過程中出現錯誤,錯誤原因:" + e.toString());
                  }

              }

          完整源碼可以在這里下載

          posted on 2008-05-21 08:59 々上善若水々 閱讀(7968) 評論(2)  編輯  收藏

          評論

          # re: jexcelapi使用小記  回復  更多評論   

          用過,跨平臺,無需裝office就可以操作,算是方便吧。
          2008-05-21 09:13 | Matthew Chen

          # re: jexcelapi使用小記  回復  更多評論   

          謝謝,正是需要的 ^_^
          2008-06-28 21:48 | smf

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


          網站導航:
           
          主站蜘蛛池模板: 阿拉尔市| 福建省| 锡林郭勒盟| 和平区| 寿宁县| 大姚县| 铁岭市| 南澳县| 柘荣县| 穆棱市| 连平县| 巴楚县| 石泉县| 冀州市| 舒城县| 枣庄市| 雅安市| 定州市| 洛阳市| 尖扎县| 景洪市| 永清县| 盖州市| 神农架林区| 九台市| 修水县| 察隅县| 察雅县| 富蕴县| 太原市| 南丰县| 平乐县| 同仁县| 大足县| 应用必备| 鄂托克前旗| 山阴县| 丰镇市| 五原县| 临漳县| 阿鲁科尔沁旗|