简体   繁体   中英

c# Excel Interop _Workbooks.Open

I am writing ac# WinForms application in Microsoft Visual Studio. Our Data team is giving me a daily report in .csv format and I need to display the results visually. However, this program was originally written to handle .xlsx files, so instead of rewriting all of my code to handle .csv, I have decided it would be easier to convert the file to an .xlsx then leave the rest of the code as is.

So, I've modified the first portion of my drawCharts() method which is responsible for gathering the information from the xlsx and rendering it as excel charts.

My research has shown me two ways to read outside files into an Excel Workbook, the first using Workbooks.Open, the second using Workbooks.OpenText.

Here's my code:

    private void drawCharts()
    {
        //Declare variables for use
        //These first three values are going to be used later and can be ignored.
        Excel.Application xlApp = null;
        Excel.Workbook xlWorkbook = null;
        Excel.Worksheet xlWorksheet = null;
        object misValue = System.Reflection.Missing.Value;

        string path = "\\\\Data\\Departmental Data\\TechSupport\\_Report"

        //define the actual file path. Separate because I use the path variable elsewhere
        string source = Path.Combine(path, "report.csv");

        //define the destination file. Using Environment.UserName so multiple people can run this program at the same time, all using unique files except when they first load. There's probably a better way of doing this.
        string use = Path.Combine(path, "Report-" + Environment.UserName + ".xlsx");

        //A boolean telling the program to exit if there was an error
        bool shouldExit = false;
        Excel.Application app = null; //Temporary application used for opening the csv

        try
        {
            MessageBox.Show("Opening file " + source);
            app = new Excel.Application();
            Excel.Workbook wb = app.Workbooks.Open(source); //source points to the csv. This is the line that fails.
            MessageBox.Show(String.Format("Saving {0} as {1}", source, use));
            wb.SaveAs(use, Excel.XlFileFormat.xlOpenXMLWorkbook);
            wb.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            shouldExit = true;
        }
        finally
        {
            if (app != null) { app.Quit(); }
            releaseObject(app);
            if (shouldExit) { Environment.Exit(1); }
        }

        //Rest of the code...
}

The offending line is the Excel.Workbook wb = app.Workbooks.Open(source); . When this line is executed, Excel throws an error stating that the file could not be opened because it could not be found. Researching this, I found this article which said I should change it to Excel.Workbook wb = app.Workbooks.OpenText(source) . However, that just causes a compiler error stating Cannot implicitly convert type 'void' to 'Microsoft.Office.Interop.Excel.Workbook' .

Any ideas would be appreciated.

So, after a bit of research, I found my error. The file path had changed because the folder the .csv had resided in was renamed. When checking whether the file path was correct, I had been looking in my test directory instead of the production directory, so everything appeared correct.

It's always the little stuff....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM