C#中如何用DataList绑定excel的数据
展开全部
C#中用dataList绑定exel的数据的方法:
1、display.asp代码
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Has Header ?" />
<asp:RadioButtonList ID="rbHDR" runat="server">
<asp:ListItem Text = "Yes" Value = "Yes" Selected = "True" >
</asp:ListItem>
<asp:ListItem Text = "No" Value = "No"></asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server"
OnPageIndexChanging = "PageIndexChanging" AllowPaging = "true">
</asp:GridView>
2、绑定excel的配置文件webConfig.xml:
<connectionStrings>
<add name ="Excel03ConString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};
Extended Properties='Excel 8.0;HDR={1}'"/>
<add name ="Excel07ConString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties='Excel 8.0;HDR={1}'"/>
</connectionStrings>
3、后端c#代码:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
FileUpload1.SaveAs(FilePath);
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
}
}
1、display.asp代码
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClick="btnUpload_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Has Header ?" />
<asp:RadioButtonList ID="rbHDR" runat="server">
<asp:ListItem Text = "Yes" Value = "Yes" Selected = "True" >
</asp:ListItem>
<asp:ListItem Text = "No" Value = "No"></asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server"
OnPageIndexChanging = "PageIndexChanging" AllowPaging = "true">
</asp:GridView>
2、绑定excel的配置文件webConfig.xml:
<connectionStrings>
<add name ="Excel03ConString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};
Extended Properties='Excel 8.0;HDR={1}'"/>
<add name ="Excel07ConString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
Extended Properties='Excel 8.0;HDR={1}'"/>
</connectionStrings>
3、后端c#代码:
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
FileUpload1.SaveAs(FilePath);
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
}
}
展开全部
OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=zk.xls; Extended Properties=Excel 8.0");
conn.Open();
string strsql="select * from [Sheet1$]";//如果有多张表 可用conn.GetOleDbSchemaTable取得一个集合,然后再动态的和数据控件绑定
OleDbCommand cmd=new OleDbCommand (strsql,conn);
OleDbDataAdapter ad=new OleDbDataAdapter ();
ad.selectCommand=cmd;
Datatable dt=new Datatable();
ad.fill(dt);
DataGridView1.Datasource=dt;//数据源与数据控件邦定
conn.Open();
string strsql="select * from [Sheet1$]";//如果有多张表 可用conn.GetOleDbSchemaTable取得一个集合,然后再动态的和数据控件绑定
OleDbCommand cmd=new OleDbCommand (strsql,conn);
OleDbDataAdapter ad=new OleDbDataAdapter ();
ad.selectCommand=cmd;
Datatable dt=new Datatable();
ad.fill(dt);
DataGridView1.Datasource=dt;//数据源与数据控件邦定
追问
怎么绑定到DataList中,前台怎么绑定字段
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询