0

I'm developing an MVC .Net web application, and I want to read data from both .xlsx and .xls file. Currently, my application have no problem reading .xls file. I read about all the connection string but I have been having problem figuring out how should I go about implementing them as this is my first experience handling C#. Anyway here are some snippets of my code.Thanks alot in advance. Appreciate your great help.

private void button_OpenFile_Click(object sender, EventArgs e)
        {
            if(fileType.Equals("csv")){
                openFileDialog_ExcelFile.Filter = "CSV Files (.csv)|*.csv";
            }
            else if (fileType.Equals("xls"))
            {
                openFileDialog_ExcelFile.Filter = "Excel Worksheets 1997 -2003(.xls)|*.xls";
            }
            else if (fileType.Equals("xlsx"))
            {
                openFileDialog_ExcelFile.Filter = "Excel Worksheets 2007 (.xlsx)|*.xlsx";
            }


            DialogResult result = openFileDialog_ExcelFile.ShowDialog(); // Show the dialog.

            if (result == DialogResult.OK) // Test result.
            {

                OpenNewFile();
                fileOpened = true;
            }

            if (includeAllAttributesByDefault)
                SelectAndIncludeAll();

            nonNumberChkBx.Checked = includeNumericValuesByDefault;

            //Debug.WriteLine(result); // <-- For debugging use only.
        }



private void OpenNewFile()
    {
        // Initializing new data object.
        try
        {
            data = new MyData(openFileDialog_ExcelFile.FileName);


            UpdateCheckListBox();
            UpdateGridView();
            UpdateCurrentFileInfo();
        }
kai
  • 57
  • 1
  • 8

1 Answers1

0

The actual error you describe can't be found in the code you posted (a possible solution can be found here: Excel "External table is not in the expected format."). I wanted to point out another flaw which might be even more important:

You are trying to use a component (OpenFileDialog) that only works in desktop applications, not in web applications. I guess it works on your local machine, since IIS Express is a desktop application, so it is capable of showing this to you. I will never work in a deployed server though.

You have to come up with some form of upload functionality to post the document to the web server.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325