【问题标题】:File Saveas or copy from xls to xlsx文件另存为或从 xls 复制到 xlsx
【发布时间】:2022-04-23 04:39:43
【问题描述】:

我在 C 磁盘中有一个名为 C:\Book1.xls

的 excel 文件

如何将 C:\Book1.xls 保存为 C:\Book2.xlsx

是否有 System.IO.File.SaveAs 类?

以下代码不起作用;

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

编辑:我不想使用 Excel 互操作,因为 Microsoft Office 版本。

【问题讨论】:

  • 什么是System.IO.File.SaveAs?我的 msdn 过时了吗?
  • 请问您能显示出不工作的特定代码吗?
  • File 是 C# 类。 SaveAs 不是其中的一部分。此外,您不能只将 .xls 保存为 .xlsx,它们是不同的格式,因此 System.IO.File 不会有太大帮助。
  • 您可能需要调查 Excel 互操作以了解更多信息。
  • 这能回答你的问题吗? How to convert xls file to xlsx file using C#?

标签: vb.net


【解决方案1】:

您可以使用此nuget package 将您当前的 xls 文档转换为 xlsx。

这样的东西对你有用:

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

This是包的主页,您可以找到更多详细信息。

【讨论】:

    【解决方案2】:

    另一个可能的选项 - 只需使用 C# 中的进程复制文件,创建一个进程将文件从 xls 复制到 xlsx。没有大惊小怪,没有混乱。这是在 .Net Core 6.0 和 o365 中。

    最终,您在进程中运行的字符串(也称为命令提示符)应该类似于:

    "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);
        }
    }
    

    要调用它,请指定所有参数、工作目录,然后就可以开始比赛了!!!

      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));
            
    

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多