poi解析excel文件,xssf的字体以及字体颜色处理

一个简单的测试用例,用来解析xlsx文件获取字体颜色


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;


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.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;





public class testReadExcel {
	public static void readExcel () throws Exception {
	String path = "d:\\字体颜色1.xlsx";
	File file = new File(path);
	InputStream is = new FileInputStream(file);
	Workbook wb = new XSSFWorkbook(is);
	int numbersheets = wb.getNumberOfSheets();
	Sheet sheet = wb.getSheetAt(0);
	int cols = sheet.getPhysicalNumberOfRows();
	for(int i = 0; i<cols;i++) {
		Row row = sheet.getRow(i);
		int cellnumber = row.getPhysicalNumberOfCells();
		for(int j = 0;j<cellnumber;j++) {
			Cell cell = row.getCell(j);
			CellStyle cellstyle1 = ((XSSFCell)cell).getCellStyle();
			XSSFCellStyle cellstyle = (XSSFCellStyle)cellstyle1;

		/*设置上边框大小和颜色边框*/
		if(isHssf) {//这里的ishssf用来判断是否是hssf类型。
			。。。
		}else {
			XSSFColor xsstopcolor = xsscellstyle.getTopBorderXSSFColor();
			String gc = xssBorderColor(xsstopcolor);
			if(!StrFunc.isNull(gc)) {
				ecell.setBorderTopColor(gc);
			}
		}
			
			/**
			 * 字体,关键是从cellstyle开始就要进行格式的转化,转成xssf的专有格式。
			 */
			XSSFFont font1 = cellstyle.getFont();
			XSSFColor xsscolor = font1.getXSSFColor();
			byte[] rgbcolor1 = xsscolor.getRGB();
			String argbHex = xsscolor.getARGBHex();
			byte[] rgbcolor = xsscolor.getARGB();
			System.out.println("argb"+rgbcolor);
			System.out.println("argbHex"+argbHex);
			StringBuffer sb = new StringBuffer();
			if (rgbcolor != null) {
				sb.append("#");
				for (int z = 0; z < rgbcolor.length; z++) {
					String part = Integer.toHexString(rgbcolor[z]);
					System.out.println(part);
				sb.append(part);
				}
			}
			System.out.println(sb);
		}
	}
	
	}
	public String xssBorderColor(XSSFColor xc) {
		if(xc!=null) {
			String argbcolor = xc.getARGBHex();
			if(!StrFunc.isNull(argbcolor)) {
			String gc = getColor(argbcolor);
			return gc;
			}
		}
		return null;
	}
	
	public String getColor(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append("#");
		if(str.length()>=8) {
			str = str.substring(2, 8);
		}
		sb.append(str);
		return sb.toString();
	}
	
	public static void main(String[] args) throws Exception{
		readExcel();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值