當幸福來敲門

          我就會牢牢抓住!
          隨筆 - 50, 文章 - 3, 評論 - 8, 引用 - 0
          數據加載中……

          Java方法導出Excel數據

          public ActionForward exportExcel(ActionMapping mapping,
             ActionForm form, HttpServletRequest request,
             HttpServletResponse response) {  
            String sfile = this.getServlet().getServletContext().getRealPath("/upload/")+ File.separator +"data.xls";// 服務器端名字
            String filename ="data.xls";// 客戶端名字
            OutputStream os = null;
            WritableWorkbook wwb = null;
            try {
             os = new FileOutputStream(savePath);
             wwb = Workbook.createWorkbook(os);//第一步,創建一個webbook,對應一個Excel文件
             WritableSheet ws = wwb.createSheet("statistics", 0); //第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
                 for (int i = 0; i < titleList.size(); i++) {
              String title = (String) titleList.get(i);
              Label titleLabel = new Label(i+1, 0, title);//從第二列   第一行 開始
              ws.addCell(titleLabel);
             }
             for (int i = 0; i < dataList.size(); i++) {
              Map  obj= (HashMap) dataList.get(i);
              String areaName = (String) obj.get("時間");//價格
              Label areaNameLabel = new Label(0, i+1, areaName);
              ws.addCell(areaNameLabel);//第一行的值
              for (int k = 0; k < str.length; k++) {
               if(db != null&& db.trim().equals("0")&&i>2){
                Label label = new Label(1+(k*3), i + 1, "xxx");
                ws.addCell(label);
                   label = new Label(2+(k*3), i + 1, "xxx");
                ws.addCell(label);
                label = new Label(3+(k*3), i + 1, "xxx");
                ws.addCell(label); 
               }else{
               String number = (String) obj.get(str[k][0] + "n");//數量
                     String sum = (String) obj.get(str[k][0] + "s");//金額
                     String cif = (String) obj.get(str[k][0] + "c");//價格
                Label label = new Label(1+(k*3), i + 1, number==null?"0":number);
               ws.addCell(label);
                  label = new Label(2+(k*3), i + 1, sum==null?"0":sum);
               ws.addCell(label);
               label = new Label(3+(k*3), i + 1, cif==null?"0":cif);
               ws.addCell(label);
               }
                  }
             }
             wwb.write();
            } catch (Exception ex) {
             ex.printStackTrace();
            } finally {
             try {
              wwb.close();
              os.close();
             } catch (Exception e) {
              e.printStackTrace();
             }
            }
          try {
              response.setHeader("Content-Disposition", "attachment;filename="
               + filename);
             response.setContentType("application/vnd.ms-excel");
             BufferedOutputStream out = new BufferedOutputStream(
               new DataOutputStream(response.getOutputStream()));
             BufferedInputStream in = new BufferedInputStream(
               new FileInputStream(sfile));
             byte[] b = new byte[in.available()];
             in.read(b);
             out.write(b);
             out.close();
             in.close();
             } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }


          }

          posted on 2011-12-30 13:45 wyx 閱讀(178) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 阳山县| 利津县| 岳池县| 读书| 岗巴县| 灵山县| 台湾省| 卫辉市| 嘉峪关市| 兴化市| 德格县| 宾阳县| 库尔勒市| 香港| 洛南县| 拉萨市| 昌宁县| 富源县| 英德市| 东平县| 承德市| 凤山县| 新昌县| 宁晋县| 高陵县| 上饶市| 盈江县| 宜丰县| 安龙县| 普陀区| 弥勒县| 井冈山市| 昌都县| 汾阳市| 六安市| 筠连县| 兴宁市| 思南县| 宜兰县| 武功县| 富平县|