ASP怎样把excel上传到远程服务器后再导入到SQL Server

ASP怎样把excel上传到远程服务器后再导入到SQL Server,第1张

哈哈

刚好我也在做这个案例

<%'sql 连接驱动

dim conn,connstr,time1,time2,mdb

time1=timer

dim aa

aa=requestform("EXCEL") '获取传递过来的值

Dim StrConnect,cc

Dim objConn

Dim rs

Dim Sql

cc=0

'Excel连接驱动

aaa="provider=MicrosoftJetOLEDB40; Data Source="&aa&";Extended Properties=Excel 80"

set StrConnect=CreateObject("ADODBConnection")

StrConnectOpen aaa

Set objConn=CreateObject("ADODBConnection")

objConnOpen StrConnect

'注意 表名一定要以下边这种格试"[表名$]"书写

Set rs = ServerCreateObject("ADODBRecordset")

Sql="select from [sheet1$]"

rsOpen Sql,StrConnect,2,2

IF rsEof And RsBof Then

responseWrite("<script>alert('没有资料可以导入!');historygo(-1)</script>")

else

Do While Not rsEOF

if rs(0)<>"" then

'插入SQL2000里

set rssql=serverCreateObject("adodbrecordset")

rssqlOpen "select from a_sell where PartNo='"&rs(0)&"'",conn,1,3

if not rssqlrecordcount=0 then

responsewrite ""

else

rssqlAddNew

rssql(1)=rs(0)

rssql(2)=rs(1)

rssql(3)=rs(2)

rssql(4)=rs(3)

rssql(5)=rs(4)

rssql(6)=rs(5)

rssql(7)=rs(6)

rssql(8)=rs(7)

rssqlUpdate

end if

end if

RsMoveNext

Loop

end if

responseWrite"<script language=javascript>alert('资料导入成功!\n重复数据未导入');historygo(-1)</script>"

%>

1、ws=workbook做什么的,是否有必要加

2、在ws上添加sheet

HSSFSHeep sheet = wscreateSheet();

3、HSSFRow row = sheetcreateRow(0)

你可以先添加2,试试

拉一个FileUpload1控件作上传,然后在服务段读取excel文件,注意excel存放目录要开发权限给network serivice

protected void btnUpload_Click(object sender, EventArgs e)

{

bool flag = false;

StringBuilder sb = new StringBuilder();

if (thisFileUpload1FileName == "")

{

sbAppend("请选择excel文件!!" + "\r\n");

}

else

{

string filefullpath = thisFileUpload1PostedFileFileName;

string filename = filefullpathSubstring(filefullpathLastIndexOf("\\") + 1);

string type = filenameSubstring(filenameLastIndexOf("") + 1)ToLower();

if (type == "xls")

{

string sJudgeIfExit = ServerMapPath("\\Upload\\") + filename;

if (SystemIODirectoryExists(sJudgeIfExit))

{

sbAppend("文件名重复!!" + "\r\n");

}

else

{

thisFileUpload1PostedFileSaveAs(sJudgeIfExit);

Session["OTCTable"] = CreateDataSource(sJudgeIfExit)Tables[0];

sbAppend("加载ExcelOK!" + "\r\n");

flag = true;

}

}

else

{

sbAppend("请选择excel文件操作!" + "\r\n");

}

}

}

private DataSet CreateDataSource(string filepath)

{

DataSet myds = new DataSet();

string strCon = "Provider=MicrosoftJetOLEDB40;Data Source=" + filepath + "; Extended Properties=Excel 80;";

OleDbConnection olecon = new OleDbConnection(strCon);

OleDbDataAdapter myda = new OleDbDataAdapter("SELECT FROM [Sheet1$]", strCon);

mydaFill(myds, "excel");

return myds;

}

这需要你在程序里面设置,把他设置成只要有人运行程序时就自动去找表格才行,如果说单一的共享表格,那太简单了。只要你把表格放在你的共享文件夹进而面,或者你把表格所在的文件夹右键属性-共享就可以了。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » ASP怎样把excel上传到远程服务器后再导入到SQL Server

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情