jxl操作合併單元格複雜表頭

後臺操作:

	@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();
		}
		
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章