http://bbs.bc-cn.net/dispbbs.asp?boardid=113&replyid=408150&id=106679&page=1&skin=0&Star=1
之前这个帖子我提问过,但是还没有得到很好的解决。看是看明白了。但是在用的时候还是不知道怎么用。因为我太菜了,而且缺乏经验。貌似里面的都是Winform的,但我希望做成ASPX的。最近考完期末试,继续对他的学习。经过我修改,现在的程序还是有点问题,希望各位大大帮忙修改一下!谢谢!
把他复制到一个ASPX文件下就可以直接调试了。
程序如下:
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.OleDb" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="System.Configuration" %>
<HTML>
<HEAD>
<title>excle</title>
<script language=c# runat=server>
void SubmitBtn_Click(object sender,System.EventArgs e)
{
string filePath=File1.Value;
string connExcel="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath+";Extended Properties=Excel 8.0";
OleDbConnection oleDbConnection = new OleDbConnection(connExcel);
oleDbConnection.Open();
DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string query="SELECT * FROM [Sheet1$]";
DataSet dataSet=new DataSet();
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(query,connExcel);
oleAdapter.Fill(dataSet,"gch_Class_Info");
DataGrid1.DataSource=dataSet;
DataGrid1.DataMember="gch_Class_Info";
DataGrid1.DataBind();
DataTable dataTable1 = new DataTable();
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection myConnection = new SqlConnection(con);
myConnection.Open();
SqlDataAdapter sqlDA1 = new SqlDataAdapter(@"SELECT * FROM gch_Class_Info",myConnection);
SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);
sqlDA1.Fill(dataTable1);
foreach(DataRow dataRow in dataSet.Tables["gch_Class_Info"].Rows)
{
DataRow dataRow1 = dataTable1.NewRow();
dataRow1["classDate"] = dataRow["日期"];
dataRow1["classPlace"] = dataRow["开课城市"];
dataRow1["classTeacher"] = dataRow["讲师"];
dataRow1["classTitle"] = dataRow["课程名称"];
dataRow1["durativeDate"] = dataRow["持续时间"];
dataTable1.Rows.Add(dataRow1);
}
Console.WriteLine("新插入 " + dataTable1.Rows.Count.ToString() + " 条记录");
sqlDA1.Update(dataTable1);
myConnection.Close();
oleDbConnection.Close();
}
</script>
</Head>
<body>
<form id="Form1" method="post" runat="server">
<H3>How to read Excel</H3>
Please select Excel form:<INPUT type="file" id="File1" runat="server"><br>
<asp:Button id="SubmitBtn" runat="server" Text="View" Onclick="SubmitBtn_Click"> </asp:Button><br><br>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</HTML>