Hello,
I’m trying to convert the attached excel (TestLayout.xlsx inside TestLayout.zip (34.7 KB)
) to word but the conversion seems to collapse all rows, hiding completely their content, as the image below shows:
image.png (51.0 KB)
I’m using the following code for the conversion:
> private void appendExcelDocument(Document wordDocument, MultipartFile file) {
> try (ByteArrayOutputStream conversionStream = new ByteArrayOutputStream()){
> // Load the Excel file
> var workbook = new Workbook(new ByteArrayInputStream(file.getBytes()));
> // For each sheet set up the page orientation and the cells size
> for (int i = 0; i < workbook.getWorksheets().getCount(); i++ )
> {
> workbook.getWorksheets().get(i).getPageSetup().setOrientation(PageOrientationType.LANDSCAPE);
> workbook.getWorksheets().get(i).getPageSetup().setFitToPagesWide(1);
> workbook.getWorksheets().get(i).getPageSetup().setFitToPagesTall(0);
> }
> // Create the output stream to save the Excel conversion to word
> workbook.save(conversionStream, com.aspose.cells.SaveFormat.DOCX);
>
> var docAttachment = new Document(new ByteArrayInputStream(conversionStream.toByteArray()));
> // Dont copy the footers/headers from the target document in case they are already defined
> disableHeadersLink(docAttachment);
> // Remove any "empty" paragraphs the document may have in the end
> removeLastParagraph(docAttachment);
>
> List<Table> tables = Arrays.stream(docAttachment.getChildNodes(NodeType.TABLE, true).toArray())
> .filter(Table.class::isInstance)
> .map(Table.class::cast)
> .collect(Collectors.toList());
>
> for (Table table: tables) {
> table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
> }
>
> wordDocument.appendDocument(docAttachment, ImportFormatMode.KEEP_SOURCE_FORMATTING);
> } catch (IOException e) {
> throw new RuntimeException("Could not read content from file " + file.getName() + ". Error: " + e);
> }
> catch (Exception e) {
> throw new RuntimeException("Could not create Aspose Excel document from file " + file.getName() + ". Error: " + e);
> }
> }
>
> private void disableHeadersLink(Document wordDocument) {
> // Disable headers/footers inheriting.
> wordDocument.getFirstSection().getHeadersFooters().linkToPrevious(false);
> }
>
> private void removeLastParagraph(Document wordDocument) {
> // Get the last paragraph in the document.
> Paragraph lastPara = wordDocument.getLastSection().getBody().getLastParagraph();
> // Remove last run in the paragraph if it contains page break.
> if (lastPara.getRuns().getCount() > 0)
> {
> Run lastRun = lastPara.getRuns().get(lastPara.getRuns().getCount() - 1);
> if (lastRun.getText().equals(ControlChar.PAGE_BREAK))
> lastRun.remove();
> }
> }
As you can see I’m using AutoFitBehavior.AUTO_FIT_TO_WINDOW, but even so it seems that the rows do not adapt to their content.
Besides as you can see in the resulting word in the attachments (TestLayout.docx), not all columns of the Excel are visible. I understand that the excel contains many columns, but what would you advice here to better show the complete content in the final word?
Many thanks.Preformatted text