標籤: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