poi操作Excel(合并)

效果图(稍微不是很美观)

poi操作Excel(合并)_第1张图片

简单实现的代码:

package com.ucsoft.bas.task.web.order;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * poi操作Excel(合并)
 * 
 * @author hgg
 * @version 2015年2月14日上午10:27:23
 */
public class PoiExcel {
	public static void main(String[] args) {
		// 创建一个新的excel
		Workbook wb = new HSSFWorkbook();
		// 创建sheet页
		Sheet sheet = wb.createSheet("new sheet");
		// 设置样式
		CellStyle cellStyle = wb.createCellStyle();
		// 对齐方式
		cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		// 边框
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setWrapText(true);// 自动换行
		cellStyle.setFont(setFont(wb));// 设置字体

		Row row = sheet.createRow((short) 0);
		Cell cell = row.createCell((short) 0);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("合并列");
		// 合并列
		sheet.addMergedRegion(new CellRangeAddress(0, // first row (0-based)
				0, // last row (0-based)
				0, // first column (0-based)
				1 // last column (0-based)
		));

		row = sheet.createRow((short) 1);
		cell = row.createCell((short) 0);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("合并行");
		// 合并行
		sheet.addMergedRegion(new CellRangeAddress(1, 4, 0, 0));

		// 合并后的行1
		cell = row.createCell((short) 1);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("行2-1");
		cell = row.createCell((short) 2);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("行2-2");

		// 合并后的行2
		row = sheet.getRow(2);
		if (row == null) {
			row = sheet.createRow(2);
		}
		cell = row.createCell((short) 1);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("3-1");
		cell = row.createCell((short) 2);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("3-2");
		// 合并后的行3
		row = sheet.getRow(3);
		if (row == null) {
			row = sheet.createRow(3);
		}
		cell = row.createCell((short) 1);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("4-1");
		cell = row.createCell((short) 2);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("4-2");

		// 单独的一行
		row = sheet.createRow(5);
		cell = row.createCell(0);
		cell.setCellStyle(cellStyle);
		cell.setCellValue("单独的一行(第五行)");

		// Write the output to a file
		FileOutputStream fileOut;
		try {
			fileOut = new FileOutputStream("c:/workbook.xls");
			wb.write(fileOut);
			fileOut.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 设置字体
	 * 
	 * @param wb
	 * @return
	 * @author hgg
	 * @version 2015年2月14日上午10:47:50
	 */
	private static Font setFont(Workbook wb) {
		// Create a new font and alter it.
		Font font = wb.createFont();
		font.setFontHeightInPoints((short) 12);
		font.setFontName("Courier New");
		// font.setItalic(true);
		// font.setStrikeout(true);//删除线
		return font;
	}
}



你可能感兴趣的:(poi操作Excel(合并))