用.asp Excel、word内容导入数据库
Excel、word中的内容导入数据库最好给个完整的.asp代码网上有很多这样的问题但是我郁闷都没调试成功过求达人...
Excel、word中的内容导入数据库
最好给个完整的.asp代码
网上有很多这样的问题 但是我郁闷都没调试成功过
求达人 展开
最好给个完整的.asp代码
网上有很多这样的问题 但是我郁闷都没调试成功过
求达人 展开
展开全部
word只能导入内容.excel才可以导入数据
这是段ASP代码,可以将excel数据导入
<%
dim conn '定义一个连接变量
dim conn2 '定义第二个连接变量
'On Error Resume Next
Server.ScriptTimeOut = 999999 '超时时间
set conn=CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source="&Server.MapPath("*.mdb") '要导入的Access数据库名称,这里是etwldbs.mdb
set conn2=CreateObject("ADODB.Connection")
conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source="&Server.MapPath("*.xls") '要导入的EXCEL表名称TEST.xls
'-----------------------------------------------------------------------------------------
'这里要说明的是,数据库和Excel表和该文件必须在网站同一目录里
'-----------------------------------------------------------------------------------------
sql = "Select * FROM [Sheet1$]" '要导入的Excel数据里面的表的名称,后面一定要加$
set rs = conn2.execute(sql)
do while not rs.eof
i=1
sql = "insert into haven([haven]) values('"& fixsql(rs(0)) &"')"
'其中的price是要导入的目标表,其后的company,fromhaven是price表中的字段名,但是要和后面的fixsql(rs(数字))要对应!
'response.write sql
'response.end
conn.execute(sql)
i=i+1
rs.movenext
Response.Write "<font face=宋体 size=2>正在插入 "&sql&"</font><Br>"
'在这里输出执行的语句,可以去掉的!
Response.Flush
loop
conn.close
set conn = nothing
conn2.close
set conn2 = Nothing
If Err = 0 Then
Response.Write "<script>alert('导入成功'),window.open('*.asp','_self')</script>"
Else
Response.Write "导入失败!"
End If
function fixsql(str)
dim newstr
newstr = str
if isnull(newstr) then
newstr = ""
else
newstr = replace(newstr,"’","’’")
end if
fixsql = newstr
end Function
%>
这是段ASP代码,可以将excel数据导入
<%
dim conn '定义一个连接变量
dim conn2 '定义第二个连接变量
'On Error Resume Next
Server.ScriptTimeOut = 999999 '超时时间
set conn=CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source="&Server.MapPath("*.mdb") '要导入的Access数据库名称,这里是etwldbs.mdb
set conn2=CreateObject("ADODB.Connection")
conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source="&Server.MapPath("*.xls") '要导入的EXCEL表名称TEST.xls
'-----------------------------------------------------------------------------------------
'这里要说明的是,数据库和Excel表和该文件必须在网站同一目录里
'-----------------------------------------------------------------------------------------
sql = "Select * FROM [Sheet1$]" '要导入的Excel数据里面的表的名称,后面一定要加$
set rs = conn2.execute(sql)
do while not rs.eof
i=1
sql = "insert into haven([haven]) values('"& fixsql(rs(0)) &"')"
'其中的price是要导入的目标表,其后的company,fromhaven是price表中的字段名,但是要和后面的fixsql(rs(数字))要对应!
'response.write sql
'response.end
conn.execute(sql)
i=i+1
rs.movenext
Response.Write "<font face=宋体 size=2>正在插入 "&sql&"</font><Br>"
'在这里输出执行的语句,可以去掉的!
Response.Flush
loop
conn.close
set conn = nothing
conn2.close
set conn2 = Nothing
If Err = 0 Then
Response.Write "<script>alert('导入成功'),window.open('*.asp','_self')</script>"
Else
Response.Write "导入失败!"
End If
function fixsql(str)
dim newstr
newstr = str
if isnull(newstr) then
newstr = ""
else
newstr = replace(newstr,"’","’’")
end if
fixsql = newstr
end Function
%>
展开全部
导出EXCEL表的方法
public class ExpExcelRule
{
public static void ExportGridViewToExcel(HttpResponse response, Control gv, String excelFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF-8";
response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", excelFileName));
response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-7");
response.ContentType = "application/ms-excel";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter);
response.Write(oStringWriter.ToString());
response.End();
}
}
protected void btnExecl_Click(object sender, EventArgs e)
{
ExpExcelRule.ExportGridViewToExcel(Response,GridView1,Server.UrlEncode("×××"));
}
(不通过服务器直接传回信息)
当无法导出EXCEL时.
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
public class ExpExcelRule
{
public static void ExportGridViewToExcel(HttpResponse response, Control gv, String excelFileName)
{
response.Clear();
response.Buffer = true;
response.Charset = "UTF-8";
response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", excelFileName));
response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-7");
response.ContentType = "application/ms-excel";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter);
response.Write(oStringWriter.ToString());
response.End();
}
}
protected void btnExecl_Click(object sender, EventArgs e)
{
ExpExcelRule.ExportGridViewToExcel(Response,GridView1,Server.UrlEncode("×××"));
}
(不通过服务器直接传回信息)
当无法导出EXCEL时.
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询