Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,


I am import excel and showing in repeater control. but it's returning an error External table is not in the expected format

my code is:

//Default.aspx page

ASP.NET
<table>
        <tr>
            <td>
                <asp:FileUpload ID="fileuploadExcel" runat="server" />  
            </td>
            <td>
                <asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
            </td>
        </tr>
    </table>
    <%--table--%>
        <asp:Repeater ID="RepDetails" runat="server">
            <HeaderTemplate>
                <table style="border: 1px solid #df5015; width: 500px" cellpadding="0">
                    <tr style="background-color: #df5015; color: White">
                        <td colspan="2">
                            Sales Order Record
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblComment" runat="server" Text='<%#Eval("CustomField") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblComment2" runat="server" Text='<%#Eval("CustomFields") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        <table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; border-bottom: 1px solid #df5015;
                            width: 500px">
                            <tr>
                                <td>
                                    User Name:
                                    <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>' />
                                </td>
                                <td>
                                    Education:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("Education") %>' />
                                </td>
                                 <td>
                                    Location:<asp:Label ID="Label1" runat="server" Font-Bold="true" Text='<%#Eval("Location") %>' />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                         
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        <div>
            <asp:Label ID="Label2" runat="server"  ></asp:Label>
        </div>

//c# page

C#
protected void btnImport_Click(object sender, EventArgs e)
    {
        string connString = "";
        string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
        string path = fileuploadExcel.PostedFile.FileName;

        //Connection String to Excel Workbook

        if (strFileType.Trim() == ".xls")
            connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        else if (strFileType.Trim() == ".xlsx")
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

        string query = "SELECT [UserName],[Education],[Location],[CustomField],[CustomFields] FROM [Sheet1$]";
        OleDbConnection conn = new OleDbConnection(connString);

        try
        {
           
            if (conn.State == ConnectionState.Closed)
                conn.Open();

            OleDbCommand cmd = new OleDbCommand(query, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            RepDetails.DataSource = ds.Tables[0];
            RepDetails.DataBind();

            da.Dispose();
            conn.Close();
            conn.Dispose();
        }
        catch (Exception ex)
        {
            Label2.Text = ex.Message;
        }
        finally
        {
            conn.Close();
            conn.Dispose();
        }

    }


Can you tell me where I went wrong?

Thanks in advance.
Posted
Updated 29-May-14 20:13pm
v2

1 solution

Try this

i got the same error long back, i used the below snippet to resolve it. :)

C#
if (version == "xlsx")
               SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "'; Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1'";
           else
               SourceConstr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + "; Extended Properties=Excel 8.0;";
 
Share this answer
 
Comments
U@007 30-May-14 3:00am    
Hi Karthik, it's not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900