當前位置:首頁 » 編程語言 » aspexcel導入sql

aspexcel導入sql

發布時間: 2022-05-21 22:17:10

① 在ASP中如何將Excel表中數據導入到sql2000中

上邊那個Function Open_Conn(SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName,SqlConn)
是連接sql資料庫的函數,可以這樣調用:
Call Open_Conn("Shat_EDG","sa","sa","(local)",SqlConn) '打開SQL Server資料庫連接

連接Excel的連接函數要重新寫的,可以這樣寫:
function open_excel_conn(filename)
on error resume next
Set conn=server.createobject("adodb.connection")
badgirl="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.Mappath(filename)&";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"
conn.open badgirl
If Err.Number <> 0 Then
Response.write "不能打開指定的Excel,請查實!<a href='upexcel.htm'>返回</a>"
Response.end
else
On Error GoTo 0
end if
end function

你可以在代碼的最後繼續寫
call open_excel_conn(Trim(Request("File")))

② ASP如何將EXCEL表導入SQL2000中

其實MSSQL2000以上是支持直接導入Excel的,你可以進入DTS導入導出工具
在選擇數據源時,請選擇Microsoft
Excel,然後下面的路徑選擇你要導入文件所在目錄!
注意Excel版本及下面的首行包含列名稱!是否有根據你的Excel選擇,沒有就不要選!
接下去一步一步按引導來做就行!

③ asp.net實現excel導入到sql

首先這個文件必須上傳到伺服器,然後讀取裡面的內容放在ds裡面,然後在導入sql
具體做法:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;

namespace ETable
{
/// <summary>
/// ETable 的摘要說明。
/// </summary>
public class ETable : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public Random rd;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
}

#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
if (File1.PostedFile!=null)
{ rd=new Random(1);
string filename=DateTime.Now.Date.ToString("yyyymmdd")+DateTime.Now.ToLongTimeString().Replace(":","")+rd.Next(9999).ToString()+".xls";
File1.PostedFile.SaveAs(@Server.MapPath("file/")+filename);
//Response.Write(File1.PostedFile.FileName.ToString());
//Response.Write("上傳成功");
Label1.Text="文件名為"+filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+Server.MapPath("file")+"/"+filename+";Extended Properties=Excel 8.0" ;
OleDbConnection thisconnection=new OleDbConnection(conn);
thisconnection.Open();
string Sql="select * from [Sheet1$]";
OleDbDataAdapter mycommand=new OleDbDataAdapter(Sql,thisconnection);
DataSet ds=new DataSet();
mycommand.Fill(ds,"[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

string conn1="User ID=sa;Data Source=127.0.0.1;Password=sa;Initial Catalog=index;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1=new OleDbConnection(conn1);
thisconnection1.Open();
int count=ds.Tables["[Sheet1$]"].Rows.Count;

for (int i=0;i<count;i++)
{
string id,id_1,id_2,id_3;
id=ds.Tables["[Sheet1$]"].Rows[i]["id"].ToString();
id_1=ds.Tables["[Sheet1$]"].Rows[i]["id_1"].ToString();
id_2=ds.Tables["[Sheet1$]"].Rows[i]["id_2"].ToString();
id_3=ds.Tables["[Sheet1$]"].Rows[i]["id_3"].ToString();
string excelsql="insert into excel(id,id_1,id_2,id_3) values ('"+id+"','"+id_1+"','"+id_2+"','"+id_3+"') ";
OleDbCommand mycommand1=new OleDbCommand(excelsql,thisconnection1);
mycommand1.ExecuteNonQuery();
}
Response.Write("更新成功");
thisconnection1.Close();

//FileStream fileStream = new FileStream(@Server.MapPath("file/")+filename);
System.IO.File.Delete(@Server.MapPath("file/")+filename);

}
}
}
}

C#

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;

namespace ETable
{
/// <summary>
/// ETable 的摘要說明。
/// </summary>
public class ETable : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public Random rd;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
}

#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
if (File1.PostedFile!=null)
{ rd=new Random(1);
string filename=DateTime.Now.Date.ToString("yyyymmdd")+DateTime.Now.ToLongTimeString().Replace(":","")+rd.Next(9999).ToString()+".xls";
File1.PostedFile.SaveAs(@Server.MapPath("file/")+filename);
//Response.Write(File1.PostedFile.FileName.ToString());
//Response.Write("上傳成功");
Label1.Text="文件名為"+filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+Server.MapPath("file")+"/"+filename+";Extended Properties=Excel 8.0" ;
OleDbConnection thisconnection=new OleDbConnection(conn);
thisconnection.Open();
string Sql="select * from [Sheet1$]";
OleDbDataAdapter mycommand=new OleDbDataAdapter(Sql,thisconnection);
DataSet ds=new DataSet();
mycommand.Fill(ds,"[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

string conn1="User ID=sa;Data Source=127.0.0.1;Password=sa;Initial Catalog=index;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1=new OleDbConnection(conn1);
thisconnection1.Open();
int count=ds.Tables["[Sheet1$]"].Rows.Count;

for (int i=0;i<count;i++)
{
string id,id_1,id_2,id_3;
id=ds.Tables["[Sheet1$]"].Rows[i]["id"].ToString();
id_1=ds.Tables["[Sheet1$]"].Rows[i]["id_1"].ToString();
id_2=ds.Tables["[Sheet1$]"].Rows[i]["id_2"].ToString();
id_3=ds.Tables["[Sheet1$]"].Rows[i]["id_3"].ToString();
string excelsql="insert into excel(id,id_1,id_2,id_3) values ('"+id+"','"+id_1+"','"+id_2+"','"+id_3+"') ";
OleDbCommand mycommand1=new OleDbCommand(excelsql,thisconnection1);
mycommand1.ExecuteNonQuery();
}
Response.Write("更新成功");
thisconnection1.Close();

}
}
}
}

④ ASP代碼中如何把EXCEL數據導入到SQL SERVER資料庫中

'定義打開Excel表格的函數
Function OpenExcel(path)
dim excel,rs,strsql
On Error Resume Next
Set rs = Server.CreateObject("ADODB.RecordSet")
Set excel = Server.CreateObject("ADODB.Connection")
excel.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & path
If Err.number<> 0 Then
Response.Write "請檢查上傳的Excel文件內部格式,文件無法打開,導入失敗!"
Response.End
End If
strsql = "SELECT * FROM [Sheet1$]" '在這里指定工作薄名稱,默認是Sheet1$
Set rs = excel.Execute(strsql)
Set OpenExcel = rs
End Function

'讀取文件中的內容
Dim rsInfo
Set rsInfo = Server.CreateObject("ADODB.RecordSet")
Set rsInfo = OpenExcel("E:/a.xls") '這里的文件路徑請用Server.Path來獲取

'檢查讀取結果
If rsInfo.State<> 1 Then
Response.Write "請檢查Excel文件中的工作表命名是否為Sheet1,導入失敗!"
Response.End
End If

If rsInfo.EOF And rsInfo.BOF Then
Response.Write "沒有找到Excel表中的數據,導入失敗!"
Response.End
End If

If IsNull(rsInfo.Fields(0)) or Trim(rsInfo.Fields(0))="" Then
Response.Write "沒有找到Excel表中的數據,導入失敗!"
Response.End
End If

'這里指定導入數據的列數,列數少了退出
If rsInfo.Fields.Count< 7 Then
Response.Write "Excel表中的數據列數不正確,導入失敗!"
Response.End
End If

'創建資料庫連接
dim dbrs,conn,sql
Set conn = Server.CreateObject("ADODB.Connection")
Set dbrs = Server.CreateObject("ADODB.Recordset")
'注: G_DB_ConnectString是連接資料庫的字元串,自己定義
conn.ConnectionString = G_DB_ConnectString
conn.Open '打開資料庫連接

'創建臨時表
sql = "IF EXISTS (SELECT * FROM sysobjects WHERE xtype='U' and name='tmp_PartRes') "
sql = sql & "BEGIN Drop table tmp_PartRes END "
sql = sql & "Create table tmp_PartRes([ID] int identity(1,1),"
sql = sql & "PartID varchar(100),Brand varchar(100),[Package] varchar(100),"
sql = sql & "BatchNo varchar(100),[Price] varchar(100),[Stock] varchar(100) default('0'),"
sql = sql & "Brief varchar(100),StockFlag int default(1),"
sql = sql & "SuperFlag int default(1),SaleFlag int default(1))"
conn.execute sql

'取表結構 注意: 只取表的結構, 不要數據, 因為我這個是剛創建的臨時表, 沒有數據,
'如果表中存在數據, 要注意加上條件句, 防止取到數據 如: where ID = -1
sql = "SELECT * FROM tmp_PartRes"
dbrs.CursorLocation = 3 '這一定要設置為3
dbrs.Open sql,conn, 3, 4 '這里的參數必須是3和4

'取到表結構後, 必須要把活動連接及資料庫連接關閉,這個很重要, 否則導入速度特慢.
Set dbrs.ActiveConnection = Nothing
conn.close

'提取Excel中的數據, 將excel中的數據放入到資料庫表中.
While Not rsInfo.EOF
If Trim(rsInfo.Fields(0))<> "" Then
dbrs.AddNew
dbrs("PartID") = Ucase(Trim(rsInfo.Fields(0)))
dbrs("Brand") = Trim(rsInfo.Fields(1))
dbrs("Package") = Trim(rsInfo.Fields(2))
dbrs("BatchNo") = Trim(rsInfo.Fields(3))
dbrs("Price") = Trim(rsInfo.Fields(4))
If Trim(rsInfo.Fields(5))<>"" Then
dbrs("Stock") = Trim(rsInfo.Fields(5))
Else
dbrs("Stock") = "0"
End If
dbrs("Brief") = Trim(rsInfo.Fields(6))
End If
rsInfo.MoveNext
Wend

'更新記錄集到資料庫臨時表
conn.Open '打開連接
dbrs.ActiveConnection = conn
dbrs.UpdateBatch '批量更新函數

'更新完成後, 關閉連接
dbrs.Close
Set dbrs = Nothing
rsInfo.Close
Set rsInfo = Nothing

⑤ 如何將excel數據通過asp導入資料庫

將excel數據通過asp導入資料庫:
參考代碼:
wenjian=request.Form("floor")
fileext=mid(wenjian,InStrRev(wenjian,".")+1)
if lcase(fileext)<>"xls" then
response.write "<script>alert ('文件格式不對,請上傳Excel文件');window.location.href='updateFloor.asp';</script>"
response.end
end if
set conne=server.CreateObject("ADODB.Connection")
connStre="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath( ""&wenjian&"" )&";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"
conne.open connStre
Sqle="select * from [sheet1$] "
Set rse = Server.CreateObject("ADODB.Recordset")
rse.open sqle,conne,1,1
'驗證
hang=2
do while not rse.eof
'名稱不能為空
if trim(rse(0))<>"" then
else
mess="第"& hang &"行名稱為空,請檢查!"
response.Write"<script>alert('"& mess &"').window.location.href='updateFloor.asp'</script>"
response.End()
end if
rse.movenext
hang=hang+1
loop
rse.movefirst
do while not rse.eof
set rst=server.CreateObject("adodb.recordset")
sqlt="select * from Sellman"
rst.open sqlt,conn,1,3
rst.addnew()
rst("CompanyName")=c2(rse(0))
rst("CompanyInfo")=c2(rse(1))
rst("address")=c2(rse(2))
rst("tel")=c2(rse(3))&" "&c2(rse(7))
rst("Fax")=c2(rse(4))
rst("linkman")=c2(rse(5))
rst("Homepage")=c2(rse(8))
rst("Email")=c2(rse(6))
rst.update()
rst.close
set rst=nothing
rse.movenext
loop
rse.close
set rse=nothing
response.Write "<script>alert('導入成功!');location.href='updateFloor.asp';</script>"

⑥ 求教: ASP把excel導入SQL資料庫時怎麼才能忽略前幾行

你這個思路有點繞啊,你為什麼要在sql里寫,完全可以在代碼里略過前幾行啊。
而且,你如果在sql里寫成固定的,也不夠靈活,寫在代碼里可以設計成從配置里取到底要略過幾行,增加程序的健壯性。

⑦ 急~~~~在ASP中怎樣將Excel中的數據導入並保存在Sqlserver2000中

可以把excel當做數據源進行連接,然後正常遍歷記錄集操作即可

strXlsConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& objBase.getAppFolder&"temp\"&strNewFileName & ";Extended Properties=""Excel 8.0;HDR=NO;"""
Set rsXls = Server.CreateObject("ADODB.RecordSet")
rsXls.Open "SELECT * FROM [sheet1$]",strXlsConn,1,1
Do while not rsxls.eof
fld1 = rsXls("f1")
fld2 = rsXls("f8")
...
//整理插入操作sql資料庫表的代碼
rsxls.movenext
Loop
....

⑧ 如何用asp.net將excel中數據批量導入到sqlserver2005中

兩步:1,上傳EXCEL到網站目錄2,使用分布式查詢導入數據

⑨ asp中怎麼導入excel到SQL中

Set db = Server.CreateObject("ADODB.Connection")
db.Open "Driver={Microsoft Excel Driver (*.xls)};Dbq=" & Server.MapPath(FileName)
'打開記錄集,表名一定要以"[表名$]"的格式
strSql="Select * From [Sheet1$]"
Set rsexcel=db.Execute(strSql)
Set rs = Server.CreateObject("ADODB.RecordSet")
SQL = "Select * From [users]"
rs.Open SQL, Conn, 1, 3
N = rs.fields.count-1'數據表欄位數
M = rsexcel.fields.count-1'excel表裡欄位數
Redim L(N)'定義數據表數組
Redim arr(M)'定義excel表數組

'把數據表裡的欄位表存入數組L中
For k=0 To N
L(k) = Rs(k).name
next
'把excel表裡的欄位存入數組arr中
for p=0 to M
arr(p) = rsexcel(p).name
next
'取數組長度
x=UBound(l)
y=UBound(arr)

j=0
startime=timer() '開始時間
Do While Not rsexcel.EOF

rs.AddNew
'循環讀取所有行
for i=6 to x'對數據表進行循環 rs(0)是 id 所以跳過
if l(i)=arr(j) then'通過數組判斷兩表欄位是否相同,如相同對其進行賦值,不相同置0
if Trim(rsexcel(j))<>"" then
'response.write Trim(rsexcel(j))&"|"
rs(i)=Trim(rsexcel(j))
else
rs(i)=0
end if
if j< M then '當execl表數據是最後一項時不能在加1了
j=j+1
else
j=j
end if
else
rs(i)=0
end if
rs(3)=typeid
Next
rsexcel.MoveNext
rs.update
j=0
Loop
endtime=timer()
'關閉對象
rsexcel.Close
Set rsexcel=nothing
db.Close
Set db=Nothing

⑩ asp中將excel導入到sql時遇到的問題

根據你說的情況判斷,可以是下面的原因:
1. 你在你的筆記本電腦上設置了Excel連接驅動,而在另外一台電腦上並沒有設置對應的Excel連接驅動。
2. 你的Excel數據文件在另外一台電腦上並不存在。

熱點內容
ubuntupython3安裝 發布:2025-02-14 00:14:45 瀏覽:661
和平精英怎麼更新比較快安卓 發布:2025-02-14 00:14:35 瀏覽:974
怎麼改密碼鎖 發布:2025-02-13 23:47:39 瀏覽:852
androidbitmap獲取大小 發布:2025-02-13 23:47:38 瀏覽:559
怎麼把升級鴻蒙系統變回安卓 發布:2025-02-13 23:36:07 瀏覽:595
偶校驗c語言 發布:2025-02-13 23:22:52 瀏覽:937
芒果如何提取離線緩存視頻 發布:2025-02-13 23:16:12 瀏覽:793
王者榮耀微信區安卓哪裡分低 發布:2025-02-13 23:14:10 瀏覽:658
安裝linuxvmwaretools 發布:2025-02-13 22:56:02 瀏覽:8
浪潮伺服器如何引導系統安裝光碟 發布:2025-02-13 22:56:02 瀏覽:112