天天看點

javaWeb 之 合并單元格

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

javaWeb 之 合并單元格

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

效果圖:

javaWeb 之 合并單元格

繼續閱讀