Search code examples
c#asp.netdatabaseoledb

Failed to create file - excel to gridview


I have the next code:

   <asp:gridview id="GridView1" runat="server" cellpadding="6" gridlines="None" 
    bordercolor="#336699" borderstyle="Solid" borderwidth="1px">

    <headerstyle backcolor="#336699" font-bold="True" forecolor="White" />
    </asp:gridview>

And code behind:

    protected void Page_Load(object sender, EventArgs e)
{
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|App_Data|Machreta.xls;Extended Properties='Excel 8.0;HDR=Yes;'";

    string query = "SELECT * FROM [Sheet1$]";

    DataSet excelDataSet = new DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);

    da.Fill(excelDataSet);

    GridView1.DataSource = excelDataSet;
    GridView1.DataBind();

}

I'm get the error:

Failed to create file

To the line:

    da.Fill(excelDataSet);

why?


Solution

  • Try using:

    string strConn = 
        @"Provider=Microsoft.Jet.OLEDB.4.0;" +
        @"Data Source=|DataDirectory|\Machreta.xls;" +
        @"Extended Properties='Excel 8.0;HDR=Yes;'";
    

    or

    string strConn = 
        @"Provider=Microsoft.Jet.OLEDB.4.0;" +
        @"Data Source=|App_Data|\Machreta.xls;" +
        @"Extended Properties='Excel 8.0;HDR=Yes;'";
    

    I splitted strings over many rows just for easy reading.