I have the next code:
<asp:gridview id="GridView1" runat="server" cellpadding="6" gridlines="None"
bordercolor="#开发者_StackOverflow336699" 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?
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.
精彩评论