Java 如何在使用ApachePOI与另一个excel进行比较后更改源excel工作表的单元格颜色
与另一个excel文件相比,我一直试图更改源excel文件中精确匹配字符串的单元格颜色,但无法使用所有建议的示例进行更改。 我将每个单元格条目视为字符串,并将该字符串与另一个excel工作表的行进行比较,如果找到匹配项,则希望将源字符串单元格颜色高亮显示为绿色。 这是我到目前为止为比较两个excel表(Book1和Book2)编写的代码,如果有人能够在Book1中指导更改精确匹配条件的单元格颜色,则需要帮助。 或者,如果需要使用Book1精确匹配条件中的重定向内容创建新的excel文件Java 如何在使用ApachePOI与另一个excel进行比较后更改源excel工作表的单元格颜色,java,excel,apache-poi,Java,Excel,Apache Poi,与另一个excel文件相比,我一直试图更改源excel文件中精确匹配字符串的单元格颜色,但无法使用所有建议的示例进行更改。 我将每个单元格条目视为字符串,并将该字符串与另一个excel工作表的行进行比较,如果找到匹配项,则希望将源字符串单元格颜色高亮显示为绿色。 这是我到目前为止为比较两个excel表(Book1和Book2)编写的代码,如果有人能够在Book1中指导更改精确匹配条件的单元格颜色,则需要帮助。 或者,如果需要使用Book1精确匹配条件中的重定向内容创建新的excel文件 imp
import org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.ss.usermodel.CellType;
导入java.io.FileInputStream;
导入java.io.IOException;
公共课{
公共静态void main(字符串[]srgs)引发IOException{
FileInputStream fileInputStream1=新建
FileInputStream(“C:\\Stuff\\JavaProject\\Book1.xlsx”);
XSSF工作簿工作簿1=新XSSF工作簿(fileInputStream1);
XSSFSheet worksheet1=工作簿1.getSheet(“Sheet1”);
int rowCount1=工作表1.getPhysicalNumberOfRows();
FileInputStream fileInputStream2=新建
FileInputStream(“C:\\Stuff\\JavaProject\\Book2.xlsx”);
XSSF工作簿工作簿2=新XSSF工作簿(fileInputStream2);
XSSFSheet worksheet2=工作簿2.getSheet(“Sheet1”);
int rowCount2=工作表2.getPhysicalNumberOfRows();
System.out.println(“行计数1=“+rowCount1+”行计数2=“+rowCount2”);
对于(int i=1;i 而(j您必须设置单元格样式,然后将所需的颜色应用于它
if (namestr1.equals(namestr2)) {
System.out.println("[Processing] :" + "NAME " + namestr1 + "=> Book 1 name = " + namestr1 + " Book 2 name = " +
// add color
XSSFCellStyle style = workbook1.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
assert name1 != null;
name1.setCellStyle(style);
FileOutputStream fos = new FileOutputStream(BOOK1);
workbook1.write(fos);
fos.close();
}
您必须设置单元格样式,然后将所需颜色应用于它
if (namestr1.equals(namestr2)) {
System.out.println("[Processing] :" + "NAME " + namestr1 + "=> Book 1 name = " + namestr1 + " Book 2 name = " +
// add color
XSSFCellStyle style = workbook1.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
assert name1 != null;
name1.setCellStyle(style);
FileOutputStream fos = new FileOutputStream(BOOK1);
workbook1.write(fos);
fos.close();
}
你可以接受这个答案来证明它有效。你可以接受这个答案来证明它有效。