Converting/Extracting Excel Charts/Selection Ranges to an SVG Image

Hi, I am trying to evaluating your product(Aspose.Cells) for our company publishing needs.


Here is what my requirement:

1. Copy excel Selection/Chart and Convert it into as an SVG Image.

I see your product is supported all other image types(System.Drawing) except SVG.
And also I know you product is supporitng entire Excel sheet to convert as an SVG. But my requirement is very specific to the selecting one chart among 10 charts in one excel sheet and convert it into an SVG Image.

Do you think you have this facility any of your product or any plans for release. Please let me know.


Thanks,
Srini.

Hi,

Yes, sure you may do it using newer Aspose.Cells for Java versions (e.g Aspose.Cells for Java v7.7.0), see the document/ article for your reference:

Converting worksheet to SVG

For your requirements, you need to specify/ set your desired printable area (that should cover your desired chart) using PageSetup options before rendering the sheet to image files, see the document on setting printing options:

Page Setup and Printing Options

Thank you.

Thanks for the Prompt response. In order to copy the chart and convert to SVG setting page area is leading me to get incorrect charts as there are
two charts over-lapped in my source excel.

And also while saving selected area as SVG it looks like it is saving along with work sheet lenght(But removing other content which is good).
(attached examples & source excel)


Is there any way to take EMF image and convert it to as an SVG?

Here is what my exact requirement:
1. Copy the chart and convert it to an SVG Image.
a) There could be single chart selection-(This works with your library very well)
b) There could be two charts over lapped each other with in same cells but user selects only Once chart
2. Copy the Range selection and convert it to an SVG Image.


I think we are pretty close, if you can provide the guidance how can we fix the above problem we could talk further.

Thanks for the Prompt response. In order to copy the chart and convert to SVG setting page area is leading me to get incorrect charts as there are
two charts over-lapped in my source excel.

And also while saving selected area as SVG it looks like it is saving along with work sheet lenght(But removing other content which is good).
(attached examples & source excel)


Is there any way to take EMF image and convert it to as an SVG?

Here is what my exact requirement:
1. Copy the chart and convert it to an SVG Image.
a) There could be single chart selection-(This works with your library very well)
b) There could be two charts over lapped each other with in same cells but user selects only Once chart
2. Copy the Range selection and convert it to an SVG Image.


I think we are pretty close, if you can provide the guidance how can we fix the above problem we could talk further.



Hi,


You have a bit complicated scenario. I think you may try the following as we cannot directly save only the Chart to SVG image format using Chart.toImage() method.

1) Add a new Chart type worksheet to your workbook.
2) Get your desired chart in the charts list
3) Get the chart shape.
4) copy the chart shape to that worksheet.
5) Now follow the document/ article in my previous post to render the chart sheet to SVG image format for your needs.

See the sample code below for your needs for your reference:
e.g
Sample code:

Workbook workbook = new Workbook(“BookCharts.xls”);
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(1);
Worksheet chartSheet = worksheets.get(worksheets
.add(SheetType.CHART));
chartSheet.setName(“Chart”);
chartSheet.setSelected(true);

//Get the first Chart from the worksheet.
Chart source = sheet.getCharts().get(0);

Shape cshape = source.getChartObject();

//Copy the Chart to the Result Worksheet
chartSheet.getShapes().addCopy(cshape, 20, 0, 2, 0);


//Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setSaveFormat(SaveFormat.SVG);
imgOptions.setOnePagePerSheet(true);

SheetRender sr = new SheetRender(chartSheet, imgOptions);

//Output the worksheet into Svg image format
sr.toImage(0, “imgsvgtest.out.svg”);

Hope, this helps a bit for your requirements.

Also, I could not find your attached files in your previous post, may be you forgot to attach here. Anyways, the above procedure may suit your needs I think.

Thank you.



1. The above code not copying the chart to SVG. It just generating Sample SVG file with out chart.Attached sample(WithoutChart.out.svg).

Note: Attached all sample files in the zip file.

Here is the code that I have written:

// The path to the documents directory.
string dataDir = Path.GetFullPath("../../../Data/");

string filePath = dataDir + "chartest.xlsm";

//Create a workbook object from the template file
Workbook book = new Workbook(filePath);

//Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.SaveFormat = SaveFormat.SVG;
imgOptions.OnePagePerSheet = true;

WorksheetCollection wrkSheets = book.Worksheets;
Worksheet wrkBook = (Worksheet)book.Worksheets["Sheet1"];
Worksheet chartSheet = wrkSheets[wrkSheets.Add(SheetType.Chart)];
chartSheet.Name = "MyChart";
chartSheet.IsSelected = true;

//Get the first Chart from the worksheet.
Chart source = wrkBook.Charts[0];

Shape cshape = source.ChartObject;

//Copy the Chart to the Result Worksheet
chartSheet.Shapes.AddCopy(cshape, 20, 0, 2, 0);


SheetRender sr = new SheetRender(chartSheet, imgOptions);

for (int i = 0; i < sr.PageCount; i++)
{

//Output the worksheet into Svg image format
sr.ToImage(i, filePath + chartSheet.Name + i + System.DateTime.Now.ToFileTime() + ".out.svg");
}


2. Secondly, while saving selected area as SVG it looks like it is saving along with entire work sheet length. In the SVG output it is generating extra spaces other than expected Image.
Attached the sample(ChartwithExtraSpace.out.svg). (We would like to display the converted SVG Image on other website that time it is rendering with extra space as same Excel Sheet length)

Here is the code that I have written:

// The path to the documents directory.
string dataDir = Path.GetFullPath("../../../Data/");

string filePath = dataDir + "Template.xlsx";

//Create a workbook object from the template file
Workbook book = new Workbook(filePath);

//Convert each worksheet into svg format in a single page.
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.SaveFormat = SaveFormat.SVG;
imgOptions.OnePagePerSheet = true;


//Convert each worksheet into svg format
foreach (Worksheet sheet in book.Worksheets)
{

//Obtaining the reference of the PageSetup of the worksheet
PageSetup pageSetup = sheet.PageSetup;

//Specifying the cells range (from A1 cell to T35 cell) of the print area
pageSetup.PrintArea = "A13:H31";

SheetRender sr = new SheetRender(sheet, imgOptions);

for (int i = 0; i < sr.PageCount; i++)
{
//Output the worksheet into Svg image format
sr.ToImage(i, filePath + sheet.Name + i + System.DateTime.Now.ToFileTime() + ".out.svg");
}
}

Hi,

Thanks for the template file.

  1. Well, you need to specify the parameters of AddCopy() method accordingly to render the chart properly, the reason is your chart has more width on the sheet. I have updated the code segment and it works fine and the chart is rendered fine in the output SVG file format.

e.g

Sample code:

//Create a workbook object from the template file

Workbook book = new Workbook(filePath);

//Convert each worksheet into svg format in a single page.

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.SaveFormat = SaveFormat.SVG;

//imgOptions.OnePagePerSheet = true;

imgOptions.OnlyArea = true;

WorksheetCollection wrkSheets = book.Worksheets;

Worksheet wrkBook = (Worksheet)book.Worksheets[“Sheet1”];

Worksheet chartSheet = wrkSheets[wrkSheets.Add(SheetType.Chart)];

chartSheet.Name = “MyChart”;

chartSheet.IsSelected = true;

//Get the first Chart from the worksheet.

Chart source = wrkBook.Charts[0];

Shape cshape = source.ChartObject;

//Copy the Chart to the Result Worksheet

**chartSheet.Shapes.AddCopy(cshape, 0, 70, 2, 80);**

SheetRender sr = new SheetRender(chartSheet, imgOptions);

for (int i = 0; i < sr.PageCount; i++)

{

//Output the worksheet into Svg image format

sr.ToImage(i, filePath + chartSheet.Name + i + System.DateTime.Now.ToFileTime() + “.out.svg”);

}
  1. Well, it might be difficult to cope with extra space around the rendered chart, anyways, could you attach your template file here, we will check it soon.

Thank you.

Save Edit

Hi,

Attached the examples. And also sending the Expected SVG Image and what Aspose is producing.

Additionally, When I copy the Cells area from Template file using cell range the converted to SVG is showing as fading some colors in the chart lines. (Attached the sample).

And also the SVG Image is not same as the one I copied from the Excel.

Any worng with the code and any solution?

// Convert selections AREA to SVG.

public static void Test3()

{

// The path to the documents directory.

string dataDir = Path.GetFullPath("…/…/…/Data/");

string filePath = dataDir + “Template.xlsx”;

//Create a workbook object from the template file

Workbook book = new Workbook(filePath);

//Convert each worksheet into svg format in a single page.

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.SaveFormat = SaveFormat.SVG;

imgOptions.OnePagePerSheet = true;

//Convert each worksheet into svg format

foreach (Worksheet sheet in book.Worksheets)

{

//Obtaining the reference of the PageSetup of the worksheet

PageSetup pageSetup = sheet.PageSetup;

//Specifying the cells range (from A13 cell to H31 cell) of the print area

pageSetup.PrintArea = “A13:H31”;

SheetRender sr = new SheetRender(sheet, imgOptions);

//sheet.SelectRange(2, 1, 10, 7, true);

for (int i = 0; i < sr.PageCount; i++)

{

//Output the worksheet into Svg image format

sr.ToImage(i, filePath + sheet.Name + i + System.DateTime.Now.ToFileTime() + “.out.svg”);

}

}

}

Hi Srinivas,

Thanks for your posting and using Aspose.Cells.

I was able to notice the faded conversion issue. However, I have logged a New Feature request of converting chart to image in SVG format directly in our database. We will look into it and implement it if possible. Once, there is some news or fix for you, we will let you know asap.

This issue has been logged as CELLSNET-42303.

Hi Faiz,

Is this feature now available in library? I am also looking for a solution that will export a given chart or range from Excel to SVG.

Regards,
Shamit

Hi Shamit,


Thank you for considering Aspose APIs.

You can save a range of cells in SVG format using the following piece of code.

C#

Workbook workbook = new Workbook(f1)
Worksheet sheet = workbook.Worksheets[0];
sheet.PageSetup.PrintArea = “A1:E4”;

ImageOrPrintOptions options = new ImageOrPrintOptions();
options.SaveFormat = SaveFormat.SVG;

SheetRender render = new SheetRender(sheet, options);
render.ToImage(0, “D:/temp/output.svg”);

Please note, we are setting Worksheet's PrintArea to render a specific range of cells at a time. You can modify the code to select the range of your own choice.

Saving the Chart to SVG format isn't working as expected so we are looking into this matter, and will keep you posted with updates in this regard.

Hi,

Please try our latest fix/version: Aspose.Cells for .NET (Latest Version)

We have supported the feature for creating SVG formatted chart’s image.

Let us know your feedback.

Thank you.

The issues you have found earlier (filed as CELLSNET-42303) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi,

Thanks for using Aspose.Cells.

Please refer to this article that explains how to use Aspose.Cells to convert Chart into SVG image.