Java匯出Excel

來源:互聯網
上載者:User

標籤:vertica   ret   close   catch   建立   wcf   number   工作表   null   

--建立Excel檔案

 1 /** 2  * 匯出表格 3  * @author gongyg 4  * @date 2017-4-7 5  */ 6 public class ExcelCreate{ 7      8     public static void createExcel() { 9         String url = null;10         try11         {12             //建立表格檔案13             String filePath = "/CIRCExcel/"+String.format("%s", new SimpleDateFormat("yyyy/MM/dd").format(new Date()))+"/";14             File path = new File(rootPath+filePath);15             if(!path.exists() && !path.isDirectory())16             {17                 path.mkdirs();18             }19             String fileName =String.format("%s%s.xls", new SimpleDateFormat("yyyy年MM月dd日HH時mm分").format(new Date()), Global.getInstance().UUID());20             File file = new File(path,fileName);21             if(!file.exists())22             {23                 file.createNewFile();24             }25             //建立工作薄26             WritableWorkbook workbook =  Workbook.createWorkbook(file);27             WritableSheet sheet = workbook.createSheet("第1頁", 0);28             29             /**30              * 定義儲存格樣式31              */32             WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,33                     jxl.format.Colour.BLACK);   //字型--大小--粗體--斜體--底線--顏色34             WritableCellFormat wcf_head = new WritableCellFormat(wf_head);//定義儲存格35             wcf_head.setBackground(jxl.format.Colour.WHITE);   //設定儲存格背景色36             wcf_head.setAlignment(jxl.format.Alignment.CENTRE);    //設定對齊37             wcf_head.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //設定垂直置中38             wcf_head.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);    //設定邊框39             40             WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,41                     jxl.format.Colour.BLACK);   //字型--大小--粗體--斜體--底線--顏色42             WritableCellFormat wcf_table = new WritableCellFormat(wf_table);//定義儲存格43             wcf_table.setBackground(jxl.format.Colour.WHITE);   //設定儲存格背景色44             wcf_table.setAlignment(jxl.format.Alignment.CENTRE);    //設定對齊45             wcf_table.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK);    //設定邊框46             //設定列寬47                 for(int i=0; i<18; i++) 48                 {49                     sheet.setColumnView(i, 8);50                 }51             //在Label對象的構造子中指名儲存格位置是第一列第一行(0,0) 52             //以及儲存格內容為test 53             Label title=new Label(0,0,"統計",wcf_title);54             Label titleOne=new Label(0,1,"統計1",wcf_title1);55             Label titleTwo=new Label(2,1,"統計2",wcf_title2);56 57             Label column1=new Label(0,2,"姓名",wcf_head1);58             Label column2=new Label(1,2,"所選課程",wcf_head1);59             60             Label column3=new Label(2,2,"姓名",wcf_head2);61             Label column4=new Label(3,2,"所選課程",wcf_head2);62 63 64 65             //或者WritableCell cell =  new jxl.write.Number(column, row, value, wcf)66             //將定義好的儲存格添加到工作表中 67             sheetOne.addCell(title); 68             sheetOne.addCell(titleOne); 69             sheetOne.addCell(titleTwo); 70 71             sheetOne.addCell(column1); 72             sheetOne.addCell(column2); 73             sheetOne.addCell(column3); 74             sheetOne.addCell(column4); 75 76 77             //合: 第1列第1行  到 第13列第1行78             sheetOne.mergeCells(0, 0, 3, 0); 79             sheetOne.mergeCells(0, 1, 1, 1); 80             sheetOne.mergeCells(2, 1, 3, 1); 81             //寫入資料並關閉檔案 82             book.write(); 83             book.close(); 84             url = String.format("%s%s", filePath, fileName);85         }86         catch (Exception e) {87            e.printStackTrace();88         }89         return url;90     }91 }
點擊 + 展開

--js端調用,頁面可下載

window.location.href = url;

Java匯出Excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.