POI 操作XLSX文件案例

package test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
 * 
 * @author jack hao
 * 2018年8月24日
 * TODOxlsx 文件创建测试
 */
public class TestExcelWhitOutGirdlines {
	public static void main(String[] args) {
		new TestExcelWhitOutGirdlines().createBody();
	}
/**
 * 
 * TODO创建xlsx 文件的样式及数据
 * @author jack hao
 * @void
 */
	public void createBody() {
		XSSFWorkbook xwb = new XSSFWorkbook();
		XSSFSheet sheet = xwb.createSheet("test");

		// turn off gridlines 设置网格线 打印
		sheet.setDisplayGridlines(false);
		sheet.setPrintGridlines(false);
		sheet.setFitToPage(true);
		sheet.setHorizontallyCenter(true);
		PrintSetup printSetup = sheet.getPrintSetup();
		printSetup.setLandscape(true);

		// 设置 下面五列的宽度
		sheet.setColumnWidth(1, 256 * 26);
		sheet.setColumnWidth(2, 256 * 50);
		sheet.setColumnWidth(3, 256 * 12);
		sheet.setColumnWidth(4, 256 * 20);
		sheet.setColumnWidth(5, 256 * 16);
		
		int lrNum = sheet.getLastRowNum();
		if(lrNum !=0){
			lrNum =lrNum+2;
		}

		// 创建 第一行
		XSSFRow row0 = sheet.createRow(lrNum);
		XSSFCell cell0_1 = row0.createCell(1);
		cell0_1.setCellValue("#######");

		XSSFCell cell0_5 = row0.createCell(5);
		cell0_5.setCellValue("#######");
		
		//创建第二行 
		XSSFRow row2 = sheet.createRow(lrNum+1);
		CellRangeAddress region2_1_5 = new CellRangeAddress(lrNum+1, lrNum+1, 1, 5);
	    sheet.addMergedRegion(region2_1_5);
		XSSFCell cell2_1 = row2.createCell(1);
		cell2_1.setCellStyle(getMyCellStyle(cell2_1, xwb));
		setFontCenter(cell2_1);
		cell2_1.setCellValue("################ ");
		
		//创建第四行
		XSSFRow row4 = sheet.createRow(3);
		row4.setHeightInPoints(28.0f);
		XSSFCell cell4_1 = row4.createCell(1);
		cell4_1.setCellStyle(getMyCellStyleLeftAndTopStrong(cell4_1, xwb));
		cell4_1.setCellValue("#######");
		
		XSSFCell cell4_2 = row4.createCell(2);
		cell4_2.setCellStyle(getMyCellStyleTopStrong(cell4_2, xwb));
		
		XSSFCell cell4_3 = row4.createCell(3);
		cell4_3.setCellStyle(getMyCellStyleTopStrong(cell4_3, xwb));
		cell4_3.setCellValue("#######");
		
		CellRangeAddress region4_4_5 = new CellRangeAddress(3, 3, 4, 5);
		sheet.addMergedRegion(region4_4_5);
		
		//创建第五行
		XSSFRow row5 = sheet.createRow(4);
		row5.setHeightInPoints(32.0f);
		XSSFCell cell5_1 = row5.createCell(1);
		cell5_1.setCellStyle(getMyCellStyleLeftStrong(cell5_1, xwb));
		cell5_1.setCellValue("#######");
		
		//合并单元格
		CellRangeAddress region5_7_2_2 = new CellRangeAddress(4, 6, 2, 2);
		sheet.addMergedRegion(region5_7_2_2);
		
		//合并单元格
		CellRangeAddress region5_9_3_4 = new CellRangeAddress(4,8,3,4);
		sheet.addMergedRegion(region5_9_3_4);
		
		XSSFCell cell5_2 = row5.createCell(2);
		cell5_2.setCellValue("#######");
		
		XSSFCell cell5_3 = row5.createCell(3);
		cell5_3.setCellValue("#######");
		
		XSSFCell cell5_5 = row5.createCell(5);
		cell5_5.setCellValue("#######");
		cell5_5.setCellStyle(getMyCellStyleRightStrong(cell5_5, xwb));
		
		//创建第六行 
		XSSFRow row6 = sheet.createRow(5);
		row6.setHeightInPoints(32.0f);
		XSSFCell cell6_1 = row6.createCell(1);
		cell6_1.setCellStyle(getMyCellStyleLeftStrong(cell6_1, xwb));
		cell6_1.setCellValue("#######");
		
		XSSFCell cell6_5 = row6.createCell(5);
		cell6_5.setCellStyle(getMyCellStyleRightStrong(cell6_5, xwb));
		cell6_5.setCellValue("#######");
		
		//创建第七行
		XSSFRow row7 = sheet.createRow(6);
		row7.setHeightInPoints(32.0f);
		XSSFCell cell7_1 = row7.createCell(1);
		cell7_1.setCellStyle(getMyCellStyleLeftStrong(cell7_1, xwb));
		cell7_1.setCellValue("#######");
		
		XSSFCell cell7_5 = row7.createCell(5);
		cell7_5.setCellStyle(getMyCellStyleRightStrong(cell7_5, xwb));
		cell7_5.setCellValue("#######");
		
		//创建第八行
		XSSFRow row8 = sheet.createRow(7);	
		row8.setHeightInPoints(32.0f);
		XSSFCell cell8_1 = row8.createCell(1);
		cell8_1.setCellStyle(getMyCellStyleLeftStrong(cell8_1, xwb));
		cell8_1.setCellValue("#######");
		
		//合并单元格
		CellRangeAddress region8_9_2_2 = new CellRangeAddress(7, 8, 2, 2);
		sheet.addMergedRegion(region8_9_2_2);
		
		XSSFCell cell8_2 = row8.createCell(2);
		cell8_2.setCellValue("#######");
		
		XSSFCell cell8_5 = row8.createCell(5);
		cell8_5.setCellStyle(getMyCellStyleRightStrong(cell8_5, xwb));
		cell8_5.setCellValue("#######");
		
		//合并单元格
		CellRangeAddress region9_11_1_1 = new CellRangeAddress(8, 10, 1, 1);
		sheet.addMergedRegion(region9_11_1_1);
		
		
		//创建第九行
		XSSFRow row9 = sheet.createRow(8);
		row9.setHeightInPoints(32.0f);
		XSSFCell cell9_1 = row9.createCell(1);
		cell9_1.setCellValue("#######");
		
		XSSFCell cell9_5 = row9.createCell(5);
		cell9_5.setCellStyle(getMyCellStyleRightStrong(cell9_5, xwb));
		
		//创建第十行
		XSSFRow row10 = sheet.createRow(9);
		row10.setHeightInPoints(28.0f);
		XSSFCell cell10_2 = row10.createCell(2);
		cell10_2.setCellStyle(getMyCellStyleGeneric(cell10_2, xwb));
		cell10_2.setCellValue("#######");
		
		//合并单元格
		CellRangeAddress region10_10_3_5 = new CellRangeAddress(9,9,3,5);
	    sheet.addMergedRegion(region10_10_3_5);
	    
	    XSSFCell cell10_3 = row10.createCell(3);
	    cell10_3.setCellValue("#######");
		
		//创建第十一行
	    
	    XSSFRow row11 = sheet.createRow(10);
	    row11.setHeightInPoints(28.0f);
		XSSFCell cell11_2 = row11.createCell(2);
		cell11_2.setCellStyle(getMyCellStyleBottomStrong(cell11_2, xwb));
		cell11_2.setCellValue("#######");
		
		//合并单元格
		CellRangeAddress region11_11_3_5 = new CellRangeAddress(10,10,3,5);
	    sheet.addMergedRegion(region11_11_3_5);
	    
	    XSSFCell cell11_3 = row11.createCell(3);
	    cell11_3.setCellValue("#######");
	    
	    //合并单元格
	    CellRangeAddress region13_14_1_5 = new CellRangeAddress(12, 13, 1, 5);
	    sheet.addMergedRegion(region13_14_1_5);
		
	    //创建第十三行
	    XSSFRow row13 = sheet.createRow(12);
	    XSSFCell cell13_1 = row13.createCell(1);
	    //setFontCenter(cell13_1);
	    cell13_1.setCellValue("#####################################");
	    
	    
	    setBorderStyleBottomAndRightStrong(sheet, region11_11_3_5);
	    setBorderStyleRightStrong(sheet, region10_10_3_5);
		setBorderStyleBottomAndLeft(sheet, region9_11_1_1);
        setBorderStyleGeneric(sheet, region8_9_2_2);
		setBorderStyleGeneric(sheet,region5_9_3_4);
		setBorderStyleGeneric(sheet, region5_7_2_2);
		setBorderStyleTopAndRightStrong(sheet, region4_4_5);
		FileOutputStream out = null;
		try {
			out = new FileOutputStream("D:\\out.xlsx");
			xwb.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * TODO设置单元格字体居中
	 * @author jack hao
	 * @void
	 * @param cell
	 */
	private  void setFontCenter(XSSFCell cell) {
		CellUtil.setAlignment(cell, HorizontalAlignment.CENTER);
		CellUtil.setVerticalAlignment(cell, VerticalAlignment.CENTER);
	}
	/**
	 * 
	 * TODO货色普通单元格样式 设置字体型号
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyle(XSSFCell cell , XSSFWorkbook xwb){
		XSSFFont font = xwb.createFont();
		font.setFontName("微软雅黑");
		font.setFontHeightInPoints((short) 16);
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setFont(font);	
		return cellStyle;	
	}
	/**
	 * 
	 * TODO单元格普通边框 黑色
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleGeneric(XSSFCell cell , XSSFWorkbook xwb){
		//XSSFFont font = xwb.createFont();
		//font.setFontName("微软雅黑");
		//font.setFontHeightInPoints((short) 16);
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		//cellStyle.setFont(font);	
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO获得单元格样式 边框黑色 左侧加粗
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleLeftStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.MEDIUM);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO单元格 右侧加粗 边框黑色
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleRightStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.MEDIUM);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO单元格左侧和顶部加粗 边框黑色
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleLeftAndTopStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.MEDIUM);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.MEDIUM);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO 单元格顶部加粗 边框黑色

	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleTopStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.THIN);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.MEDIUM);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO单元格 底部和左侧加粗黑色边框
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleBottomAndLeftStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.MEDIUM);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.MEDIUM);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO单元格底部加粗 黑色边框
	 * @author jack hao
	 * @CellStyle
	 * @param cell
	 * @param xwb
	 * @return
	 */
	private CellStyle getMyCellStyleBottomStrong(XSSFCell cell , XSSFWorkbook xwb){
		XSSFCellStyle cellStyle = xwb.createCellStyle();
		cellStyle.setBorderBottom(BorderStyle.MEDIUM);
		cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderLeft(BorderStyle.THIN);
		cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderTop(BorderStyle.THIN);
		cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
		cellStyle.setBorderRight(BorderStyle.THIN);
		cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
		return cellStyle;	
	}
	/**
	 * 
	 * TODO设置合并单元格 样式 边框黑色 上侧和右侧加粗
	 * @author jack hao
	 * @void
	 * @param sheet
	 * @param region
	 */
	private void setBorderStyleTopAndRightStrong(Sheet sheet, CellRangeAddress region) {
		// 合并单元格左边框样式
		RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
		RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格上边框样式
		RegionUtil.setBorderTop(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格右边框样式
		RegionUtil.setBorderRight(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格下边框样式
		RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
		RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
	}
	/**
	 * 
	 * TODO合并单元格 右侧加粗 整体黑色
	 * @author jack hao
	 * @void
	 * @param sheet
	 * @param region
	 */
	private void setBorderStyleRightStrong(Sheet sheet, CellRangeAddress region) {
		// 合并单元格左边框样式
		RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
		RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格上边框样式
		RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
		RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格右边框样式
		RegionUtil.setBorderRight(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格下边框样式
		RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
		RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
	}
	/**
	 * 
	 * TODO合并单元格样式下侧和右侧加粗 整体黑色
	 * @author jack hao
	 * @void
	 * @param sheet
	 * @param region
	 */
	private void setBorderStyleBottomAndRightStrong(Sheet sheet, CellRangeAddress region) {
		// 合并单元格左边框样式
		RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
		RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格上边框样式
		RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
		RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格右边框样式
		RegionUtil.setBorderRight(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格下边框样式
		RegionUtil.setBorderBottom(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
	}
	/**
	 * 
	 * TODO合并单元格 边框黑色
	 * @author jack hao
	 * @void
	 * @param sheet
	 * @param region
	 */
	private void setBorderStyleGeneric(Sheet sheet, CellRangeAddress region) {
		// 合并单元格左边框样式
		RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
		RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格上边框样式
		RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
		RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格右边框样式
		RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
		RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格下边框样式
		RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
		RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
	}
	/**
	 * 
	 * TODO合并单元格 下边框 和左侧边框加粗 边框黑色
	 * @author jack hao
	 * @void
	 * @param sheet
	 * @param region
	 */
	private void setBorderStyleBottomAndLeft(Sheet sheet, CellRangeAddress region) {
		// 合并单元格左边框样式
		RegionUtil.setBorderLeft(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格上边框样式
		RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
		RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格右边框样式
		RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
		RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
		// 合并单元格下边框样式
		RegionUtil.setBorderBottom(BorderStyle.MEDIUM, region, sheet);
		RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), region, sheet);
	}
}
发布了17 篇原创文章 · 获赞 7 · 访问量 5754

猜你喜欢

转载自blog.csdn.net/qq_29461579/article/details/82026616