To convert an entire Excel file into a single-page SVG without cutting off any content, you can utilize the ImageOrPrintOptions class in Aspose.Cells. Specifically, you should set the OnePagePerSheet property to true to ensure that all content from each worksheet is rendered on a single page.
Here’s a sample code snippet to help you achieve this:
using Aspose.Cells;
public class ConvertExcelToSVG
{
public static void Run(string filePath, string outputDir)
{
// Load the workbook
Workbook workbook = new Workbook(filePath);
// Create ImageOrPrintOptions and set the save format to SVG
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.SetSaveFormat(SaveFormat.SVG);
imgOptions.OnePagePerSheet = true; // Ensure all content is on one page
// Iterate through each worksheet and convert to SVG
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet sheet = workbook.Worksheets[i];
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int k = 0; k < sr.PageCount; k++)
{
// Save the SVG file
sr.ToImage(k, $"{outputDir}/{sheet.Name}_out.svg");
}
}
// Print completion message
Console.WriteLine("Excel to SVG conversion completed successfully.");
}
}
To implement this solution, replace filePath with the path to your Excel file and outputDir with the directory where you want to save the SVG files. This code will ensure that each worksheet is converted to a single SVG file without cutting off any content.
If you have any further questions or need additional assistance, please let me know!
You need to use ImageOrPrintOptions.OnePagePerSheet Boolean attribute to set to “true” to render all contents in single page of SVG file. Please refer to the following sample code that I tried/tested using your template Excel file and it works fine. It gives me SVG file with all the contents on single page and no content/data is clipped.
Workbook workbook = new Workbook("e:\\test2\\ÉVïKìHÄûîÅû+âVü[âg(1)_250702ü`260830.xlsx");
Worksheet worksheet = workbook.Worksheets["Sheet1"];
var imageOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Svg,
};
var sheetRender = new SheetRender(worksheet, imageOptions);
sheetRender.ToImage(0, "e:\\test2\\out_net12.svg");
Please find attached the output SVG file for your reference. out_net12.zip (424.9 KB)
Kindly note that rendering large datasets and other objects onto a single page may require additional time. The OnePagePerSheet method can be time-consuming, especially when dealing with extensive lists of data/contents or numerous objects. So, this is a normal occurrence.
var imageOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Svg,
};
var sheetRender = new SheetRender(worksheet, imageOptions);
Without using above code how to possible like get all content width and height after set as CustomPaperSize this will take less time
Fitting large contents onto a single page by adjusting paper size may not work , even in MS Excel (manually). However, could you try setting a custom (large) page manually in Excel manually if it works, Aspose.Cells should handle it too as Aspose.Cells follows MS Excel standards and specifications.
internal class LastCellOffsetHandler : DrawObjectEventHandler
{
int rowIndex = -1, columnIndex = -1;
//offset in point
float offsetX = 0, offsetY = 0;
//cell width, height in point
float cellWidth = 0, cellHeight = 0;
public override void Draw(DrawObject drawObject, float x, float y, float width, float height)
{
if (drawObject.Type == DrawObjectEnum.Cell)
{
if (rowIndex < drawObject.Cell.Row)
{
rowIndex = drawObject.Cell.Row;
offsetY = y;
cellHeight = height;
}
if (columnIndex < drawObject.Cell.Column)
{
columnIndex = drawObject.Cell.Column;
offsetX = x;
cellWidth = width;
}
}
}
public void Print()
{
Console.WriteLine($"Last Cell info: Row={rowIndex}, Column={columnIndex}, offsetX={offsetX}, offsetY={offsetY}, width={cellWidth}, height={cellHeight}");
}
}
Workbook workbook = new Workbook("input.xlsx");
Worksheet worksheet = workbook.Worksheets["Sheet1"];
LastCellOffsetHandler lastCellOffsetHander = new LastCellOffsetHandler();
var imageOptions = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Svg,
DrawObjectEventHandler = lastCellOffsetHander,
};
var sheetRender = new SheetRender(worksheet, imageOptions);
sheetRender.ToImage(0, "output.svg");
lastCellOffsetHander.Print();
Please note, the DrawObjectEventHandler only outputs info of Cell that has value. So the last column and the last row must have at least one cell has value, otherwise the info will be not right.