2016-08-19 31 views
0

我編寫了將.xls轉換爲.xlsx的程序,但程序更改了擴展名,但epplus函數不工作轉換文件。下面是轉換的.xls原來的.xlsx和顏色,程序沒有任何錯誤或異常運行的.xlsx文件細胞的代碼,但Excel是沒有得到與顏色運行程序如何使用c#將.xls文件轉換爲.xlsx,然後使用epplus編輯.xlsx文件,如使用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..."); 
     } 
    } 
} 
} 
+0

你能給我們進一步的信息嗎?有拋出異常,還是隻是不工作?如果發生異常,請提供。 –

+0

@StefanKert:在運行上述程序後打開excel時,沒有例外,excel沒有被編輯(excel中沒有值) – manvitha

+2

更改擴展名是錯誤的。你必須保存爲xlsx,文件內部不同。 –

回答

1
編後

您不能更改文件擴展名,您可以使用Office自動保存爲.xlsx。如果數據足夠簡單,那麼您可以首先使用支持.xls的辦公自動化或其他庫來讀取數據,然後使用epplus編寫.xlsx ..然後該文件將以.xlsx格式工作。

相關問題