页面显示excel内容并且打印
问题1:在页面有一个按钮打开D盘文件夹A(d:\a),显示文件夹里所有后续名为.xls的EXCEL,并且选择EXCEL后在页面显示该EXCEL的内容。
问题2:设置一个按钮将所显示的EXCEL内容打印出来。
[解决办法]
好吧,我承认我是来骗分的,不过你也太懒了,给过你代码了,就不知道自己扩充一下。
如果不想打印按纽,自己用个div把它们装起来并隐藏,或者把表格用新窗口打开并打印。
<HTML> <BODY>
<STYLE type="text/css">
table {
border-collapse:collapse;
border:solid #999;
border-width:1px 0 0 1px;
}
table caption {font-size:14px;font-weight:bolder;}
table th,table td {border:solid #999;border-width:0 1px 1px 0;padding:2px;}
tfoot td {text-align:center;}
</STYLE>
<SCRIPT language="VBScript">
'**************************************************************************
'获得xls文件
'**************************************************************************
Sub getFile
On Error Resume Next
Set oDialog = CreateObject("UserAccounts.CommonDialog")
oDialog.Filter = "xls File|*.xls|All Files|*.*"
oDialog.InitialDir = "E:\script\vba"
oDialog.ShowOpen
if Err Then
Msgbox "错误代码:" & Err.Number & VbCrLf & Err.Description
Err.Clear
else
if Len(Trim(oDialog.FileName)) then '如果选择了文件
oTxt.value=oDialog.FileName
showXLS
end if
end if
Set oDialog = Nothing
End Sub
'**************************************************************************
'显示XLS文件
'**************************************************************************
Sub showXLS
On Error Resume Next
Set oConn = CreateObject("Adodb.Connection")
Set oAdox = CreateObject("Adox.Catalog")
sConn = "Provider = Microsoft.Jet.Oledb.4.0;" & _
"Data Source = " & oTxt.value & ";" & _
"Extended Properties = ""Excel 8.0; HDR=No"";"
sSQL = "Select * From "
oConn.Open sConn
if Err Then
Msgbox "错误代码:" & Err.Number & VbCrLf & Err.Description
Err.Clear
else
oAdox.ActiveConnection = oConn
sSQL = sSQL & "[" & oAdox.Tables(0).Name & "]" '为了简便,只处理第一个工作表
Set oRecordSet = oConn.Execute(sSQL)
if Err Then
Msgbox "错误代码:" & Err.Number & VbCrLf & Err.Description
Err.Clear
else
sHTML = " <table>"
'sHTML = sHTML & " <tr>"
'for each x in oRecordSet.Fields
' sHTML = sHTML & " <td>" & x.Name & " </td>"
'next
'sHTML = sHTML & " </tr>"
oRecordSet.MoveFirst
Do
sHTML = sHTML & " <tr>"
for each x in oRecordSet.Fields
sHTML = sHTML & " <td>" & oRecordSet.Fields.Item(x.Name) & " </td>"
next
sHTML = sHTML & " </tr>"
oRecordset.MoveNext
Loop Until oRecordSet.EOF
sHTML = sHTML & " </table>"
oDiv.innerHTML = sHTML
end if
end if
Set oRecordSet = Nothing
Set oAdox = Nothing
Set oConn = Nothing
End Sub
'**************************************************************************
'打印
'**************************************************************************
Sub Prn
document.execCommand "Print"
End Sub
</SCRIPT>
<INPUT
id="oTxt"
type="text"
CONTENTEDITABLE="false"
/>
<BUTTON onClick="getFile">浏览 </BUTTON>
<BUTTON onClick="Prn">打印 </BUTTON>
<DIV id="oDIV"> </DIV>
</BODY> </HTML>