【问题标题】:C# After I save a .csv in Excel, how do I open it?C# 在 Excel 中保存 .csv 后,如何打开它?
【发布时间】:2011-10-30 16:01:09
【问题描述】:

我做了一些研究,不确定是否应该使用

Microsoft.Office.Interop.Excel

只是为了澄清这个例子:我正在使用 .txt,然后做一些事情,然后将其保存为 .CSV(逗号分隔值)。然后我想打开它(点击按钮或其他东西......)

如何做到这一点?

【问题讨论】:

标签: c# excel c#-4.0 excel-2007


【解决方案1】:

与您的评论配对:

我希望它在 Excel.exe 中打开。它应该是一个单独的窗口,在我的程序完成转换后启动

只需使用System.Diagnostics.Process 类启动它:

using System.Diagnostics.Process;//at top of your application

//
//At button click or after you finish editing
//
Process excel = new Process();

//if the excel was installed in the target machine and the default program to open csvs
//then you can simply just call process start and put the file path, like:
excel.Start(@"Your edited csv file path");

//otherwise:
excel.StartInfo.FileName = @"The excel application file path";
excel.StartInfo.Arguments = @"Your edited csv file path";
excel.Start();

【讨论】:

    【解决方案2】:

    您可以使用文件路径作为命令行参数 (excel.exe C:\myFile.csv) 启动 excel 进程。这将在 excel 中打开它。

    【讨论】:

      【解决方案3】:

      是的,Microsoft.Office.Interop.Excel 是您在 Excel 中打开 CSV 文件所需要的。

      【讨论】:

        【解决方案4】:

        取决于您使用的框架(即 Silverlight 或 Windows 窗体)。 如果我是你,我会使用 OpenFileDialog 将逗号分隔列表中的值读入字符串或类。下面的示例适用于 silverlight。

        private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
            {
                // Create an instance of the open file dialog box.
                var openFileDialog1 = new OpenFileDialog();
        
                // Set filter options and filter index.
                openFileDialog1.Filter = "CSV File (.csv)|*.csv|All Files (*.*)|*.*";
                openFileDialog1.FilterIndex = 1;
        
                openFileDialog1.Multiselect = false;
        
                // Call the ShowDialog method to show the dialog box.
                bool? userClickedOK = openFileDialog1.ShowDialog();
        
                // Process input if the user clicked OK.
                if (userClickedOK == true)
                {
                    // Open the selected file to read.
                    System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
        
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
                    {
                        // Read the first line from the file and write it the textbox.
                        tbResults.Text = reader.ReadLine();
                        //the results of your CSV are now stored in tbResults.Text
                        //optionally you could parse the .CSV using string.Spit(',') into a string      array                    
                    }
                    fileStream.Close();
                }
            }
        

        【讨论】:

        • 它的 Windows 窗体。而且我不想阅读 CSV。我创建了 CSV,我只是想打开它。
        【解决方案5】:

        这是 WinForms、WPF 还是 ASP.NET 应用程序?如果您使用 Office 互操作库,则依赖于该计算机上安装的 Office。如果它是一个 Web 服务器,您不想以这种方式运行 Excel。

        查看 www.SpreadSheetGear.com。没有隶属关系,只是很满意。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-30
          • 2014-12-13
          • 1970-01-01
          相关资源
          最近更新 更多