Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. public void createStyledTable(ArrayList<orderedObject> x) throws Exception {
  2.         XWPFDocument doc = new XWPFDocument();
  3.  
  4.         try {
  5.             ArrayList<orderedObject> orderedScrews = x;
  6.             List<String> properties = null;
  7.             ArrayList<String> tableInfo = new ArrayList<String>();
  8.             tableInfo.add("Pos");
  9.             tableInfo.add("Artikelnummer");
  10.             tableInfo.add("Qty");
  11.             tableInfo.add("Note");
  12.             tableInfo.add("€ / pcs");
  13.             tableInfo.add("Summo €");
  14.            
  15.             int nRows = orderedScrews.size();
  16.             int nCols = 6;
  17.             XWPFTable table = doc.createTable(nRows, nCols);
  18.  
  19.             // Set the table style. If the style is not defined, the table style
  20.             // will become "Normal".
  21.             CTTblPr tblPr = table.getCTTbl().getTblPr();
  22.             CTString styleStr = tblPr.addNewTblStyle();
  23.             styleStr.setVal("StyledTable");
  24.  
  25.             // Get a list of the rows in the table
  26.             List<XWPFTableRow> rows = table.getRows();
  27.             int rowCt = 0;
  28.             int colCt = 0;
  29.            
  30.             for (XWPFTableRow row : rows) {            
  31.                
  32.                 orderedObject obj = orderedScrews.get(rowCt);  
  33.                 properties =   orderedObject.toArrayList(obj);
  34.  
  35.                 // get table row properties (trPr)
  36.                 CTTrPr trPr = row.getCtRow().addNewTrPr();
  37.                 // set row height; units = twentieth of a point, 360 = 0.25"
  38.                 CTHeight ht = trPr.addNewTrHeight();
  39.                 ht.setVal(BigInteger.valueOf(360));
  40.                
  41.                 // get the cells in this row
  42.                 List<XWPFTableCell> cells = row.getTableCells();
  43.                 // add content to each cell
  44.                 for (XWPFTableCell cell : cells) {
  45.                     // get a table cell properties element (tcPr)
  46.                     CTTcPr tcpr = cell.getCTTc().addNewTcPr();
  47.                     // set vertical alignment to "center"
  48.                     CTVerticalJc va = tcpr.addNewVAlign();
  49.                     va.setVal(STVerticalJc.CENTER);
  50.                  
  51.                     // create cell color element
  52.                     CTShd ctshd = tcpr.addNewShd();
  53.                     ctshd.setColor("auto");
  54.                     ctshd.setVal(STShd.CLEAR);
  55.                     if (rowCt == 0) {
  56.                         // header row
  57.                         ctshd.setFill("D3DFEE");
  58.                     } else if (rowCt % 2 == 0) {
  59.                         // even row
  60.                         ctshd.setFill("D3DFEE");
  61.                     } else {
  62.                         // odd row
  63.                         ctshd.setFill("EDF2F8");
  64.                     }
  65.  
  66.                     // get 1st paragraph in cell's paragraph list
  67.                     XWPFParagraph para = cell.getParagraphs().get(0);
  68.                     // create a run to contain the content
  69.                     XWPFRun rh = para.createRun();
  70.                     // style cell as desired
  71.                     if (colCt == nCols - 1) {
  72.                         // last column is 10pt Courier
  73.                         rh.setFontSize(10);
  74.                         rh.setFontFamily("Courier");
  75.                     }
  76.                     if (rowCt == 0) {
  77.                         // header row
  78.                         rh.setText(tableInfo.get(colCt));
  79.                         rh.setBold(true);
  80.                         rh.setColor("4283f4");
  81.                         para.setAlignment(ParagraphAlignment.CENTER);
  82.                     } else {
  83.                         // other rows
  84.                         rh.setText((String)properties.get(colCt));
  85.                         para.setAlignment(ParagraphAlignment.CENTER);
  86.                     }
  87.                     colCt++;
  88.                 } // for cell
  89.                 colCt = 0;
  90.                 rowCt++;
  91.             } // for row
  92.             //System.out.println(rowCt);
  93.             // write the file
  94.             OutputStream out = new FileOutputStream("new2.docx");
  95.             //OutputStream out = new FileOutputStream(fileName);
  96.             try {
  97.                 doc.write(out);
  98.             } finally {
  99.                 out.close();
  100.             }
  101.         } finally {
  102.             doc.close();
  103.         }
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement