Conversion (25)
XPS (XML Paper Specification) is a specification for a page description language and a fixed-document format developed by Microsoft. It defines the layout of a document and the visual appearance of each page. Sometimes you may need to convert an Excel document to XPS for distribution, archiving or printing purposes, and this article will demonstrate how to accomplish this task programmatically using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert Excel to XPS
Spire.XLS for .NET allows you to convert Excel (.xls/ .xlsx) to XPS with only three lines of code. The detailed steps are as follows.
- Create a Workbook object.
- Load a sample Excel document using Workbook.LoadFromFile() method.
- Convert the Excel document to XPS using Workbook.SaveToFile(String, FileFormat) method.
- C#
- VB.NET
using Spire.Xls; namespace ExceltoXPS { class Program { static void Main(string[] args) { //Create a Workbook object. Workbook workbook = new Workbook(); //Load a sample Excel document workbook.LoadFromFile(@"E:\Files\\sample0.xlsx", ExcelVersion.Version2010); //Convert the document to XPS workbook.SaveToFile("result.xps", FileFormat.XPS); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Embed image in HTML when converting Excel to HTML in C#
2014-08-15 08:02:33 Written by support iceblueIf you created a pretty Excel table and now want to publish it online as a web page, the simplest way is to export it to an old good HTML file. However a problem may occur if you just simply transform image in Excel to HTML code with a relative link (URL). This way, your web page may no longer display properly on client machines since the image can't be reached through that URL on client-side. In this article, we’re going to resolve this issue by embedding image in HTML code when converting Excel to HTML.
Here is an Excel table with some images embedded in.
We're able to convert this Excel file to HTML by following below code snippet:
Step 1: Create a new instance of workbook.
Workbook book = new Workbook(); book.LoadFromFile("Book1.xlsx");
Step 2: Embed images into HTML code using Data URI scheme.
HTMLOptions options = new HTMLOptions(); options.ImageEmbedded = true;
Step 3: Save the worksheet to HTML.
book.Worksheets[0].SaveToHtml("sample.html", options); System.Diagnostics.Process.Start("sample.html");
Output:
HTML Code:
Since the HTML code is too long to be displayed here, we have to present it by a screenshot.
Full C# Code:
using Spire.Xls; using Spire.Xls.Core.Spreadsheet; namespace CreateWorkbook { class Program { static void Main(string[] args) { // create Workbook instance and load file Workbook book = new Workbook(); book.LoadFromFile("Book1.xlsx"); // embed image into html when converting HTMLOptions options = new HTMLOptions(); options.ImageEmbedded = true; // save the sheet to html book.Worksheets[0].SaveToHtml("sample.html", options); System.Diagnostics.Process.Start("sample.html"); } } }
XLS and XLSX are two different file formats for Microsoft Excel spreadsheets. XLS is the default file format for Microsoft Excel 2003 and earlier versions, while XLSX is the default file format for Microsoft Excel 2007 and later versions. In some cases, developers may need to convert between Excel XLS and XLSX file formats. In this article, we will explain how to convert XLS to XLSX or XLSX to XLS in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert XLS to XLSX in C# and VB.NET
The following are the steps to convert an XLS file to XLSX format using Spire.XLS for .NET:
- Create a Workbook instance.
- Load the XLS file using Workbook.LoadFromFile() method.
- Save the XLS file to XLSX format using Workbook.SaveToFile(string, ExcelVersion) method.
- C#
- VB.NET
using Spire.Xls; namespace ConvertXlsToXlsx { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an XLS file workbook.LoadFromFile("Input.xls"); //Convert the file to XLSX format workbook.SaveToFile("ToXlsx.xlsx", ExcelVersion.Version2016); } } }
Convert XLSX to XLS in C# and VB.NET
The following are the steps to convert an XLSX file to XLS format using Spire.XLS for .NET:
- Create a Workbook instance.
- Load the XLSX file using Workbook.LoadFromFile() method.
- Save the XLSX file to XLS format using Workbook.SaveToFile(string, ExcelVersion) method.
- C#
- VB.NET
using Spire.Xls; namespace ConvertXlsxToXls { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an XLSX file workbook.LoadFromFile("Input.xlsx"); //Convert the file to XLS format workbook.SaveToFile("ToXls.xls", ExcelVersion.Version97to2003); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Converting Excel spreadsheets to PDF files is a common task for many users. PDFs maintain the professional formatting and layout of your spreadsheets, allowing you to share your work securely and reliably. The conversion process is straightforward, giving you an easy way to distribute Excel files without compromising the original content and appearance.
This guide will walk you through the steps to convert an Excel spreadsheet to a PDF file, including how to apply specific configurations, using C# and the Spire.XLS for .NET library.
- Convert a Worksheet to PDF with Default Page Settings in C#
- Fit Sheet on One Page while Converting a Worksheet to PDF in C#
- Customize Page Margins while Converting a Worksheet to PDF in C#
- Specify Page Size while Converting a Worksheet to PDF in C#
- Preserve Gridlines while Converting a Worksheet to PDF in C#
- Specify Page Orientation while Converting a Worksheet to PDF in C#
- Convert a Cell Range to PDF in C#
- Convert an Entire Workbook to PDF in C#
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert a Worksheet to PDF with Default Page Settings in C#
Page settings control how your worksheet prints or displays when converted to different formats. These settings let you manage orientation, paper size, margins, scaling, and more. If you've already set the desired page settings for your worksheet, you can convert it to PDF using the Worksheet.SaveToPdf() method.
The following are the steps to convert a worksheet to PDF with the default page settings.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Convert it to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace ConvertExcelToPdfWithDefaultPageSettings { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Convert it to a PDF file sheet.SaveToPdf("WorksheetToPdf.pdf"); // Dispose resources workbook.Dispose(); } } }
Fit Sheet on One Page while Converting a Worksheet to PDF in C#
Fitting the content onto a single page enhances the readability and allows viewers to take in the entire dataset at a glance, improving their understanding. To ensure that your worksheet content fits neatly on a single page when converting it to a PDF format, you need to set the Workbook.ConverterSetting.SheetFitToPage property to true.
The following are the detailed steps.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Convert it to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace FitOnOnePage { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Convert the worksheet to a PDF file sheet.SaveToPdf("FitOnePage.pdf"); // Dispose resources workbook.Dispose(); } } }
Customize Page Margins while Converting a Worksheet to PDF in C#
Adjusting the page margin settings allows you to control the spacing between the content and the edges of the PDF page. The PageSetup class can be used to work with various page settings, including margins.
The steps to customize the page margins while converting a worksheet to PDF are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the top, bottom, right, left margins through TopMargin, BottomMargin, RightMargin, LeftMargin properties of the PageSetup object.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace CustomizePageMargin { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set page margins pageSetup.TopMargin = 0.5; pageSetup.BottomMargin = 0.5; pageSetup.LeftMargin = 0.3; pageSetup.RightMargin = 0.3; // Convert it to a PDF file sheet.SaveToPdf("CustomizeMargin.pdf"); // Dispose resources workbook.Dispose(); } } }
Specify Page Size while Converting a Worksheet to PDF in C#
The PageSetup class includes the PaperSize property, which you can use to define the page size when converting a worksheet to PDF. This property enables you to specify standard paper sizes like A3, A4, B4, B5, or even a custom paper size to meet your specific requirements.
The following are the steps to specify page size while converting a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Fit worksheet on one page without changing the paper size by setting Workbook.ConverterSetting.SheetFitToPageRetainPaperSize to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the paper size through PageSetup.PaperSize property.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace SpecifyPageSize { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Fit sheet on one page while retaining paper size workbook.ConverterSetting.SheetFitToPageRetainPaperSize = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set the page size to A4 pageSetup.PaperSize = PaperSizeType.PaperA4; // Convert the worksheet to a PDF file sheet.SaveToPdf("SpecifyPageSize.pdf"); // Dispose resources workbook.Dispose(); } } }
Preserve Gridlines while Converting a Worksheet to PDF in C#
By preserving the gridlines during the conversion process, you can create PDF versions of your worksheets that closely mirror the original layout and enhance the overall usability and understanding of the data. To preserve the gridlines, set the PageSetup.IsPrintGridlines property to true.
The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Preserve gridlines by setting PageSetup.IsPrintGridlines property to true.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace PreserveGridlines { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Preserve gridlines pageSetup.IsPrintGridlines = true; // Convert it to a PDF file sheet.SaveToPdf("PreserveGridlines.pdf"); // Dispose resources workbook.Dispose(); } } }
Specify Page Orientation while Converting a Worksheet to PDF in C#
Selecting the appropriate orientation ensures the data, text, and other elements are properly displayed without being cropped or stretched. To specify the page orientation, you can use the PageSetup.Orientation property.
The following are the steps to specify page orientation while converting a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the page orientation through PageSetup.Orientation property.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace SpecifyPageOrientation { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set page orientation to Landscape or Portrait pageSetup.Orientation = PageOrientationType.Landscape; // Convert it to a PDF file sheet.SaveToPdf("PageOrientation.pdf"); // Dispose resources workbook.Dispose(); } } }
Convert a Cell Range to PDF in C#
By converting only the necessary cell range, you can create a PDF that highlights the most critical data, insights, or information, without including extraneous content. To specify a cell range for conversion, you can use the PageSetup.PrintArea property.
The following are the steps to convert a call range of a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the cell range to be converted through PageSetup.PrintArea property.
- Convert the cell range to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace CellRangeToPdf { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set print area pageSetup.PrintArea = "A13:D22"; // Convert it to a PDF file sheet.SaveToPdf("CellRangeToPdf.pdf"); // Dispose resources workbook.Dispose(); } } }
Convert an Entire Workbook to PDF in C#
Converting an entire workbook to a single PDF, with each worksheet represented as a separate page, can offer benefits in terms of comprehensive reporting, streamlined distribution, preserving workbook structure, and improved document management.
To convert a workbook to a single PDF file, you can use the Workbook.SaveToFile() method. Before conversion, you may also need to configure the convert options and page settings.
The following are the steps to convert an entire worksheet to PDF using Spire.XLS.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Set the convert options through Workbook.ConverterSetting property.
- Iterate through each worksheet in the workbook, and configure the page settings through Worksheet.PageSetup property.
- Convert the workbook to PDF using Workbook.SaveToFile() method.
- C#
using Spire.Xls; namespace CellRangeToPdf { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit each sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Fit sheet on one page while retaining paper size workbook.ConverterSetting.SheetFitToPageRetainPaperSize = true; // Iterate through the worksheets for (int i = 0; i < workbook.Worksheets.Count; i++) { // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set the page size to A4 pageSetup.PaperSize = PaperSizeType.PaperA4; } // Convert the workbook to PDF workbook.SaveToFile("WorkbookToPdf.pdf", FileFormat.PDF); // Dispose resources workbook.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Compared with Excel files, text files are easier to read and take up less memory as they contain only plain text data without any formatting or complex structure. Therefore, in certain situations where simplicity and efficiency are required, converting Excel files to text files can be beneficial. This article will demonstrate how to programmatically convert Excel to TXT format using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert Excel to TXT in C# and VB.NET
Spire.XLS for .NET offers the Worksheet.SaveToFile(string fileName, string separator, Encoding encoding) method to convert a specified worksheet to a txt file. The following are the detailed steps.
- Create a Workbook instance.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet by its index using Workbook.Worksheets[sheetIndex] property.
- Convert the Excel worksheet to a TXT file using Worksheet.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; using System.Text; namespace ExcelToTXT { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile("sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Save the worksheet as a txt file sheet.SaveToFile("ExceltoTxt.txt", " ", Encoding.UTF8); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A DataTable represents a table of in-memory relational data. It can be populated from a data source like Microsoft SQL Server or from a file like CSV or Excel. In this article, you will learn how to populate DataTable from CSV, or in other words, how to convert CSV to DataTable in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert CSV to DataTable in C# and VB.NET
The following are the main steps to convert CSV to DataTable:
- Initialize an instance of Workbook class.
- Load a CSV file using Workbook.LoadFromFile() method and passing the file path and the delimiter/separator of the CSV file in the form of string as parameters.
- Get the desired worksheet by its index (zero-based) through Workbook.Worksheets[sheetIndex] property.
- Export data from the worksheet to a DataTable using Worksheet.ExportDataTable() method.
(The ExportDataTable() method has several overloads that can be used to control how the data will be exported, for example, ExportDataTable(CellRange range, bool exportColumnNames, bool computedFormulaValue): this overload allows you to specify the range to be exported along with whether to export columns names and calculated values of formulas.
- C#
- VB.NET
using Spire.Xls; using System; using System.Data; using System.Windows.Forms; namespace ConvertCsvToExcel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load a CSV file workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.csv", ","); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Export data from the worksheet to a DataTable DataTable dt = worksheet.ExportDataTable(); //This overload enables you to specify the range to be exported along with whether to export column names and calculated values of formulas //DataTable dt = worksheet.ExportDataTable(worksheet.Range["A1:C10"], true, true); //Show the DataTable in a DataGridView control (optional) dataGridView1.DataSource = dt; } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A CSV (Comma Separated Values) file is a plain text file that contains data separated by commas. It is widely used to import or export data from one application to another. In some cases, you might need to do conversions between CSV and Excel. In this article, you will learn how to implement this function programmatically in C# and VB.NET using Spire.XLS for .NET library.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert Excel to CSV in C# and VB.NET
The following are the steps to convert Excel to CSV:
- Create an instance of Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet by its index using Workbook.Worksheets[index] property.
- Save the worksheet as CSV using XlsWorksheet.SaveToFile() method. You can choose one of the following overloaded SaveToFile() methods:
- SaveToFile(string fileName, string separator)
- SaveToFile(string fileName, string separator, Encoding encoding)
- SaveToFile(string fileName, string separator, bool retainHiddenData)
- C#
- VB.NET
using Spire.Xls; using System.Text; namespace ConvertAWorksheetToCsv { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Save the worksheet as CSV sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8); } } }
Convert CSV to Excel in C# and VB.NET
The following are the steps to convert CSV to Excel:
- Create an instance of Workbook class.
- Load a CSV file using Workbook.LoadFromFile(string fileName, string separator, int startRow, int startColumn) method.
- Get the desired worksheet by its index using Workbook.Worksheets[index] property.
- Access the used range of the worksheet using Worksheet.AllocatedRange property. Then set CellRange.IgnoreErrorOptions property as IgnoreErrorType.NumberAsText to ignore possible errors while saving the numbers in the range as text.
- Autofit columns and rows using CellRange.AutoFitColumns() and CellRange.AutoFitRows() methods.
- Save the CSV to Excel using Workbook.SaveToFile(string fileName, ExcelVersion version) method.
- C#
- VB.NET
using Spire.Xls; namespace ConvertCsvToExcel { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load a CSV file workbook.LoadFromFile(@"ExcelToCSV.csv", ",", 1, 1); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Access the used range in the worksheet CellRange usedRange = sheet.AllocatedRange; //Ignore errors when saving numbers in the range as text usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText; //Autofit columns and rows usedRange.AutoFitColumns(); usedRange.AutoFitRows(); //Save the result file workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This section aims at providing a detail solution for developers on how to convert specific worksheet cells to image via this .NET Excel component Spire.XLS for .NET in C#, VB.NET. This Excel library helps us quickly convert certain excel cells to different image formats such as jpeg, png, bmp, tiff, gif, ico, emf, exif etc.
When we convert worksheet to image, Spire.XLS for .NET provides us a method: Spire.Xls.Worksheet.SaveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn); As we see, there are four parameters passed in this method. These four parameters determine the certain range of cells. After deciding the cell range, we can successfully convert cells to image. Now, let us see the whole task step by step.
Step 1: Create a template Excel Workbook in MS Excel
I create a new Excel file in MS Excel and add some data which have different formats in the first sheet, here is a screenshot of created file.
Step 2: Download and Install Spire.XLS for .NET
Spire.XLS for .NET is an Excel Api which enables users to quickly generate, read, edit and manipulate Excel files on .NET Platform. Here you can download Spire.XLS for .NET and install it on your development computer. After installing it, Spire.XLS for .NET will run in evaluation mode which is the same when you install other Spire components. This evaluation mode has no time limit.
Step 3: Create a project and Add References
We can create a new project of Console Application either in C# or VB.NET. I create in C#, but we can choose VB.NET too.
In this project, we need to add references in our project. Apart from adding System.Drawing, we will use Spire.XLS for .NET, so we also have to add Spire.Xls.dll, Spire.Common.dll and Spire.License.dll in our download Bin folder of Spire.Xls. Here we can see the default path: "..\Spire.XLS\Bin\NET4.0\Spire.XLS.dll"
Step 4: Convert Specific Worksheet Cells to Image
In this step, first we can initialize a new object of the class Spire.Xls.Workbook, then, load the template Excel file. Since I want to convert cells in the first sheet to image, I need get the first worksheet data in Excel file. You also can get other worksheet data. Finally specify cell range and save the cells in the range as image file. Spire.XLS for .NET supports 12 image formats: Bmp, Emf, Equals, Exif, Gif, Icon, Jpeg, MemoryBmp, Png, ReferenceEquals, Tiff and Wmf.
using System.Drawing; using System.Drawing.Imaging; using Spire.Xls; using Spire.Xls.Converter; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //Initialize a new Workbook object Workbook workbook = new Workbook(); //Open Template Excel file workbook.LoadFromFile(@"..\excel to image.xlsx"); //Get the first wirksheet in Excel file Worksheet sheet = workbook.Worksheets[0]; //Specify Cell Ranges and Save to certain Image formats sheet.SaveToImage(1, 1, 6, 3).Save("image1.png",ImageFormat.Png); sheet.SaveToImage(7, 1, 12, 3).Save("image2.jpeg", ImageFormat.Jpeg); sheet.SaveToImage(13, 1, 18, 3).Save("image3.bmp", ImageFormat.Bmp); } } }
Imports System.Drawing Imports System.Drawing.Imaging Imports Spire.Xls Imports Spire.Xls.Converter Namespace ConsoleApplication1 Class Program Private Shared Sub Main(args As String()) 'Initialize a new Workbook object Dim workbook As New Workbook() 'Open Template Excel file workbook.LoadFromFile("..\excel to image.xlsx") 'Get the first wirksheet in Excel file Dim sheet As Worksheet = workbook.Worksheets(0) 'Specify Cell Ranges and Save to certain Image formats sheet.SaveToImage(1, 1, 6, 3).Save("image1.png", ImageFormat.Png) sheet.SaveToImage(7, 1, 12, 3).Save("image2.jpeg", ImageFormat.Jpeg) sheet.SaveToImage(13, 1, 18, 3).Save("image3.bmp", ImageFormat.Bmp) End Sub End Class End Namespace
Result Task
After executing above code, cells in the first worksheet named "Sheet1" has been converted to three images. They are named "image1.png", "image2.jpeg" and "image3.bmp". You can see them as below:
In this section, I have introduced how we can convert specific cells to image by using Spire.XLS for .NET. I sincerely hope it can help you and give you some insight.
Spire.XLS for .NET offers fast speed, high efficiency and reliability to satisfy development application requirements. As you see above, the results do show that Spire.XLS for .NET benefit customers from years of research.
We appreciate any kind of queries, comments and suggestions at E-iceblue Forum. We promise a quick reply within minutes or hours as we can.
In daily work, you may come across some situations where you need to convert Excel to images, such as attaching a cell range to a PowerPoint presentation or safely sending your spreadsheet data via email. This article will show you how to programmatically convert Excel to images from the following two aspects using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert a Whole Excel Worksheet to an Image
The following are steps to convert a whole Excel worksheet to an image.
- Create a Workbook instance.
- Load an Excel sample document using Workbook.LoadFromFile() method.
- Get a specific worksheet of the document using Workbook.Worksheets[] property.
- Save the worksheet as an image using Worksheet.SaveToImage() method.
- C#
- VB.NET
using Spire.Xls; namespace Xls2Image { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel sample document workbook.LoadFromFile(@"sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Save the worksheet as an image sheet.SaveToImage("XlsToImage.jpg"); } } }
Convert a Specific Cell Range to an Image
In addition to converting a whole worksheet to an image, Spire.XLS for .NET also supports converting a specific cell range of a worksheet to an image. Detailed steps are listed below.
- Create a Workbook instance.
- Load an Excel sample document using Workbook.LoadFromFile() method.
- Get a specific worksheet of the document using Workbook.Worksheets[] property.
- Specify a cell range and save it as the Image object using Worksheet.ToImage() method, and then save the object as a certain image format using Image.Save() method.
- C#
- VB.NET
using Spire.Xls; using System.Drawing.Imaging; namespace SpecificCellsToImage { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel sample document workbook.LoadFromFile(@"sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Specify a cell range and save it to a certain image format sheet.ToImage(1, 1, 6, 4).Save("CellRangeToImage.png", ImageFormat.Png); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
C#/VB.NET: Convert Excel to Open XML or Open XML to Excel
2023-05-04 06:38:00 Written by support iceblueOpen XML is an XML-based file format developed by Microsoft that allows users to store and exchange various types of files such as word processing documents, spreadsheets, presentations, charts, and diagrams. It is widely recognized and supported by various applications, making it a reliable choice for long-term data preservation.
In some cases, it may be necessary to convert Excel files to Open XML format to ensure that these files can be opened and read by other software. On the other hand, there may be situations where users need to convert Open XML files to Excel format to take advantage of the data analysis tools available in Excel, such as pivot tables and charts. In this article, we will introduce how to convert Excel to Open XML or Open XML to Excel in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert Excel to Open XML in C# and VB.NET
To convert an Excel file to Open XML, you need to load the Excel file using the Workbook.LoadFromFile(string fileName) method, then call the Workbook.SaveAsXml(string fileName) method to save it in Open XML format.
The following steps demonstrate how to convert an Excel file to Open XML:
- Initialize an instance of the Workbook class.
- Load an Excel file using the Workbook.LoadFromFile(string fileName) method.
- Call the Workbook.SaveAsXml(string fileName) method to save the Excel file in Open XML format.
- C#
- VB.NET
using Spire.Xls; namespace ConvertExcelToOpenXML { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample.xlsx"); //Save the Excel file in Open XML file format workbook.SaveAsXml("ExcelToXML.xml"); } } }
Convert Open XML to Excel in C# and VB.NET
To convert an Open XML file to Excel, you need to load the Open XML file using the Workbook.LoadFromXml(string fileName) method, then call the Workbook.SaveToFile(string fileName, ExcelVersion version) method to save it in Excel format.
The following are the steps to convert an Open XML file to Excel:
- Initialize an instance of the Workbook class.
- Load an Open XML file using the Workbook.LoadFromXml(string fileName) method.
- Call the Workbook.SaveToFile(string fileName, ExcelVersion version) method to save the Open XML file in Excel format.
- C#
- VB.NET
using Spire.Xls; namespace ConvertOpenXMLToExcel { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load an Open XML file workbook.LoadFromXml("ExcelToXML.xml"); //Save the Open XML file in Excel XLSX file format workbook.SaveToFile("XMLToExcel.xlsx", ExcelVersion.Version2016); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.