0

I did a little research, and an unsure if I should be using

the Microsoft.Office.Interop.Excel

Just to clarify with the example: I'm taking a .txt, and doing stuff, then saving it as a .CSV (Comma Separated Values). I want to then open it (on button click or something...)

How can this be done?

wizlog
  • 302
  • 7
  • 22

5 Answers5

3

Pair to your comment:

I want it to open in Excel.exe. It should be a separate window that launches after my program is done with its conversion

Simply start it using System.Diagnostics.Process class:

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();
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
2

You can start the excel process with the file path as command line parameter (excel.exe C:\myFile.csv). This will open it in excel.

Bas
  • 26,772
  • 8
  • 53
  • 86
1

Yup, Microsoft.Office.Interop.Excel is what you will need to open the CSV file in Excel.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
1

Depends on which framework you are using (i.e. Silverlight or Windows Forms). If I were you I'd be using OpenFileDialog to read the values from the comma seperated list into a string or a class. The sample below is for 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();
        }
    }
The Internet
  • 7,959
  • 10
  • 54
  • 89
  • Its Windows Forms. And I'm not trying to read the CSV. I created the CSV, I'm just trying to open it. – wizlog Aug 19 '11 at 18:38
0

Is this a WinForms, WPF, or ASP.NET application? If you use the Office Interop libraries you are dependent on Office being installed on that machine. If it's a web server, you don't want to run Excel that way.

Check out www.SpreadSheetGear.com. No affiliation, just very satisfied.

n8wrl
  • 19,439
  • 4
  • 63
  • 103