POI 操作XLSX文件案例

本文详细介绍了如何使用Apache POI库来操作XLSX文件,包括读取、写入和修改Excel数据。通过实例代码,读者将掌握处理Excel文件的核心技巧。
摘要由CSDN通过智能技术生成
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);
		
		XSSFCel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_29461579

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值