'File Saveas or copy from xls to xlsx

I have an excel file in the C disk named C:\Book1.xls

How can I saveas C:\Book1.xls to C:\Book2.xlsx ?

Is there a System.IO.File.SaveAs class?

The following code doesnt work;

IO.File.Copy(sourceFileName:="‪‪C:\Book1.xls", destFileName:="C:\Book1.xlsx", overwrite:=True)

Edit: I dont want to use Excel Interop because of Microsoft Office versions.



Solution 1:[1]

You can use this nuget package for converting your current xls document to xlsx.

Something like this will work for you :

Workbook workbook = new Workbook();
workbook.LoadFromFile("Book1.xls");
workbook.SaveToFile("Book2.xlsx", ExcelVersion.Version2016);

This is the main page of package that you may find more details.

Solution 2:[2]

Another possible option - just copy the file using a process in C#, create a process to copy the file from xls to xlsx. No fuss, no muss. This is in .Net Core 6.0 and o365.

Ultimately, the string you are running in the process (aka command prompt) should look similar to this:

"C:\Program Files (x86)\Microsoft Office\root\Office16\excelcnv.exe" -oice "\Share\Folder\Cash.xls" "\Share\Folder\Cash.xlsx"

public static class Xls2XlsxCmdProcess
{ 
    
    public static string processDirectory = @"C:\Program Files (x86)\Microsoft Office\root\Office16\";

    public static void ExecuteCommandSync(string pathToExe, string command)
    {
        
        var procStartInfo = new ProcessStartInfo(pathToExe, command)
        {
            WorkingDirectory = processDirectory,
            CreateNoWindow = false,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true
        };

        var proc = new Process { StartInfo = procStartInfo };
        proc.Start();

        //proc.StandardInput.WriteLine(password);//If the app that requires a password or other params, they can be added here as a string.
        proc.StandardInput.Flush();

        // Get the output into a string
        string result = proc.StandardOutput.ReadToEnd();
        string error = proc.StandardError.ReadToEnd();

        Console.WriteLine(result);
        Console.WriteLine(error);
    }
}

To call it, specify all your params, your working directory, and you're off to the races!!!

  string baseFolder = @"\\Share\folder\";
  string fileNameCash = "Cash.xls";
  string fileNameCashOutput = "Cash.xlsx";
  //Create cash as xlsx files (for ease of use with EPPlus4)
  string pathToExe = @"C:\Program Files (x86)\Microsoft Office\root\Office16\excelcnv.exe";//Path to office XLS to XLSX Conversion Tool for o365 - You can find a similar version by searching for the excelcnv.exe within "C:\Program Files (x86)\Microsoft Office" folder.
  string commandFormat1 = string.Format(@"""{0}"" -oice ""{1}{2}"" ""{3}{4}"" ",pathToExe, @BaseFolder, fileNameCash,@BaseFolder,fileNameCashOutput);
  Xls2XlsxCmdProcess.ExecuteCommandSync(pathToExe, string.Format(commandFormat1));
        

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Danimal111