JAVA 操作execl,为execl内容添加边框

JAVA execl获取sheet页,并为其sheet页中的内容添加边框


1.添加前:
在这里插入图片描述
2.添加后:
在这里插入图片描述
代码

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
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.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

public class ExeclUtil {
    public static void SetBorder(String path) throws EncryptedDocumentException, InvalidFormatException, IOException {
    	FileInputStream inp = new FileInputStream(path); 
		Workbook wb = WorkbookFactory.create(inp);
		//获取sheet页页数
		int numberOfSheets = wb.getNumberOfSheets();
		for (int K = 0; K < numberOfSheets; K++) {
			Sheet sheet = wb.getSheetAt(K); 
			//获得总列数
			int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();
			//获得总行数
			int rowNum=sheet.getLastRowNum();
			CellStyle cellStyle = wb.createCellStyle();
			//下边框
			cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); 
			//左边框
			cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
			//上边框
			cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
	        //右边框
			cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
			for (int i = 0; i <= rowNum; i++) {
				Row row = sheet.getRow(i);
				for (int j = 0; j <coloumNum; j++) {
					Cell cell2 = row.getCell(j);
					cell2.setCellStyle(cellStyle);
				}
			}
		}
		wb.write(new FileOutputStream(path));
    }
}

测试

public static void main(String[] args) throws Exception {
	ExeclUtil.SetBorder("excel文件地址");	
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值