Convert Excel to Pdf, Image and other formats

Convert Excel Workbook to PDF

PDF files are widely used to exchange documents between organizations, government sectors, and individuals. It is a standard document format and software developers are often asked to find a way to convert Microsoft Excel files into PDF documents.

Aspose.Cells supports converting Excel files to PDF and maintains high visual fidelity in the conversion.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiate the Workbook object
// Open an Excel file
Workbook workbook = new Workbook("Book1.xlsx");
// Save the document in PDF format
workbook.Save("output.pdf");

Convert Excel Workbook to JPG

Aspose.Cells supports converting Excel files to JPG. The code example below shows how to save a workbook as JPG.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open a template excel file
Workbook book = new Workbook("Book1.xlsx");
//Convert workbook to JPG image.
book.Save("Image1.jpg");

Convert Excel Workbook to Image

Aspose.Cells supports converting Excel files to images. The code example below shows how to save a workbook as images.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open a template excel file
Workbook book = new Workbook("Book1.xlsx");
//Convert workbook to BMP image.
book.Save("Image1.bmp");
//Convert workbook to JPG image.
book.Save("Image1.jpg");
//Convert workbook to Png image.
book.Save("Image1.png");
//Convert workbook to EMF image.
book.Save("Image1.emf");
//Convert workbook to GIF image.
book.Save("Image1.gif");

Converting Excel Workbook to XPS

The XPS document format consists of structured XML markup that defines the layout of a document and the visual appearance of each page, along with rendering rules for distributing, archiving, rendering, processing, and printing documents.

The markup language for XPS is a subset of XAML which allows it to incorporate vector graphics elements in documents, using XAML to mark up the Windows Presentation Foundation (WPF) primitives. The elements used are described in terms of paths and other geometrical primitives.

An XPS file is, in fact, a unicode ZIP archive using the Open Packaging Conventions, containing the files which make up the document. These include an XML markup file for each page, text, embedded fonts, raster images, 2D vector graphics, as well as the digital rights management information. The contents of an XPS file can be examined simply by opening it in an application that supports ZIP files.

From Aspose.Cells 6.0.0, Microsoft Excel to XPS conversion is supported.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open an Excel file
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Book1.xls");
// Get the first worksheet
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
// Apply different Image and Print options
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
// Set the Format
options.SaveFormat = SaveFormat.Xps;
// Render the sheet with respect to specified printing options
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);
// Save
sr.ToImage(0, dataDir + "out_printingxps.out.xps");
// Export the whole workbook to XPS
workbook.Save(dataDir + "out_whole_printingxps.out.xps", new XpsSaveOptions());

Convert Excel to Ods,Sxc and Fods (OpenOffice / LibreOffice Calc)

Aspose.Cells supports converting Excel files to Ods,Sxc and Fods files . The code example below shows how to convert the tempalte to Ods,Sxc and Fods file.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load your source workbook
Workbook workbook = new Workbook("book1.xlsx");
// Save as ods file
workbook.Save("Out.ods");
// Save as sxc file
workbook.Save("Out.sxc");
// Save as fods file
workbook.Save("Out.fods");

Converting Excel Workbook to MHTML Files

MHTML combines normal HTML with external resources (that is, content that is usually linked in, like images, animations, audio, and so on) into one file. They are used for emails with the .mht file extension.

Aspose.Cells supports reading and writing MHTML files.

The code example below shows how to save a workbook as an MHTML file.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the file path
string filePath = dataDir + "Book1.xlsx";
// Specify the HTML Saving Options
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.MHtml);
// Instantiate a workbook and open the template XLSX file
Workbook wb = new Workbook(filePath);
// Save the MHT file
wb.Save(filePath + ".out.mht", sv);

Converting Excel Workbook to HTML

The Aspose.Cells API provides support for exporting spreadsheets to HTML format. For this purpose, Aspose.Cells uses the HtmlSaveOptions class to provide the flexibility to control several aspects of the output HTML.

The code example below shows how to save a workbook as an HTML file.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the file path
string filePath = dataDir + "sample.xlsx";
// Load your sample excel file in a workbook object
Workbook wb = new Workbook(filePath);
// Save it in HTML format
wb.Save(dataDir + "ConvertingToHTMLFiles_out.html", SaveFormat.Html);

Setting the Image Preferences for HTML

Starting from 8.0.2, Aspose.Cells has exposed ImageOptions for the HtmlSaveOptions class, allowing developers to specify image preferences when saving spreadsheets to HTML format.

Below are details of some of the image settings that can be applied,

  • ImageType: Specifies the image type. Please note, all shapes, including charts, render as images in the output HTML.
  • SmoothingMode: Specifies the anti-aliasing for lines, curves & edges of filled areas.
  • TextRenderingHint: Specifies the quality of text rendering.
  • Quality: Specifies the quality of the image between 0 to 100, when ImageType is specified as Jpeg.
  • VerticalResolution: Gets or sets the vertical resolution of the image in dots per inch.
  • HorizontalResolution: Gets or sets the horizontal resolution of the image in dots per inch.
  • TiffCompression: Gets or sets the compression type for the images when ImageType is specified as Tiff.
  • Transparent: Indicates if the background of an image should be transparent when ImageFormat is specified as Png.

The code below demonstrates how to use HtmlSaveOptions.ImageOptions to specify different preferences.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the file path
string filePath = dataDir + "Book1.xlsx";
// Load a spreadsheet to be converted
Workbook book = new Workbook(filePath);
// Create an instance of HtmlSaveOptions
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.Html);
// Set the ImageFormat to PNG
saveOptions.ImageOptions.ImageType = Drawing.ImageType.Png;
// Set SmoothingMode to AntiAlias
saveOptions.ImageOptions.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Set TextRenderingHint to AntiAlias
saveOptions.ImageOptions.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// Save spreadsheet to HTML while passing object of HtmlSaveOptions
book.Save( dataDir + "output.html", saveOptions);

Convert Excel Workbook to Markdown

The Aspose.Cells API provides support for exporting spreadsheets to Markdown format. To export the active worksheet to Markdown, pass SaveFormat.Markdown as the second parameter of Workbook.Save method. You may also use MarkdownSaveOptions class to specify additional settings for exporting worksheet to Markdown.

The following code example demonstrates exporting active worksheet to Markdown by using SaveFormat.Markdown enumeration member. Please see the output Markdown file generated by the code for reference.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
// Save as Markdown
workbook.Save(outputDir + "Book1.md", SaveFormat.Markdown);

Convert Excel Workbook to JSON

Aspose.Cells supports converting a workbook to Json(JavaScript Object Notation) file.

The following code example demonstrates exporting active worksheet to Json by using SaveFormat.Json enumeration member. Please see the code to convert source file to the output Json file generated by the code for reference.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load your source workbook
Workbook workbook = new Workbook("Book1.xlsx");
// convert the workbook to json file.
workbook.Save(dir + "book1.json");

Convert Excel to XML

Aspose.Cells supports converting a workbook to Excel 2003 Spreadsheet XML and plain XML data.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Load your source workbook
Workbook workbook = new Workbook("Book1.xlsx");
//Save as Excel 2003 Spreadsheet XML
workbook.Save("Spreadsheet.xml");
//Save as plain XML data
XmlSaveOptions xmlSaveOptions = new XmlSaveOptions();
workbook.Save("data.xml", xmlSaveOptions);

Convert Excel Workbook to TIFF

Aspose.Cells supports converting a workbook to TIFF file.

The code snippet below shows how to convert Excel to TIFF:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open a template excel file
Workbook book = new Workbook("Book1.xlsx");
//save file to tiff
book.Save("out.tiff");

Convert Excel Workbook to DOCX

The Aspose.Cells API provides support for converting spreadsheets to DOCX format. To export the workbook to DOCX, pass SaveFormat.Docx as the second parameter of Workbook.Save method. You may also use DocxSaveOptions class to specify additional settings for exporting worksheet to DOCX.

The following code example demonstrates exporting active worksheet to DOCX by using SaveFormat.Docx enumeration member. Please see the output DOCX file generated by the code for reference.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
// Save as Markdown
workbook.Save(outputDir + "Book1.docx", SaveFormat.Docx);

Convert Excel Workbook to PPTX

The Aspose.Cells API provides support for converting spreadsheets to PPTX format. To export the workbook to PPTX, pass SaveFormat.Pptx as the second parameter of Workbook.Save method. You may also use PptxSaveOptions class to specify additional settings for exporting worksheet to PPTX.

The following code example demonstrates exporting active worksheet to PPTX by using SaveFormat.Pptx enumeration member. Please see the output PPTX file generated by the code for reference.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
// Save as Markdown
workbook.Save(outputDir + "Book1.pptx", SaveFormat.Pptx);

Convert Excel Workbook to EPUB

The Aspose.Cells API provides support for converting spreadsheets to EPUB format. To export the workbook to EPUB, pass SaveFormat.Epub as the second parameter of Workbook.Save method. You may also use EBookSaveOptions class to specify additional settings for exporting worksheet to Epub.

The following code example demonstrates exporting active worksheet to EPUB by using SaveFormat.Epub enumeration member.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the file path
string filePath = dataDir + "sample.xlsx";
// Load your sample excel file in a workbook object
Workbook wb = new Workbook(filePath);
// Save it in EPUB format
wb.Save(dataDir + "ConvertingToEPUBFiles_out.epub", SaveFormat.Epub);

Convert Excel Workbook to AZW3

The Aspose.Cells API provides support for converting spreadsheets to AZW3 format. To export the workbook to AZW3, pass SaveFormat.Azw3 as the second parameter of Workbook.Save method. You may also use EBookSaveOptions class to specify additional settings for exporting worksheet to AZW3.

The following code example demonstrates exporting active worksheet to AZW3 by using SaveFormat.Azw3 enumeration member.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the file path
string filePath = dataDir + "sample.xlsx";
// Load your sample excel file in a workbook object
Workbook wb = new Workbook(filePath);
// Save it in AZW3 format
wb.Save(dataDir + "ConvertingToEPUBFiles_out.azw3", SaveFormat.Azw3);

Advance topics

  • Convert Revision of XLSB to XLSM
  • HTML
  • Image
  • Json
  • Convert Excel workbook to Ods,Sxc and Fods (OpenOffice / LibreOffice calc).
  • Pdf
  • Convert Excel to CSV,TSV and Txt
  • Track Document Conversion Progress
  • Convert CSV, TSV and TXT to Excel