Transfer Excel Range formatting to word Table formatting

@Adhirath
Please refer to the following example code for creating and traversing ranges. You can obtain a specific range by determining the starting row and column when traversing the ranges.

const AsposeCells = require("aspose.cells.node");

// Create the workbook 
const workbook = new AsposeCells.Workbook();

// Get the first worksheet in the worksheets collection.
const worksheet = workbook.getWorksheets().get(0);

const cells = worksheet.getCells();
// Create a range of cells.
const range1 = cells.createRange("A1", "A2");
const range2 = cells.createRange("C1", "C2");
const range3 = cells.createRange("E1", "E2");

const ranges = cells.getRanges();
ranges.add(range1);
ranges.add(range2);
ranges.add(range3);


const rangeCount =ranges.getCount();
console.log(rangeCount);
for (let i = 0; i < rangeCount; i++) {
    const range = ranges.get(i);
    console.log(range.getAddress() + "start row: " + range.getFirstRow() + " start column: " + range.getFirstColumn());

}

Hope helps a bit.

Thanks! Appreciate it

Hi Guys, facing an issue with Emf ImageType. I am using the following code -
worksheet.AutoFitRows();
worksheet.PageSetup.PrintArea = printArea;
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
worksheet.PageSetup.TopMargin = 0;
worksheet.PageSetup.BottomMargin = 0;

ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Emf,
};

SheetRender originalSr = new SheetRender(range.Worksheet, originalImgOptions);
using (MemoryStream originalMs = new MemoryStream())
{
originalSr.ToImage(0, originalMs);
originalImage = originalMs.ToArray();
}

the result looks like this-
image.png (8.7 KB)

if i look closely i can see small differences, the border lines for the last 2 rows are thicker, there is also an extra border wrapped around the table. The same issue happens with my chart as well, it ends up having font issues, look at ChartTitle in the below picture having no space in between etc.
image.png (15.2 KB)

my code for chart is straight forward-
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions()
{
ImageType = ImageType.Emf,
};
using (MemoryStream ms = new MemoryStream())
{
if (isChart)
{
((Aspose.Cells.Charts.Chart)chartOrShape).ToImage(ms, imgOptions);
}
else
{
((Aspose.Cells.Drawing.Shape)chartOrShape).ToImage(ms, imgOptions);
}
return ms.ToArray();
}

You can try out the same with the below excel file-
Book_1742996484.zip (1.3 MB)

Also I am using Aspose.Cells version 24.7

@Adhirath
chart (2).zip (4.8 KB)

We convert the chart to image on windows with 24.7. We can not find your mentioned issue.

the chart Title heading is cropped isn’t it?
image.png (3.5 KB)

@Adhirath
We have opened the following new ticket(s) for the border issues in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58194

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.

Is there a way to get an image with decent quality without changing the dimensions other than emf?

@Adhirath
image.png (16.5 KB)

It‘s fine without crop issue . If we convert chart to image as the following:

   ImageOrPrintOptions imgOptions = new ImageOrPrintOptions()
   {
       ImageType = ImageType.Emf,
   };
   workbook.Worksheets[0].Charts["CDMChart1743158159028"].ToImage(dir + "dest.Emf", imgOptions);

Please confirm “Aptos Narrow” font is installed on your server.

Yeah I don’t. Thank you for your help!

Can you send an emf photo for the range as well?

@Adhirath
1,sheet.zip (4.9 KB)

It’s the image of range “F6:K11”.
2,

Please set ImageOrPrintOptions.HorizontalResolution and ImageOrPrintOptions.VerticalResolution to improve quality.

I thought of that but it changes the proportions as well. Anyways, thanks a lot for your help!

@Adhirath
Setting the same higher horizontal and vertical resolution will result in larger pixels in the width and height of the image, while maintaining the same aspect ratio The physical size of the image (in unit of points) remains unchanged

thanks, will try that out

@Adhirath
You are welcome. Please take your time to try the suggested solutions. Hopefully, your issue will be sorted out. Please let us know your feedback.

1 Like

@Adhirath

Emf image is vector image, the line may be variable if zoom is different. Please check the attchement word document comparing Aspose generated Emf image and Excel Copied “As shown when printed” image.
EmfComparing.docx (21.4 KB)

Hi guys, i am facing another issue-
I am using aspose cells to take an image of a range, the image is not looking like the excel range exactly, it varies in column width, row width, overall image size etc. the code i use to get the range image is the following-
byte[]? originalImage = null;

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

ImageOrPrintOptions originalImgOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Emf,
};

SheetRender originalSr = new SheetRender(range.Worksheet, originalImgOptions);
using (MemoryStream originalMs = new MemoryStream())
{
originalSr.ToImage(0, originalMs);
originalImage = originalMs.ToArray();
}

return originalImage;

the excel range is under table 2 in this attachment -
Book_1742996484 (1).zip (2.2 MB)

and after pasting it look like this(top one) in comparison to a copy pasted image(below one)-
image.png (27.7 KB)

this is the print preview image-
image.png (2.9 KB)

for a better understanding have a look at the last page of this document-
Document_1744033388.zip (5.3 MB)

@Adhirath
Would you like to provide a runnable test code? If we could provide screenshots and highlight the error location, it would be very helpful for us to locate the issues. We will check it soon.

@Adhirath ,

There are two options when you “Copy as Picture” in Excel: “As shown on screen” and “As shown when printed”.
Excel_Copy_Picture.png (146.9 KB)

The image generated by the code you shared is close to the one by copying “As shown when printed” in Excel.
If you want the generated image to be close to the one by copying “As shown on screen” in Excel, please enable ImageOrPrintOptions.OnlyArea option.

Hi John, sorry this is as much code as I can share but it has all the logic for getting the image. I have provided the screenshots and also the source documents to show the difference. Let me know what will help you understand it better