你的位置:首页 > Java教程

[Java教程]java下载excel


public void doExport_asRunLog(){ 

      try { 

            String date = getParameter("date"); 

            String title[] = { "雨", "田" }; 

            JSONArray list = new JSONArray(); 

            List<String> objList = new ArrayList<String>(); 

            List<String> objList1 = new ArrayList<String>(); 

            List<String> objList2 = new ArrayList<String>(); 

            List<String> objList3 = new ArrayList<String>();  

            objList.add("one"); 

            objList.add("two"); 

            list.add(objList); 

            objList1.add("1"); 

            objList1.add("2"); 

            list.add(objList1); 

            objList2.add("3"); 

            objList2.add("4"); 

            list.add(objList2); 

            objList3.add("5");  

            objList3.add("6"); 

            list.add(objList3); 

            // 定义输出流,以便打开保存对话框______________________begin  

            OutputStream out = getResponse().getOutputStream();// 取得输出流 response.reset();// 清空输出流 

            // 设定输出文件头 

            getResponse().setContentType("application/msexcel");// 定义输出类型 

//          getResponse().setContentType("application/x-download;charset=UTF-8"); 

            getResponse().setHeader("Content-Disposition","attachment;filename=AsRunLog"+date+".xls"); 

            // 定义输出流,以便打开保存对话框_______________________end 

            as.export_excel(title, list, out); 

        } catch (Exception e) { 

            e.printStackTrace(); 

        } 

   }

 

 

/**

     * 导出excel

     * @param Title

     * @param list

     * @param out

     * @return

     */ 

    public int export_excel(String[] Title, List<List<Object>> list,OutputStream out){ 

        int res = 0; 

          // 以下开始输出到EXCEL 

          try {     

           /** **********创建工作簿************ */ 

           WritableWorkbook workbook = Workbook.createWorkbook(out); 

 

           /** **********创建工作表************ */ 

           WritableSheet sheet = workbook.createSheet("Sheet1", 0); 

 

           /** **********设置纵横打印(默认为纵打)、打印纸***************** */ 

           jxl.SheetSettings sheetset = sheet.getSettings(); 

           sheetset.setProtected(false); 

 

           /** ************设置单元格字体************** */ 

           WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10); 

           WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD); 

 

           /** ************以下设置三种单元格样式,灵活备用************ */ 

           // 用于标题居中 

           WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); 

           wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条 

           wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐 

           wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐 

           wcf_center.setWrap(false); // 文字是否换行 

            

           // 用于正文居左 

           WritableCellFormat wcf_left = new WritableCellFormat(NormalFont,NumberFormats.INTEGER); 

           wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条 

           wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐 

           wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐 

           wcf_left.setWrap(false); // 文字是否换行    

           sheet.getSettings().setDefaultColumnWidth(20); // 设置列的默认宽度 

           /** ***************以下是EXCEL开头大标题,暂时省略********************* */ 

           //sheet.mergeCells(0, 0, colWidth, 0); 

           //sheet.addCell(new Label(0, 0, "XX报表", wcf_center)); 

           /** ***************以下是EXCEL第一行列标题********************* */ 

           for (int i = 0; i < Title.length; i++) { 

            sheet.addCell(new Label(i, 0,Title[i],wcf_center)); 

           }    

           /** ***************以下是EXCEL正文数据********************* */ 

           for (int i = 0; i < list.size(); i++) { 

               List<Object> objList = list.get(i); 

               for (int j = 0; j < Title.length; j++) { 

                   Object obj = objList.get(j); 

                   if (obj instanceof Integer) { 

                       sheet.addCell(new jxl.write.Number(j, i+1,new Integer(objList.get(j).toString()).intValue(),wcf_left)); 

                   }else if(obj instanceof Double ){ 

                       sheet.addCell(new jxl.write.Number(j, i+1,new Double(objList.get(j).toString()).doubleValue(),wcf_left)); 

                   }else if(obj instanceof Float){ 

                       sheet.addCell(new jxl.write.Number(j, i+1,new Double(objList.get(j).toString()).floatValue(),wcf_left)); 

                   }else{ 

                       sheet.addCell(new Label(j, i+1,new String(objList.get(j).toString()),wcf_left)); 

                   } 

               } 

           } 

           /** **********将以上缓存中的内容写到EXCEL文件中******** */ 

           workbook.write(); 

           /** *********关闭文件************* */ 

           workbook.close();    

          } catch (Exception e) { 

              res = -1; 

              e.printStackTrace(); 

          } 

        return res; 

    }