asp.net读取excel中的数据并绑定在gridview

 这篇文章主要介绍了asp.net读取excel中的数据并绑定在gridview上的方法,需要的朋友可以参考下

 

 
 

前台label,DropDownList,gridview控件

aspx.cs核心代码:
代码如下:

using System.Data.OleDb;//需要引入命名
public void Excel_Click(object sender, EventArgs e)
{
if (this.AttachmentFile.Value == "" && this.Label1.Text == "" && DropDownList2.SelectedValue == "")
{
Response.Write("<script>window.alert('请选择要导入的文件')</script>");
}
if (this.AttachmentFile.Value != "" && this.DropDownList2.SelectedValue == "")
{
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
postedFile.SaveAs("localhost文件夹" + fileName);
}
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "localhost文件夹" + fileName + ";Extended Properties=Excel 8.0;";//this.AttachmentFile.Value.ToString()
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable sheetNames = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in sheetNames.Rows)
{
DropDownList2.Items.Add(dr[2].ToString());
}
this.Label1.Text = "localhost文件夹" + fileName;//this.AttachmentFile.Value.ToString();
conn.Close();
}
if (this.Label1.Text.ToString() != "" && this.DropDownList2.SelectedValue != "")// && this.DropDownList1.SelectedValue.ToString() != "全部"
{

//绑定到gridview
GridView1.DataSource = createDataSource(DropDownList2.SelectedValue.ToString(), this.Label1.Text.ToString());//, this.DropDownList1.SelectedValue.ToString()
GridView1.DataBind();

}

}
//以Excel为数据源获取数据集
private DataSet createDataSource(string select, string lable)

{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lable + ";Extended Properties=Excel 8.0;";
string strsql = "select 登记号码,姓名,日期,签到时间,签退时间,部门 from [" + select + "] order by 部门,日期,姓名";//excel表格的字段
OleDbConnection conn = new OleDbConnection(strCon);
OleDbDataAdapter da = new OleDbDataAdapter(strsql, conn);
try
{
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
catch (Exception e)
{
Response.Write("<script>window.alert('没有数据,或者" + e.Message + "')</script>");
return null;
}
}

以上是插入07以前版本excel

如果07版本以后只需要做小小修改
代码如下:

string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + lable + ";Extended Properties=Excel 12.0;"; 

时间: 2024-07-31 21:50:51

asp.net读取excel中的数据并绑定在gridview的相关文章

asp.net读取excel中的数据并绑定在gridview_实用技巧

前台label,DropDownList,gridview控件 aspx.cs核心代码: 复制代码 代码如下: using System.Data.OleDb;//需要引入命名 public void Excel_Click(object sender, EventArgs e) { if (this.AttachmentFile.Value == "" && this.Label1.Text == "" && DropDownLis

C#如何读取Excel中的数据

#region 读取Excel中的数据 02./// <summary> 03./// 读取Excel中的数据 04./// </summary> 05./// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param> 06./// <returns>Excel中的数据</returns> 07.private DataT

C# 读取Excel中的数据

#region 读取Excel中的数据 /// <summary> /// 读取Excel中的数据 /// </summary> /// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param> /// <returns>Excel中的数据</returns> private DataTable GetTable(stri

c++-读取Excel中字段数据,格式为日期,但读出来与表格中日期不一样

问题描述 读取Excel中字段数据,格式为日期,但读出来与表格中日期不一样 if(ret.vt==VT_DATE) //时间格式 { SYSTEMTIME st; VariantTimeToSystemTime((long)&ret.date, &st); CString date; date.Format("%d/%d/%d %d:%d:%d",st.wYear,st.wMonth, st.wDay,st.wHour,st.wMinute,st.wSecond,st.

问题: asp.net 读取excel 模版 填充数据 另存为

问题描述 如题:用asp.net读取excel模版在填充数据然后另存为html,怎么作,最好有封装好的类, 解决方案 解决方案二:没做过.做个标记,帮顶下.解决方案三:不用封装类,就像操作Access数据库一样,只不过连接字符串有点不一样,如下:Provider=Microsoft.Jet.OleDb.4.0;DataSource="+Excel路径+";ExtendedProperties=Excel8.0;";解决方案四:唉,自己顶一下,希望高手出现.解决方案五:唉,自己

ASP.NET中如何用程序将excel中的数据导入到数据库中(C#)

问题描述 ASP.NET中如何用程序将excel中的数据导入到数据库中(C#),比较不同的时,excel只有一个表,里面是全部的详细信息,导入到sqlserver中的是多个表,这个如何实现?希望有具体的例子! 解决方案 解决方案二:大致原理:先将EXCEL导入GridView,然后再操作GridView中的数据存入数据库相应表中!在这里不能给你提供原码了,但你可以在Google内收到很多!解决方案三:用的是vs.net2003啊,应该如何?解决方案四:以下是我的一个真实的例子,供参考!<tr>

以前收集的一些资料---如何在ASP中通过ODBC调用Excel中的数据

excel|odbc|数据                如何在ASP中通过ODBC调用Excel中的数据    在ASP中通过ODBC调用Excel中的数据方式和普通的通过ODBC调用数据库的方式有一些不同的地方.下面将介绍如何通过ODBC在ASP中使用Excel的数据.    在使用的过程中,要注意的是你需要使用的ODBC驱动为:Microsoft ODBC Driver for Excel. 下面给出一个例子来具体说明如何调用的过程:1.在Excel的电子表格中新建一个叫Range的表单a

如何在ASP中通过ODBC调用Excel中的数据

excel|odbc|数据 http://www.chinaasp.com/ 在ASP中通过ODBC调用Excel中的数据方式和普通的通过ODBC调用数据库的方式有一些不同的地方.下面将介绍如何通过ODBC在ASP中使用Excel的数据. 在使用的过程中,要注意的是你需要使用的ODBC驱动为:Microsoft ODBC Driver for Excel. 下面给出一个例子来具体说明如何调用的过程:1.在Excel的电子表格中新建一个叫Range的表单a)在Excel文件ADOtest.xls中

利用asp.net从数据库中传出数据,导入EXCEL中

问题描述 Excel具有强大的报表等功能,通过对excel和VBA进行设计和编程可以制作强大的报表系统.毕业设计管理系统软件在完成主要的业务处理后,需要处理大量的不同需求的报表.本课题在调查毕业设计系统报表需求的基础上,利用asp.net从数据库中传出数据,导入EXCEL中,制做毕业设计管理系统的所有报表,以及打印功能 解决方案 解决方案二:henhaozuodejietieba