Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JSpreadsheet
>
Tutorial
> Set Cell Fill Color
The following example shows you how to set cell fill color.
import com.independentsoft.office.spreadsheet.Cell; import com.independentsoft.office.spreadsheet.Formula; import com.independentsoft.office.spreadsheet.Workbook; import com.independentsoft.office.spreadsheet.Worksheet; import com.independentsoft.office.spreadsheet.styles.BackgroundColor; import com.independentsoft.office.spreadsheet.styles.CellFormat; import com.independentsoft.office.spreadsheet.styles.Fill; import com.independentsoft.office.spreadsheet.styles.ForegroundColor; import com.independentsoft.office.spreadsheet.styles.PatternFill; import com.independentsoft.office.spreadsheet.styles.PatternType; public class Example { public static void main(String[] args) { try { Fill grayFill = new Fill(); grayFill.setPattern(new PatternFill()); grayFill.getPattern().setType(PatternType.SOLID); grayFill.getPattern().setForegroundColor(new ForegroundColor()); grayFill.getPattern().getForegroundColor().setTheme(0); grayFill.getPattern().getForegroundColor().setTint(-0.34998626667073579); grayFill.getPattern().setBackgroundColor(new BackgroundColor()); grayFill.getPattern().getBackgroundColor().setColor("FF000000"); //black color for cell text Fill lightGrayFill = new Fill(); lightGrayFill.setPattern(new PatternFill()); lightGrayFill.getPattern().setType(PatternType.SOLID); lightGrayFill.getPattern().setForegroundColor(new ForegroundColor()); lightGrayFill.getPattern().getForegroundColor().setTheme(0); lightGrayFill.getPattern().getForegroundColor().setTint(-0.049989318521683403); lightGrayFill.getPattern().setBackgroundColor(new BackgroundColor()); lightGrayFill.getPattern().getBackgroundColor().setColor("FF000000"); //black color for cell text CellFormat format1 = new CellFormat(); format1.setFill(grayFill); format1.setApplyFill(true); //important CellFormat format2 = new CellFormat(); format2.setFill(lightGrayFill); format2.setApplyFill(true); //important Cell a1 = new Cell(100); a1.setFormat(format1); Cell b1 = new Cell(200); b1.setFormat(format2); Cell c1 = new Cell(); c1.setFormula(new Formula("SUM(A1,B1)")); Worksheet sheet1 = new Worksheet(); sheet1.set("A1", a1); sheet1.set("B1", b1); sheet1.set("C1", c1); Workbook book = new Workbook(); book.getSheets().add(sheet1); book.save("c:\\test\\output.xlsx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*