I have an excel file with extension xls. I want to give a background color of all the rows based on the first column value of each row . My code is not working based on my requirement .Back ground color is happening but not as per the requirement . I am using apache POI jar for implementation .
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
public class DemoPainter {
public void colorSheet() throws IOException {
FileInputStream fi = new FileInputStream(
"/vobs/SampleFile.xls");
POIFSFileSystem fs = new POIFSFileSystem(fi);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
System.out.println("Sheet Name: " + sheet.getSheetName());
startColor(sheet, wb);
}
public void startColor(HSSFSheet sheet, HSSFWorkbook wb) throws IOException {
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++){
Row row=sheet.getRow(rowIndex);
Cell cell=row.getCell(0);
System.out.println(cell.getStringCellValue());
if(cell.getStringCellValue().equals("modified")){
setYellowColor(wb,row);
}
else if(cell.getStringCellValue().equals("removed")){
setRedColor();
}
else {
setGreenColor();
}
}
FileOutputStream out = new FileOutputStream(new File("/vobs/SampleFile1.xls"));
wb.write(out);
out.close();
}
public void setYellowColor(HSSFWorkbook wb,Row row){
CellStyle style = wb.createCellStyle();
System.out.println("started Yellow color");
style.setFillForegroundColor(HSSFColor.YELLOW.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
row.setRowStyle(style);
}
public void setRedColor(){
}
public void setGreenColor(){
}
}
Required Output excel sheet image中获取div中第一个孩子的值, Current Output excel sheet image based on my code behavior
有人可以帮忙吗?
答案 0 :(得分:1)
宣传对答案的评论....
你有两个问题。问题#1 - 单元格样式是工作簿范围,所以不要为每一行创建一个!预先创建它们,否则您将超出工作簿中Excel允许的最大样式数。
问题#2 - 您没有在行中设置单个已完成单元格的样式。行样式是默认值,适用于Excel中添加的新单元格。它们不适用于已存在的单元格,因为在创建单元格时,它始终具有对应用样式的引用。因此,您还需要在行中的现有单元格上设置单元格样式
奖金问题#3 - 您已在所有地方HSSF
编码,因此您的代码仅适用于XLS
个文件。有关如何将代码更改为常规代码以及XLSX
的工作原理
将您的代码更改为......
File input = new File("/vobs/SampleFile.xls");
DataFormatter formatter = new DataFormatter();
Workbook wb = WorkbookFactory.create(input);
Sheet sheet = wb.getSheetAt(0);
System.out.println("Sheet Name: " + sheet.getSheetName());
CellStyle yellow = wb.createCellStyle();
yellow.setFillForegroundColor(Color.YELLOW.index);
yellow.setFillPattern(CellStyle.SOLID_FOREGROUND);
// And Red etc
for (Row r : sheet) {
Cell c1 = r.getCell(0);
if (c1 == null) {
// Empty row
continue;
}
// Get cell as string
String val = formatter.formatCellValue(c1);
// Check
if(val.equals("modified")) {
r.setRowStyle(yellow);
for (Cell c : r) {
c.setCellStyle(yellow);
}
}
else if(val.equals("removed")){
// etc
}
}
FileOutputStream out = new FileOutputStream(new File("/vobs/Changed.xls"));
wb.write(out);
out.close();