时间:2021-07-01 10:21:17 帮助过:24人阅读
%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel_CS.aspx.cs" Inherits="ReadExcel_CS" % !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel_CS.aspx.cs" Inherits="ReadExcel_CS" %>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class ReadExcel_CS : System.Web.UI.Page
{
public DataTable DT;
protected void Page_Load(object sender, EventArgs e)
{
//read excel file
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C://test.xls;" +
"Extended Properties=Excel 8.0;";
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
this.GridView1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
this.GridView1.DataBind();
DT = (DataTable)myDataSet.Tables["ExcelInfo"];
/*
//read access file
string strConn2;
strConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c://test.mdb;" +
"Persist Security Info=True;";
OleDbDataAdapter myCommand2 = new OleDbDataAdapter("select * from test", strConn2);
DataSet myDataSet2 = new DataSet();
myCommand2.Fill(myDataSet2, "AccessInfo");
this.GridView2.DataSource = myDataSet2.Tables["AccessInfo"].DefaultView;
this.GridView2.DataBind();
*/
}
protected void Button1_Click(object sender, EventArgs e)
{
/*
//写入数据库david
for (int i = 1; i < GridView1.Rows.Count; i++)
{
for (int j = 1; j < 3 ; j++)
{
string a = GridView1.Rows[i].Cells[j].ToString() ;
Response.Write(a.ToString() );
}
}
*/
for (int i = 0; i < DT.Rows.Count; i++)
{
//for (int j = 0; j < DT.Columns.Count; j++)
//{
string A=DT.Rows[i][0].ToString();
string B= DT.Rows[i][1].ToString();
string C = DT.Rows[i][2].ToString();
string D = DT.Rows[i][3].ToString();
string sqlinsert = "insert TEST(A,B,C,D)values('"+A+"','"+B+"','"+C+"','"+D+"')";
DataAccess.DataAccessSHDB.GetNonQuery(sqlinsert.ToString());
//Response.Write(a.ToString()+",");
//}
}
}
}