如何使用c#将.xls文件转换为.xlsx,然后编辑.xlsx文件,如使用epplus对单元格着色

我编写了将.xls转换为.xlsx的程序,该程序更改了扩展名,但epplus函数不能处理转换后的文件。 下面是将.xls转换为.xlsx并为.xlsx文件的单元格着色的代码,程序运行时没有任何错误或exception,但是运行程序后,excel没有被颜色编辑

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Drawing; using OfficeOpenXml; using OfficeOpenXml.Style; namespace Project42 { class Class1 { static void Main(string[] args) { Console.WriteLine("File Extension Conversion From .xls to .xlsx"); try { string filePath = @"C:\Users\mvmurthy\Desktop\abc.xls"; if (filePath.EndsWith(".xls")) { File.Move(filePath, Path.ChangeExtension(filePath, ".xlsx")); Console.WriteLine("File Extension Updated To .xlsx"); } FileInfo newFile = new FileInfo(@"C:\Users\mvmurthy\Downloads\abc.xlsx"); ExcelPackage pck = new ExcelPackage(newFile); var ws = pck.Workbook.Worksheets["Contents"]; ws.Cells["K:K"].Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Cells["K:K"].Style.Fill.BackgroundColor.SetColor(Color.Yellow); ws.Cells["M:M"].Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Cells["M:M"].Style.Fill.BackgroundColor.SetColor(Color.Yellow); ws.Cells["O:O"].Style.Fill.PatternType = ExcelFillStyle.Solid; ws.Cells["O:O"].Style.Fill.BackgroundColor.SetColor(Color.Yellow); pck.Save(); } catch (Exception) { Console.WriteLine("File Extension Cannot Be Changed...Try again..."); } } } } 

你不能只是改变文件扩展名,你可能能够使用办公自动化保存为.xlsx。 如果数据足够简单,那么您可以首先使用支持.xls的办公自动化或其他库来读取数据,然后使用epplus编写.xlsx ..然后将使用.xlsx格式。