Wypełnianie tabel w dokumentach docx

0

W swojej aplikacji muszę generować pliki docx. Do tej pory do wypełniania tabel używałem XDocReport oraz Velocity. Jednak teraz potrzebuję wygenerować dane w tabeli tak aby wyglądały w następujący sposób
user image

Aktualnie potrafię tylko wypisać wszystkie wiersze z powtarzającą się datą w następujący sposób.
user image

Czy jest możliwe, żeby to osiągnąć używając XDocReport? Jeżeli nie, to czy moglibyście polecić jakąś bibliotekę w której mógłbym taki efekt uzyskać?

0

Nie używałem XDocReport więc nie powiem Ci, czy da sie to osiągnać. Wiem natomiast, że możesz coś takiego wygenerować używając biblioteki docx4j http://www.docx4java.org/trac/docx4j

0

Próbowałem używać docx4j, ale potrafiłem tylko edytować proste tabele. Ze względu na prędkość oraz prostotę później używałem DocxReport. Czy mógłbyś podpowiedzieć w jaki sposób mógłbym uzyskać oczekiwany przeze mnie efekt?

	private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
		List<Object> result = new ArrayList<Object>();
		if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
 
		if (obj.getClass().equals(toSearch))
			result.add(obj);
		else if (obj instanceof ContentAccessor) {
			List<?> children = ((ContentAccessor) obj).getContent();
			for (Object child : children) {
				result.addAll(getAllElementFromObject(child, toSearch));
			}
 
		}
		return result;
	}
	
	private static void replacePlaceholder(WordprocessingMLPackage template, String name, String placeholder ) {
		List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class);
 
		for (Object text : texts) {
			Text textElement = (Text) text;
			if (textElement.getValue().equals(placeholder)) {
				textElement.setValue(name);
			}
		}
	}
	
	private static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd,
			WordprocessingMLPackage template) throws Docx4JException, JAXBException {
		List<Object> tables = getAllElementFromObject(template.getMainDocumentPart(), Tbl.class);
 
		Tbl tempTable = getTemplateTable(tables, placeholders[0]);
		List<Object> rows = getAllElementFromObject(tempTable, Tr.class);
 
		if (rows.size() == 2) {
			Tr templateRow = (Tr) rows.get(1);
 
			for (Map<String, String> replacements : textToAdd) {
				addRowToTable(tempTable, templateRow, replacements);
			}
 
			tempTable.getContent().remove(templateRow);
		}
	}
	
	private static Tbl getTemplateTable(List<Object> tables, String templateKey) throws Docx4JException, JAXBException {
		for (Iterator<Object> iterator = tables.iterator(); iterator.hasNext();) {
			Object tbl = iterator.next();
			List<?> textElements = getAllElementFromObject(tbl, Text.class);
			for (Object text : textElements) {
				Text textElement = (Text) text;
				if (textElement.getValue() != null && textElement.getValue().equals(templateKey))
					return (Tbl) tbl;
			}
		}
		return null;
	}
	
	private static void addRowToTable(Tbl reviewtable, Tr templateRow, Map<String, String> replacements) {
		Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);
		List<?> textElements = getAllElementFromObject(workingRow, Text.class);
		for (Object object : textElements) {
			Text text = (Text) object;
			String replacementValue = (String) replacements.get(text.getValue());
			if (replacementValue != null)
				text.setValue(replacementValue);
		}
 
		reviewtable.getContent().add(workingRow);
	}
0

Robiłem takie rzeczy na ODTkach. Tylko, ze operowałem na czystym XMLu.
Zobacz, moze będzie ci łatwiej.

0

Zależałoby mi jednak, aby uzyskać docx. Obawiam się, że jak będę zmieniał rozszerzenie z odt to zawartość dokumentu się rozjedzie.

0

docx to tez jest xml

0

A jakiej biblioteki używałeś do edycji plików .odt?

0

tylko org.w3c.dom.*

1 użytkowników online, w tym zalogowanych: 0, gości: 1