[英]How do I convert .xls file to .xlsx using c# and then edit the .xlsx file like coloring cells using epplus
我编写了将.xls转换为.xlsx的程序,该程序更改了扩展名,但是epplus函数无法正常运行转换后的文件。 下面是将.xls转换为.xlsx并为.xlsx文件的单元格着色的代码,该程序正在运行,没有任何错误或异常,但是在运行该程序后,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。 如果数据足够简单,则可以先使用Office Automation或其他支持.xls的库读取数据,然后使用epplus ..编写.xlsx,然后再以.xlsx格式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.