protected void Button1_Click(object sender, System.EventArgs e)
{
string XLS_Path="";
string XLS_Name = MakeFileName();
string fileExtName = "";
if(File1.PostedFile.ContentLength >0)
{
try
{
fileExtName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("."));
if(fileExtName!=".xls"||fileExtName.Length<1)
{
Response.Write( "");
return;
}
File1.PostedFile.SaveAs(Server.MapPath("../uploadexcel/")+XLS_Name+fileExtName);
XLS_Path =Server.MapPath("../uploadexcel/")+ XLS_Name+fileExtName;
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
Response.Write( "");
return;
}
string ConStr=System.Configuration.ConfigurationManager.AppSettings["ConnString"];
SqlConnection Conn=new SqlConnection(ConStr);
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '"+XLS_Path+"'"+";Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
try
{
myDa.Fill(myDs);
}
catch
{
Response.Write( "");
return;
}
if(myDs.Tables[0].Rows.Count<=0)
{
Response.Write( "");
return;
}
int []zjid=new int[myDs.Tables[0].Rows.Count];
string strSql = "";
string CnnString="Provider=SQLOLEDB;"+ConStr;
OleDbConnection conn =new OleDbConnection(CnnString);
OleDbCommand myCmd =null;
楼上的我不说了,我只用java+jdbc的另一种思路完成你的设计,你应该知道怎么用java去读取excel的数据吧,这个网上一大推代码,我不说了,你就一个挨个读出来然后插入! 如
数据库表 user 列 user,password
那么excel就设计为 :
第一行 user password
第二行 a1 p1
第三行 a2 p2
java
int row = ... (用jiava读取excel的行数);
String sql = "insert into user "
for(int i=1;i
String password= xx.get..(1,i); (获得第(i+1)行的第二列 (即password))
sql += " select " + user + "," + password ;
if(i!=(row-1)){
sql += " union ";
}
}
..executeUpdate(sql);
......
using System.Data.OleDb; //必须添加
连接语句
OleDbConnection oCon = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source=D:\My Documents\SanGuo\WJXJ.xls;Extended Properties=Excel 8.0");
其中Source为excel的地址。
string Str = "select * from [wj$] where BH= 7";
OleDbCommand Cmd = new OleDbCommand(Str, oCon);
OleDbDataReader Re = Cmd.ExecuteReader();
上述语句的作用是从wj这个标签从找出BH列为7的行,和使用Access数据库一样,然后再添加就不用说了吧