jxl操作合併單元格複雜表頭
阿新 • • 發佈:2019-02-08
後臺操作:
@RequestMapping("/export") public void export(HttpServletRequest resquest,HttpServletResponse response){ try { /** * web端生成儲存開啟excel彈出框 */ response.setContentType("application/x-msdownload;charset=gbk"); String fileName = "課程表.xls"; String fileNameTemp = URLEncoder.encode(fileName, "UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileNameTemp.getBytes("utf-8"), "gbk")); OutputStream os = response.getOutputStream(); System.out.println("****start*****"); //開啟檔案 WritableWorkbook book= Workbook.createWorkbook(os); //生成名為“第一頁”的工作表,引數0表示這是第一頁 WritableSheet sheetOne=book.createSheet("第一頁",0); /** * 非web端的話可以直接採用如下 * 開啟檔案 * WritableWorkbook book= Workbook.createWorkbook(new File("測試.xls")); * 生成名為“第一頁”的工作表,引數0表示這是第一頁 * WritableSheet sheetOne=book.createSheet("第一頁",0); */ /** * 定義單元格樣式 */ WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定義格式 字型 下劃線 斜體 粗體 顏色 WritableFont wf_head = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定義格式 字型 下劃線 斜體 粗體 顏色 WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); // 定義格式 字型 下劃線 斜體 粗體 顏色 WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 單元格定義 wcf_title.setBackground(jxl.format.Colour.WHITE); // 設定單元格的背景顏色 wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 設定對齊方式 wcf_title.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //設定邊框 WritableCellFormat wcf_title1 = new WritableCellFormat(wf_title); // 單元格定義 wcf_title1.setBackground(jxl.format.Colour.LIGHT_GREEN); // 設定單元格的背景顏色 wcf_title1.setAlignment(jxl.format.Alignment.CENTRE); // 設定對齊方式 wcf_title1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_title1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //設定邊框 WritableCellFormat wcf_title2 = new WritableCellFormat(wf_title); // 單元格定義 wcf_title2.setBackground(jxl.format.Colour.YELLOW2); // 設定單元格的背景顏色 wcf_title2.setAlignment(jxl.format.Alignment.CENTRE); // 設定對齊方式 wcf_title2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_title2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //設定邊框 WritableCellFormat wcf_head1 = new WritableCellFormat(wf_head); wcf_head1.setBackground(jxl.format.Colour.LIGHT_GREEN); wcf_head1.setAlignment(jxl.format.Alignment.CENTRE); wcf_head1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_head1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); WritableCellFormat wcf_head2 = new WritableCellFormat(wf_head); wcf_head2.setBackground(jxl.format.Colour.YELLOW2); wcf_head2.setAlignment(jxl.format.Alignment.CENTRE); wcf_head2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_head2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); WritableCellFormat wcf_table1 = new WritableCellFormat(wf_table); wcf_table1.setBackground(jxl.format.Colour.LIGHT_GREEN); wcf_table1.setAlignment(jxl.format.Alignment.CENTRE); wcf_table1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_table1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); WritableCellFormat wcf_table2 = new WritableCellFormat(wf_table); wcf_table2.setBackground(jxl.format.Colour.YELLOW2); wcf_table2.setAlignment(jxl.format.Alignment.CENTRE); wcf_table2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 設定垂直對齊方式 wcf_table2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); sheetOne.setColumnView(0, 15); // 設定列的寬度 sheetOne.setColumnView(1, 15); // 設定列的寬度 sheetOne.setColumnView(2, 15); // 設定列的寬度 sheetOne.setColumnView(3, 15); // 設定列的寬度 sheetOne.setColumnView(4, 15); // 設定列的寬度 sheetOne.setColumnView(5, 15); // 設定列的寬度 sheetOne.setColumnView(6, 15); // 設定列的寬度 sheetOne.setColumnView(7, 15); // 設定列的寬度 sheetOne.setColumnView(8, 15); // 設定列的寬度 sheetOne.setColumnView(9, 15); // 設定列的寬度 sheetOne.setColumnView(10, 15); // 設定列的寬度 sheetOne.setColumnView(11, 15); // 設定列的寬度 sheetOne.setColumnView(12, 15); // 設定列的寬度 sheetOne.setColumnView(13, 15); // 設定列的寬度 //在Label物件的構造子中指名單元格位置是第一列第一行(0,0) //以及單元格內容為test Label title=new Label(0,0,"統計",wcf_title); Label titleOne=new Label(0,1,"統計1",wcf_title1); Label titleTwo=new Label(2,1,"統計2",wcf_title2); Label column1=new Label(0,2,"姓名",wcf_head1); Label column2=new Label(1,2,"所選課程",wcf_head1); Label column3=new Label(2,2,"姓名",wcf_head2); Label column4=new Label(3,2,"所選課程",wcf_head2); //或者WritableCell cell = new jxl.write.Number(column, row, value, wcf) //將定義好的單元格新增到工作表中 sheetOne.addCell(title); sheetOne.addCell(titleOne); sheetOne.addCell(titleTwo); sheetOne.addCell(column1); sheetOne.addCell(column2); sheetOne.addCell(column3); sheetOne.addCell(column4); //合: 第1列第1行 到 第13列第1行 sheetOne.mergeCells(0, 0, 3, 0); sheetOne.mergeCells(0, 1, 1, 1); sheetOne.mergeCells(2, 1, 3, 1); /*動態資料 */ Label content1=new Label(0,3,"張三",wcf_table1); Label content2=new Label(0,4,"張三",wcf_table1); Label content3=new Label(0,5,"張三",wcf_table1); Label kecheg1=new Label(1,3,"語文",wcf_table1); Label kecheg2=new Label(1,4,"數學",wcf_table1); Label kecheg3=new Label(1,5,"英語",wcf_table1); sheetOne.addCell(content1); sheetOne.addCell(content2); sheetOne.addCell(content3); sheetOne.addCell(kecheg1); sheetOne.addCell(kecheg2); sheetOne.addCell(kecheg3); sheetOne.mergeCells(0, 3, 0, 2+3); Label content11=new Label(2,3,"李四",wcf_table2); Label content22=new Label(2,4,"李四",wcf_table2); Label content33=new Label(2,5,"李四",wcf_table2); Label kecheg11=new Label(3,3,"語文",wcf_table2); Label kecheg22=new Label(3,4,"數學",wcf_table2); Label kecheg33=new Label(3,5,"英語",wcf_table2); sheetOne.addCell(content11); sheetOne.addCell(content22); sheetOne.addCell(content33); sheetOne.addCell(kecheg11); sheetOne.addCell(kecheg22); sheetOne.addCell(kecheg33); sheetOne.mergeCells(2, 3, 2, 2+3); //寫入資料並關閉檔案 book.write(); book.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }