J'ai écrit le programme pour convertir .xls en .xlsx, le programme change l'extension, mais les fonctions epplus ne fonctionnent pas le fichier converti. ci-dessous est le code pour convertir .xls .xlsx et colorer les cellules de .xlsx, le programme fonctionne sans erreur ou exception, mais Excel ne reçoit pas édité avec les couleurs après l'exécution du programmeComment puis-je convertir le fichier .xls en .xlsx en utilisant C#, puis modifier le fichier .xlsx comme cellules de coloration en utilisant epplus
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...");
}
}
}
}
Pouvez-vous nous donner plus d'informations? Existe-t-il une exception ou ne fonctionne-t-elle pas? Si une exception est levée, veuillez la fournir. –
@StefanKert: il n'y a pas d'exceptions, l'Excel n'est pas édité (les valeurs ne sont pas là en Excel) quand j'ouvre l'Excel après avoir exécuté le programme précédent – manvitha
Changer l'extension est une mauvaise chose à faire. Vous devez enregistrer en tant que xlsx, le fichier est différent à l'intérieur. –