Hi Guys I am facing a problem when saving an excel workbook as .docx. I am trying to convert the following 2 table’s that are present in the following workbook-
check.zip (16.3 KB)
One is having issues with it’s border weights and another with it’s column heading-
Border wights are not coming right with this one-
image.png (13.6 KB)
column header is not correct with this one-
image.png (494 Bytes)
the code is straight forward i set the imgorprintoptions and pagesetup-
PageSetup-
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
ImgOrPrintOptions-
ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.OfficeCompatibleEmf,
};
@Adhirath
When converting an Excel workbook to a DOCX format, issues with table formatting, such as border weights and column headers, can arise. Here are some insights and potential solutions based on similar experiences shared by other users:
-
Border Issues: It has been noted that during the conversion process, borders may appear differently than expected. For instance, borders that are set to “None” in Excel might still render as thick borders in the DOCX output. This discrepancy can occur even if the Excel file does not visually display these borders. To address this, you can try explicitly setting the border styles to “None” in your code before saving the workbook. Here’s a sample code snippet that demonstrates how to remove borders:
Workbook workbook = new Workbook("path_to_your_excel_file.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
Range range = worksheet.Cells.MaxDisplayRange;
Style style = workbook.CreateStyle();
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.None;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.None;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.None;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.None;
StyleFlag flag = new StyleFlag();
flag.Borders = true;
range.ApplyStyle(style, flag);
DocxSaveOptions saveOptions = new DocxSaveOptions();
workbook.Save("output.docx", saveOptions);
-
Column Header Issues: Another common problem is that column headers may not be transferred correctly to the DOCX file. This can happen if the headers are not formatted properly in the Excel file or if there are specific settings in the conversion process that need to be adjusted. Unfortunately, there is currently no direct fix for this issue, but it has been acknowledged, and a ticket has been logged for further investigation.
-
General Recommendations: It is advisable to ensure that your Excel file is formatted correctly before conversion. Additionally, using the latest version of the Aspose.Cells library can help, as updates may include fixes for known issues.
If you continue to experience problems, consider reaching out to Aspose support with your specific files for more tailored assistance.
Sources:
[1]: Aspose Cells Save as Word
@Adhirath
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): CELLSNET-58430
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like
@Adhirath
Net58430.docx (10.7 KB)
Please check the attached file. We have fixed this issue.
Hi Simon, table 2 looks great but I am unable differentiate the border weights in table 1.
@Adhirath
We copied table1 from Excel to Word, then save the file.
We found :
Excel Word
Hair/Thin 4pt
Medium 8pt
Thick 12pt.
We exported border weight as above table .
Got you. Thanks guys! Also any idea when the changes made for table 2 will be available
@Adhirath
Thank you for your feedback. You are welcome. These modifications will be reflected in the next version v25.6 which will be released in the first half of June 2025. We will notify you promptly once version v25.6 is released.
1 Like
The issues you have found earlier (filed as CELLSNET-58430) have been fixed in this update. This message was posted using Bugs notification tool by leoluo
1 Like
Great! I had a small doubt guys is there a method to get the address of a table?
@Adhirath
Please refer to the following example code to get the table address.
ListObject listObject = "your table object";
Console.WriteLine(listObject.DataRange.Address);
Hope helps a bit.
will this include the headers or just the data, is there a way to get the range for the entire table. So if there is a range CN156:CR174 and it has a table with range CN160:CR167, i want the range for the table
@Adhirath
Please refer to the following code to get the table range and table data range.
ListObject listObject = workbook.Worksheets[0].ListObjects["your table object"];
//table range
string startCell = CellsHelper.CellIndexToName(listObject.StartRow, listObject.StartColumn);
string endCell = CellsHelper.CellIndexToName(listObject.EndRow, listObject.EndColumn);
Range range = sheet.Cells.CreateRange(startCell, endCell);
Console.WriteLine(range.Address);
//table data range
Console.WriteLine(listObject.DataRange.Address);
Hope helps a bit.
1 Like
Thanks for the replay. I am facing another issue. The table created is wider when compared to a table which is pasted directly from excel. below is a word document containing 2 tables, 1 created using aspose by converting xlsx to docx and the other copy pasted from excel.
Code-
PageSetup-
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
ImgOrPrintOptions-
ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
OnlyArea = true,
ImageType = ImageType.OfficeCompatibleEmf,
};
is there a way to fix the issue?
word doc-
Document_1750066092.zip (31.3 KB)
@Adhirath,
Thank you for sharing the Word document.
Could you please also provide the source XLSX file? This will help us evaluate your issue more effectively using Aspose.Cells APIs on our end.
Sure, here-
ExcelFormats.zip (24.5 KB)
@Adhirath,
I tested your scenario/case using your template Excel file and following sample code. I found both Aspose.Cells and copy/paste table from Excel work the same way. I got approximately same results.
e.g.,
Sample code:
Workbook workbook = new Workbook("e:\\test2\\ExcelFormats.xlsx");
foreach (Worksheet worksheet in workbook.Worksheets)
{
if (worksheet.Index > 0)
{
ListObject listObject = worksheet.ListObjects[0];
//table range
string startCell = CellsHelper.CellIndexToName(listObject.StartRow, listObject.StartColumn);
string endCell = CellsHelper.CellIndexToName(listObject.EndRow, listObject.EndColumn);
Aspose.Cells.Range range = worksheet.Cells.CreateRange(startCell, endCell);
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = range.Address;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;
//table data range
//Console.WriteLine(listObject.DataRange.Address)
}
}
DocxSaveOptions docxSaveOptions = new DocxSaveOptions();
workbook.Save("e:\\test2\\outdocument1_Aspose.Cells.docx", docxSaveOptions);
outdocument1_Aspose.Cells.docx (14.1 KB)
Please also find attached MS Word file when copying/pasting from Excel for the table in the second worksheet.
outdocument1table1_excel.zip (14.3 KB)
In case you still find the issue, kindly share your exact (runnable) sample code (same as above), sample files and provide some screenshots to highlight with problematic areas, we will check it soon.
I use the same logic, below is the code. I convert it to flatopc as I need the ooxml content. Could that be causing the issue?
code-
docx conversion code.zip (1.3 KB)
Please provide the changes in the same shared text file in order to maintain privacy in case there are any.
@Adhirath
We export Excel file to docx as printing. When printing the sheet in Excel, the width of the columns will be scaled up. So result of aspose.cells is larger than copied from Excel.
If we export as the view of MS Excel, sometimes the paging will be totally different .
We will check how to work as copying.
sure, let me know if there is anything I can tweak to make it more in line with excel paste.