Indentation applied to first cell affects the entire table

Hello everyone,
I’m encountering an issue when converting an Excel table to a Word document using Aspose.Cells and Aspose.Words.

Steps to reproduce:

  1. In Excel, create a table where the first cell has an indentation.
  2. Define a named range (area) over the table.
  3. Using Aspose.Cells, read the area and export it as HTML.
  4. Using Aspose.Words, insert the generated HTML table into a Word document.

Expected result:

The indentation should be applied only to the first cell.

Actual result:
In addition to the first cell being indented, the entire table is indented as well.

In the attachment, you can find an example Excel file and a Java test class that reproduces the issue.

first-cell-with-indent.png (29.5 KB)
sample.zip (12.8 KB)

@a.franco

It seems you’re experiencing an issue where the indentation applied to the first cell of an Excel table affects the entire table when converting it to a Word document using Aspose.Cells and Aspose.Words.

To address this, you can try the following steps:

  1. Export the Table as HTML: Ensure that when you export the table from Excel to HTML, the indentation is correctly applied only to the first cell. Check the HTML output to see how the indentation is represented.

  2. Insert HTML into Word Document: When inserting the generated HTML into the Word document, use the appropriate method that respects individual cell formatting.

Here’s a minimal code snippet to help you with the insertion:

// Load the HTML content
String htmlContent = "<table><tr><td style='padding-left: 20px;'>Indented Cell</td><td>Other Cell</td></tr></table>";

// Create a Document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert the HTML
builder.insertHtml(htmlContent);

// Save the document
doc.save("output.docx");
  1. Check CSS Styles: If the indentation still affects the entire table, consider using CSS styles to specifically target the first cell. This can help ensure that only the intended cell is indented.

  2. Review the Excel File: If possible, check the original Excel file to ensure that the indentation is set correctly and that there are no additional formatting settings that might be causing this issue.

If the problem persists, consider consulting the Aspose documentation or community forums for more specific guidance on handling HTML and table formatting during conversion.

The issue cannot be reproduced by inserting HTML directly into Word.
It only occurs when the HTML is first extracted from Excel and then written into Word.
Most likely, the HTML extraction process from Excel adds some styling that Word interprets incorrectly.

@a.franco,

Thanks for the sample file, screenshot and code snippet.

I have separated the module to test the functionality of extracting and exporting HTML from an Excel spreadsheet range or “INDENT” area using Aspose.Cells for Java. It appears to work well, and the resulting HTML output is satisfactory. I am currently utilizing the latest version, Aspose.Cells for Java v25.10, for this purpose…

import com.aspose.cells.HtmlHiddenColDisplayType;
import com.aspose.cells.HtmlHiddenRowDisplayType;
import com.aspose.cells.HtmlSaveOptions;
import com.aspose.cells.MergeEmptyTdType;
import com.aspose.cells.Range;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.aspose.cells.WorksheetCollection;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class TestIndent {

  private static final String DOCUMENT = "d:\\files\\first-cell-with-indent.xlsx";
  private static final String AREA = "INDENT";  

  public static void main(String[] args) throws Exception {
	  TestIndent testIndent = new TestIndent();
	  testIndent.testTableHavingFirstCellWithIndent();	  
  }

  public void testTableHavingFirstCellWithIndent() throws Exception {
    Workbook wb = new Workbook("d:\\files\\first-cell-with-indent.xlsx");
    String html = getAreaAsHtml(wb, AREA);        
    
  }

  public static String getAreaAsHtml(Workbook workbook, String areaName) throws Exception {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
      Range rangeByName = getRangeByName(workbook, areaName);

      workbook.getWorksheets().setActiveSheetIndex(rangeByName.getWorksheet().getIndex());
      rangeByName.getWorksheet().getPageSetup().setPrintArea(rangeByName.getAddress());
      workbook.save("d:\\files\\out1.html", defaultSaveOptions());
      workbook.save(byteArrayOutputStream, defaultSaveOptions());      
      return byteArrayOutputStream.toString(StandardCharsets.UTF_8);
    }
  }

  private static HtmlSaveOptions defaultSaveOptions() {
    HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
    options.setAddGenericFont(false);
    options.setExportPrintAreaOnly(true);
    options.setExportBogusRowData(false);
    options.setExportActiveWorksheetOnly(true);
    options.setHiddenRowDisplayType(HtmlHiddenRowDisplayType.REMOVE);
    options.setHiddenColDisplayType(HtmlHiddenColDisplayType.REMOVE);
    options.setMergeEmptyTdType(MergeEmptyTdType.NONE);

    return options;
  }

  private static Range getRangeByName(Workbook workbook, String name) {
    if (name == null || name.isEmpty()) {
      throw new RuntimeException("Range is empty");
    }
    WorksheetCollection worksheets = workbook.getWorksheets();
    Range range = worksheets.getRangeByName(name);
    if (Objects.isNull(range) && name.contains("!")) {
      range = worksheets.getRangeByName(name.split("!")[1]);
    }
    if (Objects.isNull(range)) {
      throw new RuntimeException("Range not found");
    }
    return range;
  }

}

Please find attached the output HTML file which seems OK to me.
out1.zip (2.0 KB)

A member of our Aspose.Words team will examine it concerning Aspose.Words API and will return to you with their findings and updates, @alexey.noskov FYI.