Results 1 to 5 of 5
  1. #1
    louisa2012 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Writing a table without a Border to Word with Poi

    Hello :-)

    I will write a table to Word with the framework poi.
    From another webside I found an example of this. But unfortunately I don't know how to remove the border of the table.

    Did anyone have an idea?

    Best regards,

    louisa.


    Java Code:
    public static void main(String[] args) throws Exception {
    
    		XWPFDocument document = new XWPFDocument();
    
    		// Create a new table with 6 rows and 3 columns
    		int nRows = 2;
    		int nCols = 5;
    		XWPFTable table = document.createTable(nRows, nCols);
    
    
    		CTTblPr tablePr = table.getCTTbl().getTblPr();
    		CTString styleTable = tablePr.addNewTblStyle();
    		styleTable.setVal("meinStyle");
    
    		// Get a list of the rows in the table
    		List<XWPFTableRow> rows = table.getRows();
    		int rowCt = 0;
    		int colCt = 0;
    
    		// ueber die Zeilen iterieren
    		for (XWPFTableRow row : rows) {
    			CTTrPr rowProperties = row.getCtRow().addNewTrPr();
    			CTHeight rowHeight = rowProperties.addNewTrHeight();
    			rowHeight.setVal(BigInteger.valueOf(360));
    
    			// get the cells in this row
    			List<XWPFTableCell> cells = row.getTableCells();
    
    			// iteriere ueber die Zellen
    			for (XWPFTableCell cell : cells) {
    				CTTcPr cellPropertie = cell.getCTTc().addNewTcPr();
    				CTVerticalJc verticalCell = cellPropertie.addNewVAlign();
    				verticalCell.setVal(STVerticalJc.CENTER);
    
    				CTShd ctshd = cellPropertie.addNewShd();
    				ctshd.setColor("auto");
    				ctshd.setVal(STShd.CLEAR);
    				if (rowCt == 0) {
    					// header row
    					ctshd.setFill("A7BFDE");
    				} 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);
    
    				XWPFRun rh = para.createRun();
    				// style cell as desired
    
    				rh.setFontSize(5);
    				rh.setFontFamily("Courier");
    				para.setAlignment(ParagraphAlignment.CENTER);
    
    				if (rowCt == 0) {
    					// header row
    					rh.setText("header row, col " + colCt);
    				} else if (rowCt % 2 == 0) {
    					// even row
    					rh.setText("row " + rowCt + ", col " + colCt);
    				} else {
    					// odd row
    					rh.setText("row " + rowCt + ", col " + colCt);
    				}
    				colCt++;
    			} // for cell
    			colCt = 0;
    			rowCt++;
    		} // for row
    
    		// write the file
    
    		FileOutputStream out = new FileOutputStream("D:/styledTable1.doc");
    		document.write(out);
    		out.close();
    	}

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Writing a table without a Border to Word with Poi

    XWPFTable has some methods that deal with border types.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    louisa2012 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Writing a table without a Border to Word with Poi

    thats true. I try it with the following in Row 51:

    para.setBorderBottom(Borders.NONE);
    para.setBorderLeft(Borders.NONE);
    para.setBorderRight(Borders.NONE);
    para.setBorderTop(Borders.NONE);
    But it doesnt work, I still see the borders in the word document

  4. #4
    louisa2012 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Writing a table without a Border to Word with Poi

    Very good, now I paste only this Rows to my programm:


    Java Code:
    XWPFTable table = document.createTable(nRows, nCols);
    table.setInsideHBorder(XWPFBorderType.NONE, 10, 5, "1C7331");
    table.setInsideVBorder(XWPFBorderType.NONE, 10, 5, "1C7331");
    But the border outside are still there. Only the bordes outside are hidden.

    So I search for this solution....

    Thank you....

  5. #5
    louisa2012 is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Smile Re: Writing a table without a Border to Word with Poi

    Here is my solution. I use a window document which Iam import to my programm. This Word-Document already have an empty table without border and fill it in the programm:
    Java Code:
    private void adText2Table(XWPFDocument doc, String text, int row, int cell, int tableIdx) {
    
    		XWPFTableRow xWPFTableRow;
    		XWPFTable table = doc.getTables().get(tableIdx);
    		List<XWPFTableRow> zeile = table.getRows();
    
    		if (zeile.size() >= row + 1) {
    			xWPFTableRow = zeile.get(row);
    		} else {
    			xWPFTableRow = doc.getTables().get(tableIdx).createRow();
    		}
    
    		xWPFTableRow.getCell(cell).setText(text);
    	}

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  2. writing word document using poi hwpf
    By devday in forum New To Java
    Replies: 4
    Last Post: 08-07-2011, 11:15 AM
  3. create word doucment with Table of contents(TOC)
    By jagadeeshkasa in forum Forum Guides
    Replies: 0
    Last Post: 02-04-2011, 05:50 AM
  4. How to create a table in word document using hwpf
    By devday in forum Advanced Java
    Replies: 0
    Last Post: 07-23-2009, 02:41 PM
  5. Reading Writing to Microsoft Word -Urgent
    By Robert_85 in forum Advanced Java
    Replies: 5
    Last Post: 04-13-2009, 12:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •