Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWord
>
Tutorial
> Create header row
The example shows you how to create table's header row.
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); Cell cell1 = new Cell(); cell1.setWidth(new Width(TableWidthUnit.POINT, 1260)); cell1.setShading(new Shading(ShadingPattern.PERCENT_10)); 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 headerRow = new Row(); headerRow.setHeader(ExtendedBoolean.TRUE); headerRow.add(cell1); headerRow.add(cell2); headerRow.add(cell3); headerRow.add(cell4); headerRow.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(headerRow); for(int i=0; i < 100; i++) { 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*