javaWeb 之 合併單元格

1、下圖excel,如何導出這樣含有合併單元格的excel?


2、解決辦法

excel自帶合併單元格方法,調用方法即可。

步驟如下:

1、由於是合併單元格,所以每行的列數不一樣。但是,我們每行插入的列數必須是最多的列數,方便合併單元格,而且也能控制單元格的格式(如線框顏色之類)

代碼如下

		//列名
		String[] columnName = { "序號","借款申請ID", "當前期數", "總期數", "借款主體", "貸款金額", "放款日期", "應還日期", "計息天數","客戶","","資方名稱","","運金所","","狀態"};
		String[] secondColumnName = { "單價", "應收利息","單價", "應收利息","單價", "服務費"};
		// 創建每列的欄目標題名
		createColunmName(myStyle, sheet, cell, row, workbook, 1, columnName);
		//創建二級欄目標題名
		createSecondColunmName(myStyle, sheet, cell, row, workbook, 2, secondColumnName,9);

		/**
		* 創建一級欄目
		*/
		public void createColunmName(HSSFCellStyle myStyle, Sheet sheet, Cell cell, Row row, HSSFWorkbook workbook,
			int rowIndex, String[] columnName) {
		// 創建第2行列名稱數據
		row = sheet.createRow(rowIndex);// excel座標從0開始
		row.setHeight((short) 400);
		// 插入數據
		for (int i = 0; i < columnName.length; i++) {
			cell = row.createCell(i);
			cell.setCellValue(columnName[i]);
			cell.setCellStyle(myStyle);
			}
		}

                /**
		* 創建二級欄目
		*/
		public static void createSecondColunmName(HSSFCellStyle myStyle, Sheet sheet, Cell cell, Row row, HSSFWorkbook workbook,
			int rowIndex, String[] columnName,int index) {
		// 創建二級欄目的列名稱數據
		row = sheet.createRow(rowIndex);// excel座標從0開始
		row.setHeight((short) 400);
		// 插入數據
		for (int i = index; i < columnName.length + index; i++) {
			cell = row.createCell(i);
			cell.setCellValue(columnName[i - index]);
			cell.setCellStyle(myStyle);
		}
		//設置第2行空餘格式
		for (int i = 0; i < index; i++) {
			cell = row.createCell(i);
			cell.setCellStyle(myStyle);
		}
		cell = row.createCell(15);
		cell.setCellStyle(myStyle);
		}

2、合併單元格。上下合併或左右合併。

因爲上圖的excel合併單元格在中間,所以我把excel分成了三段。

第一段,合併單元格區域之前的列。

第二段,合併單元格區域。

第三段,合併單元格區域之後的列。

	public void mergeColunm(Sheet sheet) {
		//合併
		//第一段,
		for (int i = 0; i < 9; i++) {
			sheet.addMergedRegion(new CellRangeAddress(1, 2, i, i));
		}
		//第二段,
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 9, 10));
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 11, 12));
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 13, 14));
		//第三段
		sheet.addMergedRegion(new CellRangeAddress(1, 2, 15, 15));
	}

3、每行按最多的列數插入數據。

由於插入數據比較簡單,且與合併單元格並無關係,所以免去不展示代碼部分,每行只需按最多的列數插入數據即可。


小結

以上,就可以作出合併單元格的excel。可能大家對有些數字看不懂,代碼裏充斥了一些列數的數字,如下面這行代碼

sheet.addMergedRegion(new CellRangeAddress(1, 1, 9, 10));

因爲不同的表格合併區域可能不同,所以這裏並沒有把這個方法封裝進工具類,直接使用了數字。

爲了讓大家能夠理解的更清楚,我特地借鑑了一部分核心代碼貼在下面:

/** 
     * 合併單元格 
     * @Title:MergeCell 
     * @Description: 
     * @param args 
     * @Date:2015年11月4日 下午2:36:46 
     * @return: void  
     * @throws Exception 
     */  
    @SuppressWarnings({ "resource", "deprecation" })  
    public static void main(String[] args) throws Exception   
    {  
        //創建workbook   
        HSSFWorkbook workbook = new HSSFWorkbook();   
        //創建sheet頁  
        HSSFSheet sheet = workbook.createSheet("學生表");   
        //創建單元格  
        HSSFRow row = sheet.createRow(0);   
        HSSFCell c0 = row.createCell(0);   
        c0.setCellValue(new HSSFRichTextString("學號"));   
        HSSFCell c1 = row.createCell(1);   
        c1.setCellValue(new HSSFRichTextString("姓名"));   
        HSSFCell c2 = row.createCell(2);   
        c2.setCellValue(new HSSFRichTextString("性別"));   
        HSSFCell c3 = row.createCell(3);   
        c3.setCellValue(new HSSFRichTextString("年齡"));   
        HSSFCell c4 = row.createCell(4);   
        c4.setCellValue(new HSSFRichTextString("2015年分數"));   
        HSSFCell c5 = row.createCell(7);   
        c5.setCellValue(new HSSFRichTextString("2014年分數"));   
        HSSFRow row1 = sheet.createRow(1);   
        HSSFCell c6 = row1.createCell(4);   
        c6.setCellValue(new HSSFRichTextString("語文"));   
        HSSFCell c7 = row1.createCell(5);   
        c7.setCellValue(new HSSFRichTextString("數學"));   
        HSSFCell c8 = row1.createCell(6);   
        c8.setCellValue(new HSSFRichTextString("外語"));  
        HSSFCell c9 = row1.createCell(7);   
        c9.setCellValue(new HSSFRichTextString("語文"));   
        HSSFCell c10 = row1.createCell(8);   
        c10.setCellValue(new HSSFRichTextString("數學"));   
        HSSFCell c11 = row1.createCell(9);   
        c11.setCellValue(new HSSFRichTextString("外語"));  
          
        Region region1 = new Region(0, (short)0, 1, (short)0);   
        Region region2 = new Region(0, (short)1, 1, (short)1);   
        Region region3 = new Region(0, (short)2, 1, (short)2);   
        Region region4 = new Region(0, (short)3, 1, (short)3);   
        Region region5 = new Region(0, (short)4, 0, (short)6);   
        Region region6 = new Region(0, (short)7, 0, (short)9);   
        sheet.addMergedRegion(region1);   
        sheet.addMergedRegion(region2);   
        sheet.addMergedRegion(region3);   
        sheet.addMergedRegion(region4);   
        sheet.addMergedRegion(region5);   
        sheet.addMergedRegion(region6);   
          
        FileOutputStream stream = new FileOutputStream("d:/student.xls");   
        workbook.write(stream);  
    }  

效果圖:



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