-1

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.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Dr. Code
  • 53
  • 6
  • 1
    What is `System.IO.File.SaveAs`? Is my msdn outdated? – Sinatr Nov 06 '19 at 14:55
  • Please can you show the specific piece of code that's not working? – GameScripting Nov 06 '19 at 14:56
  • 3
    `File` is the C# class. `SaveAs` isn't a part of that. Also, you can't just save a .xls as an .xlsx, they are different formats, so `System.IO.File` isn't going to be of much help. – Broots Waymb Nov 06 '19 at 14:56
  • You may want to investigate Excel interop for more information. – Christopher Townsend Nov 06 '19 at 15:07
  • The code you've posted is isn't going to work because you need to convert the format. What you have is just doing a simple copy & rename. It would be like if I took a .docx file from Word and tried to save it as a .jpg and expected a picture. – Broots Waymb Nov 06 '19 at 15:07
  • 2
    Does this answer your question? [How to convert xls file to xlsx file using C#?](https://stackoverflow.com/questions/46937604/how-to-convert-xls-file-to-xlsx-file-using-c) – Broots Waymb Nov 06 '19 at 15:13

2 Answers2

4

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.

nzrytmn
  • 6,193
  • 1
  • 41
  • 38
0

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));
        
Danimal111
  • 1,976
  • 25
  • 31