Conversion (30)
We have discussed before about converting workbook to PDF. However, in this section, we will show you a neat solution to convert the specific worksheet to PDF with C# and VB.NET in workbook. Apply Spire.Xls for .NET in your application and you can turn worksheet into PDF easily without changing the layout of worksheet.
In the following sections, we will demonstrate how to convert worksheet to PDF.
Step 1: Initialize a new instance of Workbook class and load the sample Excel file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Step 2: Get its first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Convert the selected worksheet to PDF and save to file.
sheet.SaveToPdf("toPDF.pdf");
Step 4: Launch the file.
System.Diagnostics.Process.Start("toPDF.pdf");
Effective screenshot:

Full codes:
using Spire.Xls;
namespace Excel_Worksheet_to_PDF
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToPdf("toPDF.pdf");
System.Diagnostics.Process.Start("toPDF.pdf");
}
}
}
Imports Spire.Xls
Namespace Excel_Worksheet_to_PDF
Class Program
Private Shared Sub Main(args As String())
Dim workbook As New Workbook()
workbook.LoadFromFile("Sample.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToPdf("toPDF.pdf")
System.Diagnostics.Process.Start("toPDF.pdf")
End Sub
End Class
End Namespace
Show formula and its result separately when converting excel to datatable in C#
2015-07-10 05:37:43 Written by KoohjiThis article shows how to display formula and its result separately when converting excel to database via Spire.XLS. This demo uses an Excel file with formula in it and show the conversion result in a Windows Forms Application project.
Screenshot of the test excel file:

Here are the detailed steps:
Steps 1: Create a Windows Forms Application in Visual Studio.
Steps 2: Drag a DataGridView and two Buttons from Toolbox to the Form and change names of the buttons as Formula and Result to distinguish.

Steps 3: Double click Button formula and add the following code.
3.1 Load test file and get the first sheet.
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"1.xlsx"); Worksheet sheet = workbook.Worksheets[0];
3.2 Invoke method ExportDataTable of the sheet and output data range. Parameters of ExportDataTable are range to export, indicates if export column name and indicates whether compute formula value, then it will return exported datatable.
Description of ExportDataTable:
public DataTable ExportDataTable(CellRange range, bool exportColumnNames, bool computedFormulaValue);
Code:
DataTable dt = sheet.ExportDataTable(sheet.AllocatedRange, false, false);
3.3 Show in DataGridView
this.dataGridView1.DataSource = dt;
Steps 4: Do ditto to Button Result. Only alter parameter computedFormulaValue as true.
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"1.xlsx"); Worksheet sheet = workbook.Worksheets[0]; DataTable dt = sheet.ExportDataTable(sheet.AllocatedRange, false, true); this.dataGridView1.DataSource = dt;
Steps 5: Start the project and check the result.

Button code here:
//Formula
private void button1_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"1.xlsx");
Worksheet sheet = workbook.Worksheets[0];
DataTable dt = sheet.ExportDataTable(sheet.AllocatedRange, false, false);
this.dataGridView1.DataSource = dt;
}
//Result
private void button2_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"1.xlsx");
Worksheet sheet = workbook.Worksheets[0];
DataTable dt = sheet.ExportDataTable(sheet.AllocatedRange, false, true);
this.dataGridView1.DataSource = dt;
}
Spire.XLS has powerful functions to export Excel worksheets into different image file formats. In the previous articles, we have already shown you how to convert Excel worksheets into BMP, PNG, GIF, JPG, JPEG, TIFF. Now Spire.XLS newly starts to support exporting Excel worksheet into EMF image. With the help of Spire.XLS, you only need three lines of codes to finish the conversion function.
Make sure Spire.XLS (Version 7.6.43 or above) has been installed correctly and then add Spire.xls.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Xls\Bin\NET4.0\ Spire. Xls.dll". Here comes to the details of how to convert excel worksheet to EMF image.
Step 1: Create an excel document and load the document from file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("XLS2.xlsx");
Step 2: Get the first worksheet in excel workbook.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Save excel worksheet into EMF image.
sheet.SaveToEMFImage("result.emf", 1, 1, 19, 6, system.Drawing.Imaging.EmfType.EmfPlusDual);
Effective screenshot:

Full codes:
using Spire.Xls;
namespace XLStoEMF
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("XLS2.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToEMFImage("result.emf", 1, 1, 19,6, System.Drawing.Imaging.EmfType.EmfPlusDual);
}
}
}
Using Spire.XLS, programmers are able to save the whole worksheet as PDF by calling the method SaveToPdf(). However, you may only want to save or export a part of worksheet as PDF. Since Spire.XLS doesn't provide a method to directly convert a range of cells to PDF, we can copy the selected ranges to a new worksheet and then save it as PDF file. This method seems complex, but it is still efficient with Spire.XLS.
Look at the test file below, we only want the cells from A1 to H11 converted as PDF. We will firstly create a new blank worksheet, copy the selected range to the new sheet using CellRange.Copy() method, then convert the new sheet as PDF.

Code Snippet:
Step 1: Create a new workbook and load the test file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010);
Step 2: Add a new worksheet to workbook.
workbook.Worksheets.Add("newsheet");
Step 3: Copy the selected range from where it stores to the new worksheet.
workbook.Worksheets[0].Range["A1:H11"].Copy(workbook.Worksheets[1].Range["A1:H11"]);
Step 4: Convert the new worksheet to PDF.
workbook.Worksheets[1].SaveToPdf("result.pdf", Spire.Xls.FileFormat.PDF);
Result:

Full Code:
using Spire.Xls;
namespace Convert
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010);
// add a new sheet to workbook
workbook.Worksheets.Add("newsheet");
//Copy your area to new sheet.
workbook.Worksheets[0].Range["A1:H11"].Copy(workbook.Worksheets[1].Range["A1:H11"]);
//convert new sheet to pdf
workbook.Worksheets[1].SaveToPdf("result.pdf", Spire.Xls.FileFormat.PDF);
}
}
}
Imports Spire.Xls
Namespace Convert
Class Program
Private Shared Sub Main(args As String())
Dim workbook As New Workbook()
workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010)
' add a new sheet to workbook
workbook.Worksheets.Add("newsheet")
'Copy your area to new sheet.
workbook.Worksheets(0).Range("A1:H11").Copy(workbook.Worksheets(1).Range("A1:H11"))
'convert new sheet to pdf
workbook.Worksheets(1).SaveToPdf("result.pdf", Spire.Xls.FileFormat.PDF)
End Sub
End Class
End Namespace
A file with the XLSM extension is an Excel Macro-Enabled Workbook file. For security reasons, XLS file or XLSX file does not enable macros by default. Thus, if you want to execute macros in Excel file, you need to convert XLS or XLSX to XLSM at the first place. In this article, I’ll introduce you how to convert XLS to XLSM with the macro maintained using Spire.XLS.
Here is the method:
Step 1: Create a new instance of Spire.Xls.Workbook class.
Workbook workbook = new Workbook();
Step 2: Load the test file and imports its data to workbook.
workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003);
Step 3: Save the workbook as a new XLSM file.
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
Full Code:
using Spire.Xls;
namespace Convert
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003);
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);
}
}
}
Imports Spire.Xls
Namespace Convert
Class Program
Private Shared Sub Main(args As String())
Dim workbook As New Workbook()
workbook.LoadFromFile("test.xls", ExcelVersion.Version97to2003)
workbook.SaveToFile("result.xlsm", FileFormat.Version2007)
End Sub
End Class
End Namespace
Test File:
As is shown in the picture, Excel automatically disables macro in XLS file.

Result:
No security warning in the converted XLSM file.

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.
If 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.
How to Convert Excel (XLS or XLSX) to PDF in C# .NET: Complete Guide
2025-05-08 08:50:00 Written by Koohji
Converting Excel files to PDF is a crucial task for anyone looking to share spreadsheet data in a secure, consistent, and universally accessible format. Whether you are generating financial reports, creating invoices, or sharing analytical data, PDFs ensure that your document's layout and formatting remain intact across all devices and platforms. Unlike Excel files, which require compatible software to open, PDFs are universally viewable without any dependency.
This guide provides a comprehensive overview of how to efficiently convert Excel files to PDF in C# using a .NET Excel library – Spire.XLS for .NET. You will learn both basic and advanced conversion techniques, including how to export specific sheets or cell ranges, customize page setup, secure converted PDFs with passwords, generate PDF/A-compliant files, and more.
Table of Contents
- Why Convert Excel to PDF
- C# .NET Excel to PDF Conversion Library
- Basic Excel to PDF Conversion
- Advanced Excel to PDF Conversion Features
- FAQs
- Conclusion
Why Convert Excel to PDF
Converting Excel files to PDF offers several key advantages:
- Preserved Layout and Formatting: PDF maintains the original structure and formatting of your Excel file, ensuring consistent appearance across devices.
- Cross-Platform Accessibility: PDF is universally compatible, viewable on any device or operating system without requiring Excel or other spreadsheet software.
- Enhanced Security: PDF files can be encrypted, digitally signed, and restricted to prevent unauthorized access, copying, or editing.
C# .NET Excel to PDF Conversion Library
Spire.XLS for .NET is a comprehensive Excel library that enables seamless conversion of Excel files to PDF within .NET applications without the need for Microsoft Office. It provides developers with full control over how the content is rendered and ensures that the layout and formatting are preserved during the conversion process.
Install Spire.XLS for .NET
Before starting the conversion process, install Spire.XLS for .NET using one of the following methods:
- Option 1: Install via NuGet (Recommended)
Install-Package Spire.XLS
- Option 2: Manually Add DLLs to Your Project
- Download the Spire.XLS package and extract the files.
- In Visual Studio, right-click References > Add Reference > Browse, then select the appropriate Spire.Xls.dll based on your target framework.
Basic Excel to PDF Conversion
Converting an Excel file to PDF with Spire.XLS is simple and requires only a few lines of code. The following example demonstrates how to load an Excel file and save it as a PDF:
- C#
using Spire.Xls;
namespace ExcelToPdf
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load an Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Save the Excel file to PDF
workbook.SaveToFile("Output.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}

Advanced Excel to PDF Conversion Features
In addition to basic conversion, Spire.XLS for .NET provides advanced options for customized Excel-to-PDF conversions, including:
- Export specific sheet or cell range as PDF.
- Fit sheet to one page.
- Adjust page setup options (e.g., margins, orientation, paper size) for customized PDF output.
- Secure the converted PDF with password.
- Generate PDF/A-compliant files for long-term preservation.
Export Specific Sheet or Cell Range as PDF
Sometimes, you may want to export only a specific sheet or range of cells from an Excel file to PDF. Here's how to do that:
- C#
using Spire.Xls;
namespace WorksheetOrCellRangeToPdf
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
// Set the print area to a specific cell range
// Comment this line out if you need to export the entire worksheet as a PDF
sheet.PageSetup.PrintArea = "B1:E6";
// Save the cell range as a PDF
sheet.SaveToPdf("CellRange.pdf");
// Dispose resources
workbook.Dispose();
}
}
}
Fit Sheet to One Page
Spire.XLS allows you to fit the content of a sheet to one page, which is particularly useful for printing or distributing concise reports.
- C#
using Spire.Xls;
namespace FitWorksheetToOnePage
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Fit every worksheet in the workbook to one page
workbook.ConverterSetting.SheetFitToPage = true;
// Save the Excel file to PDF
workbook.SaveToFile("FitToOnePage.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
Adjust Page Setup for Customized PDF Output
Before converting an Excel worksheet to PDF, you can adjust page setup options such as margins, paper size, orientation, and gridline visibility. This ensures the final PDF is accurately formatted to meet your presentation requirements.
- C#
using Spire.Xls;
namespace AdjustPageSetup
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
// Adjust page setup settings
// Set page orientation
sheet.PageSetup.Orientation = PageOrientationType.Landscape;
// Set paper size
sheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
// Set margins
sheet.PageSetup.LeftMargin = 0.5;
sheet.PageSetup.RightMargin = 0.5;
sheet.PageSetup.TopMargin = 0.5;
sheet.PageSetup.BottomMargin = 0.5;
// Display Gridlines
sheet.PageSetup.IsPrintGridlines = true;
// Save the worksheet as a PDF
sheet.SaveToPdf("CustomPageSetup.pdf");
// Dispose resources
workbook.Dispose();
}
}
}
Secure the Converted PDF with Password
You can secure the converted PDF by applying password protection. This ensures that unauthorized users cannot access or modify the document.
- C#
using Spire.Xls;
using Spire.Xls.Pdf.Security;
namespace SecurePdfWithPassword
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Set the open and permission passwords for the converted PDF
workbook.ConverterSetting.PdfSecurity.Encrypt("openPassword", "persmissionPassword", PdfPermissionsFlags.Print, PdfEncryptionKeySize.Key128Bit);
// Save the Excel file to PDF
workbook.SaveToFile("SecurePdf.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
Generate PDF/A-compliant Files
If you need to archive your documents for long-term storage or ensure they meet certain accessibility standards, you can generate PDF/A-compliant files from Excel.
- C#
using Spire.Xls;
using Spire.Xls.Pdf;
namespace ExcelToPdfA
{
internal class Program
{
static void Main(string[] args)
{
// Create a Workbook object
Workbook workbook = new Workbook();
// Load the Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
// Set the compliance for the converted PDF
workbook.ConverterSetting.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1A;
// Save the Excel file to PDF
workbook.SaveToFile("PdfA_Compliant.pdf", FileFormat.PDF);
// Dispose resources
workbook.Dispose();
}
}
}
FAQs
Q1: Can I convert a password-protected Excel file to PDF?
Yes, you can load password-protected Excel files by providing the password before loading:
- C#
Workbook workbook = new Workbook();
workbook.OpenPassword = "ExcelPassword";
workbook.LoadFromFile("ProtectedExcel.xlsx");
workbook.SaveToFile("Output.pdf", FileFormat.PDF);
Q2: Is Spire.XLS compatible with .NET Core?
Yes, Spire.XLS supports .NET Framework, .NET Core, and .NET Standard.
Q3: Can I batch convert multiple Excel files to PDF?
Yes, you can batch convert multiple Excel files to PDF using a loop:
- C#
string[] files = Directory.GetFiles("ExcelFolder", "*.xlsx");
foreach (string file in files)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(file);
string outputPath = Path.ChangeExtension(file, ".pdf");
workbook.SaveToFile(outputPath, FileFormat.PDF);
}
Q4: Can I convert multiple Excel files to a single PDF?
Yes, you can combine multiple Excel files into a single workbook and then save them as a single PDF:
- C#
Workbook combinedWorkbook = new Workbook();
combinedWorkbook.Worksheets.Clear();
foreach (string file in Directory.GetFiles("ExcelFolder", "*.xlsx"))
{
Workbook tempWorkbook = new Workbook();
tempWorkbook.LoadFromFile(file);
foreach (Worksheet sheet in tempWorkbook.Worksheets)
{
combinedWorkbook.Worksheets.AddCopy(sheet);
}
}
combinedWorkbook.SaveToFile("Combined.pdf", FileFormat.PDF);
Q5: Why does my converted PDF look different from the Excel file?
This issue could be due to missing fonts on the system. Make sure that all fonts used in the Excel file are installed on the machine performing the conversion.
Conclusion
Spire.XLS for .NET provides a powerful and flexible solution for converting Excel files to PDF in C#. Whether you need simple conversions or advanced features—such as exporting specific sheets or cell ranges, customizing page setup, applying password protection to the converted PDF, or generating PDF/A-compliant files—Spire.XLS offers a comprehensive set of tools to meet all your requirements. By following the steps outlined in this guide, you can easily integrate Excel-to-PDF conversion capabilities into your .NET applications.
Get a Free License
To fully experience the capabilities of Spire.XLS for .NET without any evaluation limitations, you can request a free 30-day trial license.
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.