1

In my application I have to read an OpenOffice spreadsheet and display the data in the spreadsheet in grid view below is the code that I used but it is providing an exception that external table is not in the correct format how can I resolve this

 <div>
        Import Excel File:  
        <asp:FileUpload ID="FileUpload1" runat="server" />  
        <br />  
        <br />  
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />  
        <br />  
        <br />  
        <asp:Label ID="Label1" runat="server"></asp:Label>  
        <br />  
        <asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
            <EditRowStyle BackColor="#999999" />  
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
            <SortedAscendingCellStyle BackColor="#E9E7E2" />  
            <SortedAscendingHeaderStyle BackColor="#506C8C" />  
            <SortedDescendingCellStyle BackColor="#FFFDF8" />  
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
        </asp:GridView>  

    </div>  

 //Coneection String by default empty  
            string ConStr = "";
            //Extantion of the file upload control saving into ext because   
            //there are two types of extation .xls and .xlsx of Excel   
            string ext = Path.GetExtension(FileUpload1.FileName).ToLower();
            //getting the path of the file   
            string path = Server.MapPath("~/MyFolder/" + FileUpload1.FileName);
            //saving the file inside the MyFolder of the server  
            FileUpload1.SaveAs(path);
            Label1.Text = FileUpload1.FileName + "\'s Data showing into the GridView";
            //checking that extantion is .xls or .xlsx  
            if (ext.Trim() == ".ods")
            {
                //connection string for that file which extantion is .xls  
                ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (ext.Trim() == ".xlsx")
            {
                //connection string for that file which extantion is .xlsx  
                ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            //making query  
            string query = "SELECT * FROM [Sheet1$]";
            //Providing connection  
            OleDbConnection conn = new OleDbConnection(ConStr);
            //checking that connection state is closed or not if closed the   
            //open the connection  
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            //create command object  
            OleDbCommand cmd = new OleDbCommand(query, conn);
            // create a data adapter and get the data into dataadapter  
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            //fill the Excel data to data set  
            da.Fill(ds);
            //set data source of the grid view  
            gvExcelFile.DataSource = ds.Tables[0];
            //binding the gridview  
            gvExcelFile.DataBind();
            //close the connection  
            conn.Close(); 
  • What browser are you using? Also, in the code it says "Excel" but I think it should say OpenOffice Calc instead. I edited the question but did not change the code since this is frowned on at stackoverflow. – Jim K Jun 27 '16 at 17:17
  • i my office we have only open office installed In our systems now I should read the content in open office spread sheet and display it in gridview can you please help me I tried a lot but unable to do it it is very urgent requirement –  Jun 27 '16 at 17:32
  • Well, the fact that it is an urgent requirement is not relevant on stackoverflow -- we are volunteers only. Anyway what browser are you using? Also please edit the question and change the code to say "OpenOffice" or "Calc" instead of "Excel." – Jim K Jun 28 '16 at 00:07
  • Also i gave permissions to run unsafely by making it enabled but still i am not getting the output –  Jun 28 '16 at 01:37

1 Answers1

0

ActiveXObject will only work for Internet Explorer, as explained here.

For other browsers, Silverlight has AutomationFactory that might be able to do it. However, I did not try it because of the security risks involved.

Another way is to put the ReadExcel() function in a file ending in .js and then add the line ReadExcel(); at the end of the file. Set excelFile to the path of a spreadsheet file. I double-clicked to run it, and then it successfully loaded the OpenOffice document.

EDIT:

See if the following line causes the same error:

var objShell = new ActiveXObject("WScript.shell");

If so, then the problem is not related to OpenOffice or to your code. Perhaps you need to give Internet Explorer permission to run unsafely. See https://social.technet.microsoft.com/Forums/ie/en-US/8db7ec28-45ca-4859-b051-f0571a4da14e/error-automation-server-cant-create-object?forum=ieitpropriorver.

Community
  • 1
  • 1
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • if it worked for you then can you please modify my code it is working if both ms office and open office are installed on same pc if only open office is installed on the pc then it is showing an exception that automation server cannot create object –  Jun 27 '16 at 17:36
  • can you please help me out its urgent requirement to display open office spread sheet data in gridview –  Jun 27 '16 at 18:48
  • Also i gave permissions to run unsafely by making it enabled but still i am not getting the output –  Jun 28 '16 at 01:36
  • yes after changing the line also i got the same error –  Jun 28 '16 at 01:48