求教。将Excel文件导入DataTable中,然后绑定到GridView中。
2012-10-22 22:02
429 查看
private System.Data.DataTable GetExcelTable(string uploadPath)
{
DataSet ds = new DataSet();
string Xls_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";//HDR为yes 则第一数据行为列名,为no 则自动为列加列名F1 F2 F3
OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);
try
{
Conn.Open();
string sql_str = "select * from [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql_str, Conn);
da.Fill(ds, "excel_data");
Conn.Close();
}
catch
{
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
return null;
}
finally
{
Conn.Dispose();
}
if (ds == null)
{
return null;
}
if (ds.Tables.Count < 1)
{
return null;
}
return ds.Tables[0];
}
dt=GetExcelTable(ooxx);
DataGridView1.DataSource=dt.DefaultView;
{
DataSet ds = new DataSet();
string Xls_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";//HDR为yes 则第一数据行为列名,为no 则自动为列加列名F1 F2 F3
OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);
try
{
Conn.Open();
string sql_str = "select * from [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sql_str, Conn);
da.Fill(ds, "excel_data");
Conn.Close();
}
catch
{
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
return null;
}
finally
{
Conn.Dispose();
}
if (ds == null)
{
return null;
}
if (ds.Tables.Count < 1)
{
return null;
}
return ds.Tables[0];
}
dt=GetExcelTable(ooxx);
DataGridView1.DataSource=dt.DefaultView;
相关文章推荐
- 将Excel文件中的数据导入GridView
- EXCEL导入GridView,然后再汇入数据库.
- 将Dataset中的多个DataTable导入到一个Excel文件的多个Sheet中
- GridView导出为Excel后,导出的.xls文件无法作为源文件导入的问题
- [导入]ASP.NET 2.0中将 GridView 导出到 Excel 文件中
- EXCEL导入GridView,然后再汇入数据库.
- 给予POI机制的把Excel文件导入Oracle,数据全部读入list,然后遍历list插入oracle
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- 如何从Excel文件中导入所有Sheet到DataTable中?
- DataTable 对xml、excel、csv 对文件的导入和导出功能
- EXCEL导入GridView,然后再汇入数据库.
- 把数据从DataTable导入到Excel文件里并创建表
- Gridview读取Excel文件中的数据,并将其导入数据库
- 把EXCEL文件导入到GridView,GridView根据要求动态的增加列(转)
- DataTable导入到Excel文件
- 【C#.NET】将excel或将CSV文件中的sheet1导入到dataGridView1中 ,或者导入到DataTable,导入到DataSet
- 导入Excel后绑定GridView实例
- GridView表中数据导入excel文件中
- gridview读取Excel文件中的数据,并将其导入数据库
- 把EXCEL文件导入到GridView,GridView根据要求动态的增加列!