asp实现Execl导入导出ACCESS操作SQL
来源:
互联网
日期:2012-7-31
Tc711Com提示:asp实现Execl导入导出ACCESS操作SQL | 一:ASP连结EXECEL【SQL 操作EXCEL】
<% dim conn set conn=server.createobject("adodb.connection") conn.open "driver={Microsoft Excel Driver (*.xls)};DBQ="&server.mappath("execl.xls")
dim rs set rs=conn.execute("select * from [Sheet1$]") %>
数据表后面要加$号才行
EXECL里第一行就是字段名,用法用ACCESS差不多
二 :ASP: EXCEL导入到ACCESS
原始EXECL表中的信息导到WEB里~~
下面连接到student.xls表sheet1$ '表后面要加个$的,我以前试用,一定要加这个符号 Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Driver={Microsoft Excel Driver (*.xls)};ReadOnly=0;DBQ=" & Server.MapPath("student.xls")
SQL1="select * from [sheet1$]" Set rs = Server.CreateObject("ADODB.Recordset") rs.Open SQL1, conn, 3, 3
'下面连接到data.mdb表table curDir = Server.MapPath("../data.mdb") Set conn1 = Server.CreateObject("ADODB.Connection") conn1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & curDir Set rs1 = Server.CreateObject("ADODB.Recordset") Set rs1.ActiveConnection = conn1 rs1.Source = "select * from table" rs1.CursorType = 3 ' adOpenKeyset rs1.LockType = 3 'adLockOptimistic rs1.Open Do While Not rs.Eof rs1.AddNew rs1(0)=j for i=0 to rs.Fields.Count-1 rs1(i)=Trim(rs(i)) Next rs1.Update rs.MoveNext j=j+1 Loop
rs.Close rs1.Close conn.Close conn1.Close Set rs=nothing Set conn=nothing
三 :ASP: Access导出到Excel 如果导出数据结构不复杂,直接用TABLE,在页面里加入<%response.ContentType ="application/vnd.ms-excel"%>即可!
server.scripttimeout=100000 '处理时间较长,设置值应大一点 On Error Resume Next set objExcelApp = createObject("Excel.Application") objExcelApp.DisplayAlerts = false objExcelApp.Application.Visible = false objExcelApp.WorkBooks.add set objExcelBook = objExcelApp.ActiveWorkBook set objExcelSheets = objExcelBook.Worksheets set objSpreadsheet = objExcelBook.Sheets(1) Dim Conn Dim Connstr Dim DB DB="weste.mdb" '这里选择数据库 Set conn = Server.createObject("ADODB.Connection") Connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(DB) Conn.Open Connstr Dim objRS Set objRS = Server.createObject("ADODB.Recordset") objRS.Open "select * FROM FriendLink",conn,1,3 '这里用sql语句查询需要导出的内容 If objRS.EOF then response.write("Error") respose.end End if
Dim objField, iCol, iRow iCol = 1 '取得列号 iRow = 1 '取得行号 objSpreadsheet.Cells(iRow, iCol).Value = "用ASP将Access中的数据导入到Excel文件——西部e网" '单元格插入数据 objSpreadsheet.Columns(iCol).ShrinkToFit=true '设定是否自动适应表格单元大小(单元格宽不变) '设置Excel表里的字体 objSpreadsheet.Cells(iRow, iCol).Font.Bold = True '单元格字体加粗 objSpreadsheet.Cells(iRow, iCol).Font.Italic = False '单元格字体倾斜 objSpreadsheet.Cells(iRow, iCol).Font.Size = 20 '设置单元格字号 objSpreadsheet.Cells(iRow, iCol).ParagraphFormat.Alignment=1 '设置单元格对齐格式:居中 objspreadsheet.Cells(iRow,iCol).font.name="宋体" '设置单元格字体 objspreadsheet.Cells(iRow,iCol).font.ColorIndex=2 '设置单元格文字的颜色,颜色可以查询,2为白色 objSpreadsheet.Range("A1:F1").merge '合并单元格(单元区域) objSpreadsheet.Range("A1:F1").Interior.ColorIndex = 1 '设计单元络背景色 'objSpreadsheet.Range("A2:F2").WrapText=true '设置字符回卷(自动换行) iRow=iRow+1 For Each objField in objRS.Fields 'objSpreadsheet.Columns(iCol).ShrinkToFit=true objSpreadsheet.Cells(iRow, iCol).Value = objField.Name '设置Excel表里的字体 objSpreadsheet.Cells(iRow, iCol).Font.Bold = True objSpreadsheet.Cells(iRow, iCol).Font.Italic = False objSpreadsheet.Cells(iRow, iCol).Font.Size = 20 objSpreadsheet.Cells(iRow, iCol).Halignment = 2 '居中 iCol = iCol + 1 Next 'objField 'Display all of the data Do While Not objRS.EOF iRow = iRow + 1 iCol = 1 For Each objField in objRS.Fields If IsNull(objField.Value) then objSpreadsheet.Cells(iRow, iCol).Value = "" Else objSpreadsheet.Columns(iCol).ShrinkToFit=true objSpreadsheet.Cells(iRow, iCol).Value = objField.Value objSpreadsheet.Cells(iRow, iCol).Halignment = 2 objSpreadsheet.Cells(iRow, iCol).Font.Bold = False objSpreadsheet.Cells(iRow, iCol).Font.Italic = False objSpreadsheet.Cells(iRow, iCol).Font.Size = 10 'objSpreadsheet.Cells(iRow, iCol).Halignment = 2 objSpreadsheet.Cells(iRow, iCol).ParagraphFormat.Alignment=1 End If iCol = iCol + 1 Next 'objField objRS.MoveNext Loop
Dim SaveName SaveName="temp1" Dim objExcel Dim ExcelPath ExcelPath = "" & SaveName & ".xls" objExcelBook.SaveAs server.mappath(ExcelPath) response.write("<a href='" & server.URLEncode(ExcelPath) & "'>下载</a>") objExcelApp.Quit set objExcelApp = Nothing
更多的asp实现Execl导入导出ACCESS操作SQL请到论坛查看: http://BBS.TC711.COM
【 双击滚屏 】 【 评论 】 【 收藏 】 【 打印 】 【 关闭 】
来源:
互联网
日期:2012-7-31
|
|
|