Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void createStyledTable(ArrayList<orderedObject> x) throws Exception {
- XWPFDocument doc = new XWPFDocument();
- try {
- ArrayList<orderedObject> orderedScrews = x;
- List<String> properties = null;
- ArrayList<String> tableInfo = new ArrayList<String>();
- tableInfo.add("Pos");
- tableInfo.add("Artikelnummer");
- tableInfo.add("Qty");
- tableInfo.add("Note");
- tableInfo.add("€ / pcs");
- tableInfo.add("Summo €");
- int nRows = orderedScrews.size();
- int nCols = 6;
- XWPFTable table = doc.createTable(nRows, nCols);
- // Set the table style. If the style is not defined, the table style
- // will become "Normal".
- CTTblPr tblPr = table.getCTTbl().getTblPr();
- CTString styleStr = tblPr.addNewTblStyle();
- styleStr.setVal("StyledTable");
- // Get a list of the rows in the table
- List<XWPFTableRow> rows = table.getRows();
- int rowCt = 0;
- int colCt = 0;
- for (XWPFTableRow row : rows) {
- orderedObject obj = orderedScrews.get(rowCt);
- properties = orderedObject.toArrayList(obj);
- // get table row properties (trPr)
- CTTrPr trPr = row.getCtRow().addNewTrPr();
- // set row height; units = twentieth of a point, 360 = 0.25"
- CTHeight ht = trPr.addNewTrHeight();
- ht.setVal(BigInteger.valueOf(360));
- // get the cells in this row
- List<XWPFTableCell> cells = row.getTableCells();
- // add content to each cell
- for (XWPFTableCell cell : cells) {
- // get a table cell properties element (tcPr)
- CTTcPr tcpr = cell.getCTTc().addNewTcPr();
- // set vertical alignment to "center"
- CTVerticalJc va = tcpr.addNewVAlign();
- va.setVal(STVerticalJc.CENTER);
- // create cell color element
- CTShd ctshd = tcpr.addNewShd();
- ctshd.setColor("auto");
- ctshd.setVal(STShd.CLEAR);
- if (rowCt == 0) {
- // header row
- ctshd.setFill("D3DFEE");
- } else if (rowCt % 2 == 0) {
- // even row
- ctshd.setFill("D3DFEE");
- } else {
- // odd row
- ctshd.setFill("EDF2F8");
- }
- // get 1st paragraph in cell's paragraph list
- XWPFParagraph para = cell.getParagraphs().get(0);
- // create a run to contain the content
- XWPFRun rh = para.createRun();
- // style cell as desired
- if (colCt == nCols - 1) {
- // last column is 10pt Courier
- rh.setFontSize(10);
- rh.setFontFamily("Courier");
- }
- if (rowCt == 0) {
- // header row
- rh.setText(tableInfo.get(colCt));
- rh.setBold(true);
- rh.setColor("4283f4");
- para.setAlignment(ParagraphAlignment.CENTER);
- } else {
- // other rows
- rh.setText((String)properties.get(colCt));
- para.setAlignment(ParagraphAlignment.CENTER);
- }
- colCt++;
- } // for cell
- colCt = 0;
- rowCt++;
- } // for row
- //System.out.println(rowCt);
- // write the file
- OutputStream out = new FileOutputStream("new2.docx");
- //OutputStream out = new FileOutputStream(fileName);
- try {
- doc.write(out);
- } finally {
- out.close();
- }
- } finally {
- doc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement