Saving Excel as .Docx having an issue with cell values if they are greater than column width

When I try to save a excel document having a table with cell values wider than the column width as Docx, it ends up distorting the values in the resulting table.
this is the table I am trying to convert to a word table and notice the HireDate column, it has content which spans beyond the column-
image.png (6.1 KB)

and this is the result I am getting-
image.png (6.1 KB)

notice the blank space in the same column. the cell actually holds the value if i expand it but otherwise it looks very odd and seems like it has only partial value.

the code i use-
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;

DocxSaveOptions saveOptions = new DocxSaveOptions();
workbook.Save(ms, saveOptions);
ms.Position = 0;

here are the files for the same-
referenceWord.zip (26.0 KB)

reference Excel.zip (18.4 KB)

@Adhirath

Cause

The issue you’re experiencing with cell values in the resulting Word document likely stems from the column width not being sufficient to display the full content of the cells. When the content exceeds the column width, it can lead to distortion or blank spaces in the output.

Solution

To address this, you can adjust the column widths in your Excel worksheet before saving it as a DOCX file. Here are a few strategies you can implement:

  1. AutoFit Columns: Use the AutoFitColumns() method to automatically adjust the width of the columns based on their content.
  2. Set Column Width Manually: If you have specific widths in mind, you can set them manually to ensure that all content fits properly.

Code

Here’s an example of how you can modify your code to include auto-fitting for columns:

// AutoFit columns before saving
worksheet.AutoFitColumns();

// Set print area and margins
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;

// Save options for DOCX
DocxSaveOptions saveOptions = new DocxSaveOptions();
workbook.Save(ms, saveOptions);
ms.Position = 0;

Next Steps

  1. Implement the AutoFitColumns() method in your code to ensure that all columns adjust to fit their content.
  2. Test the output after making these changes to confirm that the distortion issue is resolved.
  3. If the problem persists, manually set the column widths based on the maximum content length to ensure proper display.

Please proceed with these steps, and let me know if you need further assistance!

@Adhirath,

Thanks for the sample files and screenshots.

In your code you are using AutoFitRows() method, you should use AutoFitColumns() method. I tested using your template Excel file and following sample code and it works fine and as expected.
e.g.,
Sample code:

Workbook workbook = new Workbook("e:\\test2\\reference Excel.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
string printArea = "H17:N22";

worksheet.AutoFitColumns();

worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;

DocxSaveOptions saveOptions = new DocxSaveOptions();
workbook.Save("e:\\test2\\out1.docx", saveOptions);

Please find attached the output DOCX file in the zipped archive for your reference.
out1.zip (8.4 KB)

Let us know if you still find any issue.

I actually don’t want to use Autofit for rows or columns

@Adhirath,

I am afraid, there may not be better alternative or solution at the moment. You might notice and confirm that the content or data isn’t fully displayed for the columns, even in MS Excel. So, to resolve this, kindly consider using AutoFitColumns(). You may also use AutoFitColumn() method (instead of AutoFitColumns()) to fit your desired column’s data only.

1 Like

Thanks for the help, I need help with one more thing. How do I copy a range using aspose cells for .net without copying the formula?

@Adhirath
If you only want to copy range data and styles, please refer to the following documents.

Of course, you can also use PasteOptions to have finer grained control over the formats, row height, etc. that need to be copied when copying the scope. please refer to the following document.

1 Like

so there is no way to fix this blank space without using autofitcolumns?
image.png (7.8 KB)

@Adhirath
Thank you for your feedback. The best solution currently is to use the AutoFitColumns method to adjust column width and fully display the content. If you have any questions, please feel free to contact us at any time.

sure, appreciate the help!

@Adhirath
Thank you for your feedback. You are welcome. If you have any questions, please feel free to contact us at any time.

1 Like

quick question guys, does calculate formula calculate the pivot table as well and what does getSettings().getFormulaSettings().setEnableCalculationChain(true) do?

@Adhirath
calculateFormula() does not directly calculate the data of PivotTables themselves; it is used to evaluate all formula cells within the workbook.
PivotTables are not calculated through formulas.
If your formulas depend on the result of a PivotTable, you should ensure the PivotTable is up to date before calling calculateFormula().

For each PivotTable, you can call:

pivotTable.refreshData();
pivotTable.calculateData();

Additionally, regarding setEnableCalculationChain, please refer to the following explanation:
When there are lots of formulas in the workbook and the user needs to calculate them repeatedly while only modifying a small part of them, enabling the calculation chain may help improve performance.

On the other hand, if the chain is enabled, maintaining the chain model requires extra memory, and it also consumes a bit more CPU time for certain operations such as changing cell values or formulas.

After setting this property from false to true, the calculation chain will be analyzed and built during the first formula calculation of the workbook, so the initial calculation may take longer than a normal calculation without the chain enabled.
Hope helps a bit!

1 Like

@Adhirath,

Workbook.calculateFormula() does not calculate PivotTables. The method only processes spreadsheet formulas (e.g., =SUM(A1:A10), =VLOOKUP(...), etc.). PivotTables are a different engine, they are not updated by formula calculation engine. To refresh/calculate PivotTables, you must use:

pivotTable.refreshData();
pivotTable.calculateData();

Or refresh all pivot tables:

For getSettings().getFormulaSettings().setEnableCalculationChain(), this method enables or disables the formula calculations chain in the workbook. When the calculation chain is enabled (true), Aspose.Cells maintains a chain of formulas that depend on each other. This can improve performance when recalculating formulas multiple times because only the affected formulas in the chain are recalculated when you call Workbook.calculateFormula() method. When the calculation chain is disabled (false), all formulas are recalculated every time from the scratch, which can be slower for large workbooks with many formulas when you need to do the operation repeatedly.

Workbook workbook = new Workbook("Book1.xlsx");
workbook.getSettings().getFormulaSettings().setEnableCalculationChain(true); // Enable calculation chain

Hope, this helps a bit.

1 Like