Dear All,
Please need your support i need to convert xlsx file to xls. can anyone share with me best solution.
Dear All,
Please need your support i need to convert xlsx file to xls. can anyone share with me best solution.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 17, 2024, 5:57 AM EST
To convert an XLSX file to XLS format in C#, you can make use of the Microsoft Office Interop libraries. These libraries provide functionality to work with Excel files programmatically.
Here's an example of how you can convert a single XLSX file to XLS format:
using Excel = Microsoft.Office.Interop.Excel; public void ConvertXlsxToXls(string xlsxFilePath, string xlsFilePath) { Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(xlsxFilePath); workbook.SaveAs(xlsFilePath, Excel.XlFileFormat.xlExcel8); workbook.Close(); excelApp.Quit(); }In the above code, we create an instance of the
Excel.Applicationclass and open the XLSX file using theWorkbooks.Openmethod. We then save the workbook in XLS format using theSaveAsmethod with thexlExcel8file format. Finally, we close the workbook and quit the Excel application.To convert multiple XLSX files to XLS format, you can iterate over the files and call the
ConvertXlsxToXlsmethod for each file. Here's an example:public void ConvertMultipleXlsxToXls(string[] xlsxFilePaths, string outputFolderPath) { foreach (string xlsxFilePath in xlsxFilePaths) { string xlsFilePath = Path.Combine(outputFolderPath, Path.GetFileNameWithoutExtension(xlsxFilePath) + ".xls"); ConvertXlsxToXls(xlsxFilePath, xlsFilePath); } }In the above code, we iterate over the array of XLSX file paths and generate the corresponding XLS file path using the
Path.CombineandPath.GetFileNameWithoutExtensionmethods. We then call theConvertXlsxToXlsmethod to convert each XLSX file to XLS format.Remember to add a reference to the
Microsoft.Office.Interop.Excelassembly in your project.Tahir AnsariRank #366 · 6.8K reputationPosted Jan 17, 2024, 3:29 AM EST
First, you'll need to install the required NuGet packages. In your Visual Studio project, open the Package Manager Console and run the following commands:
Once the packages are installed, you can use the following code to convert a .xlsx file to .xls:
using System; using System.IO; using OfficeOpenXml; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; class Program { static void Main() { string inputFilePath = "path/to/input/file.xlsx"; string outputFilePath = "path/to/output/file.xls"; ConvertXlsxToXls(inputFilePath, outputFilePath); Console.WriteLine("Conversion complete."); } static void ConvertXlsxToXls(string inputFilePath, string outputFilePath) { using (var excelPackage = new ExcelPackage(new FileInfo(inputFilePath))) { var workbook = excelPackage.Workbook; var xlsWorkbook = new HSSFWorkbook(); foreach (var sheet in workbook.Worksheets) { var xlsSheet = xlsWorkbook.CreateSheet(sheet.Name); for (int row = 1; row <= sheet.Dimension.Rows; row++) { var xlsRow = xlsSheet.CreateRow(row - 1); for (int col = 1; col <= sheet.Dimension.Columns; col++) { var cellValue = sheet.Cells[row, col].Text; var xlsCell = xlsRow.CreateCell(col - 1); xlsCell.SetCellValue(cellValue); } } } using (var fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write)) { xlsWorkbook.Write(fs); } } } }This code uses the EPPlus library to read the .xlsx file and the NPOI library to create a new .xls file with the same data.
Uttam ChaturvediRank #477 · 5.2K reputationPosted Jan 17, 2024, 9:45 AM EST
I would also recommend NPOI as a quite useful package
Microsoft Office Interop does'nt work on azure appservices
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 18, 2024, 2:55 AM EST
Hello Feroz,
Please use this code, use EPPlus Nuget package
using System; using System.IO; using OfficeOpenXml; using OfficeOpenXml.ExcelPackage; class Program { static void Main() { // XLSX files folder path string inputDirectory = @"C:\Path\To\Your\XLSX\Files"; // output path for for XLS files string outputDirectory = @"C:\Path\To\Your\Output\Directory"; // Ensure the output directory exists Directory.CreateDirectory(outputDirectory); // Read all XLSX files in the input directory string[] xlsxFiles = Directory.GetFiles(inputDirectory, "*.xlsx"); foreach (var xlsxFile in xlsxFiles) { FileInfo xlsxFileInfo = new FileInfo(xlsxFile); // Create the corresponding output XLS file path string xlsFilePath = Path.Combine(outputDirectory, Path.ChangeExtension(xlsxFileInfo.Name, "xls")); // Convert XLSX to XLS file ConvertXlsxToXls(xlsxFileInfo.FullName, xlsFilePath); } Console.WriteLine("Conversion complete."); } static void ConvertXlsxToXls(string xlsxFilePath, string xlsFilePath) { // Load the XLSX file using EPPlus using (var package = new ExcelPackage(new FileInfo(xlsxFilePath))) { // Save the workbook as XLS format using (var xlsFile = new FileStream(xlsFilePath, FileMode.Create)) { package.SaveAs(xlsFile); } } } }Leon DRank #851 · 2.6K reputationPosted Jan 21, 2024, 9:37 PM EST
Hello,
You can use Spire.XLS package to easily convert xlsx to xls and vice versa.
Install-Package Spire.XlsHere is an example that converts an xlsx file to xls format:
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); } } }More details can be found in this documentation:
https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Conversion/NET-Excel-Conversion-between-Excel-97-2003.xls-and-Excel-2007.xlsx.html