Mit Hilfe von POI eine Tabelle ohne Rahmen in Word schreiben

louisa2012

Mitglied
Hallo ihr Lieben,

ich sitze nun schon ewig an einem Problem.
Ich möchte eine Tabelle ohne Rahmen in ein Word-Dokument schreiben.
Dazu habe ich zwei Ansätze die aber beide nicht funktionieren.

Zu einem habe ich :


Java:
private static void test(int rows, short columns) throws Exception {

		POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream("D:/empty.doc"));
		HWPFDocument doc = new HWPFDocument(fileSystem);
		Range range = doc.getRange();

		TableProperties properties = new TableProperties(columns);
		Table table = range.insertBefore(properties, rows);

		for (int rowIdx = 0; rowIdx < table.numRows(); rowIdx++) {
			TableRow row = table.getRow(rowIdx);
			row.setRowHeight(20);

			for (int colIdx = 0; colIdx < row.numCells(); colIdx++) {
				TableCell cell = row.getCell(colIdx);
				try {
					Paragraph par = cell.getParagraph(0);
					par.insertBefore("test" + (rowIdx));
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}

		FileOutputStream outStream = null;
		try {
			outStream = new FileOutputStream("D:/test" + (new Date()).getTime() + ".doc");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			doc.write(outStream);
			outStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

In diesem Fall wird meine Tabelle als "ein Strich" dargestellt und erst wenn ich im Word-Dokument selber bin und die Tabelle "auseinanderziehe" kann ich alles sehen.

Im zweiten Fall habe ich es mit XWPFTableRow versucht. Bzw. ich habe den Code auf einer Seite gesehen und ihn versucht zu verstehen. Es klappt auch soweit, aber wie kann ich jetzt setzen das kein Rahmen gedruckt wird?

Java:
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();
	}


Ich hoffe ihr könnt mir Tipps geben.... ich verzweifle....
 
Hallo liebe Javafreunde,

ich habe für meine zweite Idee einen weiteren Hinweis bekommen. In Zeile 9 habe ich nun folgendes eingefügt:


Code:
XWPFTable table = document.createTable(nRows, nCols);
table.setInsideHBorder(XWPFBorderType.NONE, 10, 5, "1C7331");
table.setInsideVBorder(XWPFBorderType.NONE, 10, 5, "1C7331");

Leider ist der äußere Rahmen immer noch zu sehen. Die inneren Rahmen sind so ausgeblendet wie ich es haben möchte. Hat jemand eine Idee?

Lg Louisa.
 
Ich habe die Lösung. In meiner Vorlage füge ich schon solche Tabellen hinzu und kann die Zellen hiermit füllen:


Code:
private void tableHinzufuegen(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);
	}

Das einzige was jetzt noch fehlt ist das Ändern der Schriftgröße, sowie der Schriftart des Textes in der Zelle.

Hat jemand eine Idee,

lg Louisa.
 

Zurück
Oben