急求ASP.NET操作EXCEL的资料,有的相关资料的贴出来啊,先谢谢了啊!! 本人在做一个查询系统,读EXCEL数据至GridView,再将GridView送入oracle数据库或导出EXCEL,急求ASP.NET操作EXCEL的资料,有的相关资料的贴出来啊,先谢谢了啊!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 protected void binDis_Click(object sender, EventArgs e) { if (filePath.PostedFile.FileName != "") { //取得文件路径 txtFilePath = filePath.PostedFile.FileName; fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".") + 1, 3); if (fileExtName != "xls" && fileExtName != "XLS") { Response.Write(Functions.Alert("请选择Execl文件")); } else { ViewState["path"] = txtFilePath = filePath.PostedFile.FileName; string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(connstr); conn.Open(); OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn); DataSet ds = new DataSet(); da.Fill(ds); DataGrid2.DataSource = ds; DataGrid2.DataBind(); conn.Close(); btnInsert.Visible = true; } } else { Response.Write(Functions.Alert("请浏览Execl文件")); } } protected void btnInsert_Click(object sender, EventArgs e) { //txtFilePath = filePath.PostedFile.FileName; txtFilePath = ViewState["path"].ToString(); string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(connstr); conn.Open(); OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]",conn); OleDbConnection conn1 = DB.createconnection(); conn1.Open(); OleDbTransaction trans = conn1.BeginTransaction(); try { using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { string sql = "insert into student(S_ID,S_Name,S_Pwd,S_Gender,S_Nation,S_Birthday,S_Time,S_ClassID) values('" + dr[0].ToString() + "','" + dr[1].ToString() + "','" + dr[0].ToString()+ "','" + dr[2].ToString() + "','" + dr[3].ToString() + "','" + dr[4].ToString() + "','" + dr[5].ToString() + "','" + dr[6].ToString() + "')"; OleDbCommand cmd1 = new OleDbCommand(sql, conn1); cmd1.Transaction = trans; cmd1.ExecuteNonQuery(); } } trans.Commit(); Response.Write(Functions.Alert("导入成功!!")); btnInsert.Enabled=false; BindData(); } catch (Exception ex) { trans.Rollback(); Response.Write(Functions.Alert("导入失败...."+ex.Message)); } finally { conn.Close(); conn1.Close(); } } Excel是可以用OLEDB连上的楼上很详细了,连上后,操作就简单了 怎么实现点击下一页只让iframe里面刷新 水晶报表 求教多个系统之间的数据交换与共享目前最常用的方法? asp.net datagrid 关于不同页选中checkbox记忆 怎样将动态生成的asp.net网页转为静态的html网页 html控件 与服务器端控件 ReportViewer使用出错???? webcombo控件选中值如何取?菜鸟,分不多,大家帮忙,谢了 急问:在线,各位高手看看这是什么原因啊? Dll如何放到Bin目录下的子目录? (100分) JS中控件取值时写 <%=%> 出错的问题,高手请指教 Vista下VS2005用File System建立Web项目是无法运行
protected void binDis_Click(object sender, EventArgs e)
{
if (filePath.PostedFile.FileName != "")
{
//取得文件路径
txtFilePath = filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".") + 1, 3);
if (fileExtName != "xls" && fileExtName != "XLS")
{
Response.Write(Functions.Alert("请选择Execl文件"));
}
else
{
ViewState["path"] = txtFilePath = filePath.PostedFile.FileName;
string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid2.DataSource = ds;
DataGrid2.DataBind();
conn.Close();
btnInsert.Visible = true;
}
}
else
{
Response.Write(Functions.Alert("请浏览Execl文件"));
}
}
protected void btnInsert_Click(object sender, EventArgs e)
{
//txtFilePath = filePath.PostedFile.FileName;
txtFilePath = ViewState["path"].ToString();
string connstr = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + txtFilePath + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(connstr);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]",conn);
OleDbConnection conn1 = DB.createconnection();
conn1.Open();
OleDbTransaction trans = conn1.BeginTransaction();
try
{
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string sql = "insert into student(S_ID,S_Name,S_Pwd,S_Gender,S_Nation,S_Birthday,S_Time,S_ClassID) values('" + dr[0].ToString() + "','" + dr[1].ToString() + "','" + dr[0].ToString()+ "','" + dr[2].ToString() + "','" + dr[3].ToString() + "','" + dr[4].ToString() + "','" + dr[5].ToString() + "','" + dr[6].ToString() + "')";
OleDbCommand cmd1 = new OleDbCommand(sql, conn1);
cmd1.Transaction = trans;
cmd1.ExecuteNonQuery();
}
}
trans.Commit();
Response.Write(Functions.Alert("导入成功!!"));
btnInsert.Enabled=false;
BindData();
}
catch (Exception ex)
{
trans.Rollback();
Response.Write(Functions.Alert("导入失败...."+ex.Message));
}
finally
{
conn.Close();
conn1.Close();
}
}
楼上很详细了,连上后,操作就简单了