How can I convert the entire Excel file into a single-page SVG without cutting off any content?

@amjad.sahi, @Professionalize.Discourse, @John.He, @simon.zhao

新規工事件名シート(1)_250702~260830 (23).zip (502.5 KB)

How can I convert the entire Excel file into a single-page SVG without cutting off any content?

Sample in zip (新規工事件名シート(1)_250702~260830 (23).zip) Excel SVG create time cut off issue.

using Aspose.Cells;

public class SetColumnWidthExample
{
    public static void Run()
    {
        Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.Worksheets[0];

		int firstRow = 0;
		int lastRow = worksheet.Cells.MaxDisplayRange.RowCount - 1;
		int firstCol = 0;
		int lastCol = worksheet.Cells.MaxDisplayRange.ColumnCount - 1;

		double totalHeightPx = 0;
		for (int i = firstRow; i <= lastRow; i++)
			totalHeightPx += worksheet.Cells.GetRowHeightPixel(i);

		double totalWidthPx = 0;
		for (int j = firstCol; j <= lastCol; j++)
			totalWidthPx += worksheet.Cells.GetColumnWidthPixel(j);

		double heightInInches = totalHeightPx / 96.0;
		double widthInInches = totalWidthPx / 96.0;

		worksheet.PageSetup.PaperSize = PaperSizeType.Custom;
		worksheet.PageSetup.CustomPaperSize(widthInInches, heightInInches);
		worksheet.PageSetup.Orientation = PageOrientationType.Portrait;

		// Save the workbook 			
		workbook.Save(newFilePath, SaveFormat.Svg);
    }
}


//Download Excle File 
[HttpGet]
public ActionResult Download(string fileName)
{
	//Get the temp folder and file path in server
	string fullPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
	byte[] fileByteArray = System.IO.File.ReadAllBytes(fullPath);
	bool fileExists = System.IO.File.Exists(fullPath);
	if (fileExists)
	{
		System.IO.File.Delete(fullPath);
	}
	return File(fileByteArray, "application/svg+xml", (fileName.Substring(0, fileName.LastIndexOf('_')) + ".svg"));
}

Sorry for the full logic shape not being provided for security reasons.

@imparthgalani

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!

How to set all content is on one page give example

@imparthgalani,

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)

Hope, this helps a bit.

1 Like

Taking lots of time when large Excel data. to create 500mb svg file how to reduce time

@imparthgalani,

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

worksheet.PageSetup.CustomPaperSize(widthInInches, heightInInches);

@imparthgalani,

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.

@imparthgalani ,

OnePagePerSheet is the most mathed option for your requirement. The width and height of outputed page is calculated by source file content internally.

Even you can get a custom paper size that can handle all the content in the source file, it will not take less time in this way.

But if any other way to get last use column offset left and row offset top of Excel with all different possible approach please give example of this.

@imparthgalani ,

Please try ImageOrPrintOptions.DrawObjectEventHandler, then get x,y offset of Cell.

//Print the coordinates and the value of Cell object
if (drawObject.Type == DrawObjectEnum.Cell)
{
    Console.WriteLine("[X]: " + x + " [Y]: " + y + " [Width]: " + width + " [Height]: " + height + " [Cell Value]: " + drawObject.Cell.StringValue);
}

@imparthgalani ,

Code:

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.