Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Set cell color
The example shows you how to set table cell color.
import com.independentsoft.office.ExtendedBoolean; import com.independentsoft.office.word.Paragraph; import com.independentsoft.office.word.Run; import com.independentsoft.office.word.Shading; import com.independentsoft.office.word.ShadingPattern; import com.independentsoft.office.word.StandardBorderStyle; import com.independentsoft.office.word.WordDocument; import com.independentsoft.office.word.tables.Cell; import com.independentsoft.office.word.tables.Row; import com.independentsoft.office.word.tables.Table; import com.independentsoft.office.word.tables.TableWidthUnit; import com.independentsoft.office.word.tables.Width; public class Example { public static void main(String[] args) { try { WordDocument doc = new WordDocument(); Run run1 = new Run("Quantity"); run1.setBold(ExtendedBoolean.TRUE); Paragraph paragraph1 = new Paragraph(); paragraph1.add(run1); Run run2 = new Run("Item #"); run2.setBold(ExtendedBoolean.TRUE); Paragraph paragraph2 = new Paragraph(); paragraph2.add(run2); Run run3 = new Run("Description"); run3.setBold(ExtendedBoolean.TRUE); Paragraph paragraph3 = new Paragraph(); paragraph3.add(run3); Run run4 = new Run("Unit Price"); run4.setBold(ExtendedBoolean.TRUE); Paragraph paragraph4 = new Paragraph(); paragraph4.add(run4); Run run5 = new Run("Line Total"); run5.setBold(ExtendedBoolean.TRUE); Paragraph paragraph5 = new Paragraph(); paragraph5.add(run5); com.independentsoft.office.word.Color redColor = new com.independentsoft.office.word.Color("#FF0000"); Shading cell1Shading = new Shading(ShadingPattern.PERCENT_10); cell1Shading.setBackgroundColor(redColor); Cell cell1 = new Cell(); cell1.setWidth(new Width(TableWidthUnit.POINT, 1260)); cell1.setShading(cell1Shading); cell1.add(paragraph1); Cell cell2 = new Cell(); cell2.setWidth(new Width(TableWidthUnit.POINT, 1440)); cell2.setShading(new Shading(ShadingPattern.PERCENT_10)); cell2.add(paragraph2); Cell cell3 = new Cell(); cell3.setWidth(new Width(TableWidthUnit.POINT, 4140)); cell3.setShading(new Shading(ShadingPattern.PERCENT_10)); cell3.add(paragraph3); Cell cell4 = new Cell(); cell4.setWidth(new Width(TableWidthUnit.POINT, 1620)); cell4.setShading(new Shading(ShadingPattern.PERCENT_10)); cell4.add(paragraph4); Cell cell5 = new Cell(); cell5.setWidth(new Width(TableWidthUnit.POINT, 1620)); cell5.setShading(new Shading(ShadingPattern.PERCENT_10)); cell5.add(paragraph5); Row firstRow = new Row(); firstRow.add(cell1); firstRow.add(cell2); firstRow.add(cell3); firstRow.add(cell4); firstRow.add(cell5); Row row1 = new Row(); row1.add(new Cell()); row1.add(new Cell()); row1.add(new Cell()); row1.add(new Cell()); row1.add(new Cell()); Table table1 = new Table(StandardBorderStyle.SINGLE_LINE); table1.add(firstRow); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); table1.add(row1); doc.getBody().add(table1); doc.save("c:\\test\\output.docx", true); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Need help? Ask our developers:
Name*
Email*
Message*