用ASP创建PDF文件

转载 2006年06月24日 13:40:00

<%
Option Explicit

 

Sub CheckXlDriver()
On Error Resume Next

Dim vConnString
Dim oConn, oErr

' try to connect to file NUL:
vConnString = 'DRIVER={Microsoft Excel Driver (*.xls)};DBQ=NUL:'

Set oConn = CreateObject('ADODB.Connection')
oConn.Open vConnString

For Each oErr in oConn.Errors
' when the Excel driver reports 'Failure creating file',
' then it must be installed and working ;-))
If oErr.NativeError = -5036 Then
Exit Sub
End If
Next

Response.Write 'Provider or Driver not available. (Re-)Install MDAC.

'

Response.Write hex(Err.Number) & ' ' & Err.Description & '
'
For Each oErr in oConn.Errors
Response.Write hex(oErr.Number) & ' ' & oErr.NativeError & ' ' & oErr.Description & '
'
Next
Response.End

End Sub

Function GetConnection(vConnString)
On Error Resume Next

Set GetConnection = Server.CreateObject('ADODB.Connection')
GetConnection.Open vConnString

If Err.Number <> 0 Then
Set GetConnection = Nothing
End If

End Function

Function OptionTag(vChoice,vTrue)
Dim vSelected

If vTrue Then
vSelected = 'selected'
End If

OptionTag = '<option ' & vSelected & '>' & _
Server.HtmlEncode(vChoice) & '</option>' & vbCrLf

End Function

Function IsChecked(vTrue)
If vTrue Then
IsChecked = 'checked'
End If
End Function

Function BookOptions(vXlFile)
Dim vServerFolder
Dim oFs, oFolder, oFile

Dim vSelected

vServerFolder = Server.MapPath('.')

Set oFs = Server.CreateObject('Scripting.FileSystemObject')
Set oFolder = oFs.GetFolder(vServerFolder)

For Each oFile in oFolder.Files
If oFile.Type = 'Microsoft Excel Worksheet' Then
vSelected = (oFile.Name = vXlFile)

BookOptions = BookOptions & _
OptionTag(oFile.Name, vSelected)
End If
Next

Set oFolder = Nothing
Set oFs = Nothing

End Function

Function NamedRangeOptions(oConn, vXlRange, vTableType)
Dim oSchemaRs
Dim vSelected

NamedRangeOptions = OptionTag(Empty, Empty)

If TypeName(oConn) = 'Connection' Then
Set oSchemaRs = oConn.OpenSchema(adSchemaTables)

Do While Not oSchemaRs.EOF
If oSchemaRs('TABLE_TYPE') = vTableType Then
vSelected = (oSchemaRs('TABLE_NAME') = vXlRange)
NamedRangeOptions = NamedRangeOptions & _
OptionTag(oSchemaRs('TABLE_NAME'), vSelected)

End If

oSchemaRs.MoveNext
Loop

End If
End Function

Function DataTable(oConn, vXlRange, vXlHasHeadings)
On Error Resume Next

' from OleDbVbc.inc
Const DB_E_ERRORSINCOMMAND = &H80040E14

Dim oRs, oField
Dim vThTag, vThEndTag

If vXlHasHeadings Then
vThTag = '<th>'
vThEndTag = '</th>'
Else
vThTag = '<td>'
vThEndTag = '</td>'
End If

DataTable = '<table border=1>'

If TypeName(oConn) = 'Connection' Then
Set oRs = oConn.Execute('[' & vXlRange & ']')

If oConn.Errors.Count > 0 Then
For Each oConnErr in oConn.Errors
If oConnErr.Number = DB_E_ERRORSINCOMMAND Then
DataTable = DataTable & _
'<tr><td>No such range :</td><th>' & vXlRange & '</th></tr>'
Else
DataTable = DataTable & _
'<tr><td>' & oConnErr.Description & '</td></tr>'
End If
Next
Else
DataTable = DataTable & '<tr>'

For Each oField in oRs.Fields
DataTable = DataTable & vThTag & oField.Name & vThEndTag
Next

DataTable = DataTable & '</tr>'

Do While Not oRs.Eof
DataTable = DataTable & '<tr>'

For Each oField in oRs.Fields
DataTable = DataTable & '<td>' & oField.Value & '</td>'
Next

DataTable = DataTable & '</tr>'
oRs.MoveNext
Loop

End If

Set oRs = Nothing
Else

DataTable = DataTable & '<tr><td>File locked by another application or otherwise not accessible. Cannot continue.</td></tr>'
End If

DataTable = DataTable & '</table>'
End Function


' --main--
%>
<html>
<head>
<title>Read Excel</title>
<SCRIPT LANGUAGE=javascript>
<!--
function XlBook_onchange(theForm) {
with (theForm) {
XlSheet.selectedIndex = 0;
XlSheet.disabled = true;
XlNamedRange.selectedIndex = 0;
XlNamedRange.disabled = true;
XlTypedRange.value = 'A:IV';
}
}

function XlSheet_onchange(theForm) {
with (theForm) {
XlNamedRange.selectedIndex = 0;
XlTypedRange.value = XlSheet.options[XlSheet.selectedIndex].text;
}
}

function XlNamedRange_onchange(theForm) {
with (theForm) {
XlSheet.selectedIndex = 0;
XlTypedRange.value = XlNamedRange.options[XlNamedRange.selectedIndex].text;
}
}

function XlTypedRange_onchange(theForm) {
with (theForm) {
XlSheet.selectedIndex = 0;
XlNamedRange.selectedIndex = 0;
}
}

//-->
</SCRIPT>
</head>
<body>
<%
Dim vXlFile, vXlFilePath
Dim vXlRange, vXlHasHeadings
Dim vDisabled

Dim vConnString
Dim oConn, oConnErr

Const adSchemaTables = 20 ' from adovbs.inc

CheckXlDriver ' make sure it is working

vXlFile = Request('XlBook')

If vXlFile <> Empty Then

vXlRange = Request('XlTypedRange')

If vXlRange = Empty Then
vXlRange = 'A:IV'
Else
vXlRange = Replace(vXlRange, '!', '$')
End If

vXlHasHeadings = Request('XlHasHeadings')

' establish connection
vXlFilePath = Server.MapPath(vXlFile)
vConnString = 'DRIVER={Microsoft Excel Driver (*.xls)};ReadOnly=1;DBQ=' & _ vXlFilePath

Set oConn = GetConnection(vConnString)

Else
vDisabled = 'disabled'
End If

%>
<form name=MyForm method='POST' action='<%=Request.ServerVariables('SCRIPT_NAME')%>'>
 <table border='1' width='100%'>
  <tr>
   <th>Workbook :</th>
   <td>
   <select name='XlBook' LANGUAGE=javascript onchange='return XlBook_onchange(MyForm)'>
    <%= BookOptions(vXlFile) %>
    </select></td>
   <td align='center'>Worksheet :</td>
   <td><select <%=vDisabled%> name='XlSheet' LANGUAGE=javascript onchange='return XlSheet_onchange(MyForm)'>
    <%= NamedRangeOptions(oConn, vXlRange, 'SYSTEM TABLE') %>
    </select></td>
  </tr>
<tr>
   <th>Range :</th>
   <td><input type='text' name='XlTypedRange' LANGUAGE=javascript onchange='return XlTypedRange_onchange(MyForm)'
    value ='<%= vXlRange %>'></td>
   <td align='center'>Named Range :</td>
   <td><select <%=vDisabled%> name='XlNamedRange' LANGUAGE=javascript onchange='return XlNamedRange_onchange(MyForm)'>
    <%= NamedRangeOptions(oConn, vXlRange, 'TABLE') %>
    </select></td>
  </tr>
  <tr>
   <th>
    <p> </th>
   <td colspan='3'>
    <input type='checkbox' name='XlHasHeadings'
    <%= IsChecked(vXlHasHeadings) %>
    value='True'> Show first row as column headings</td>
  </tr>
  <tr>
   <th>
    <p> </th>
   <td colspan=3>
    <a href=<%= vXlFile %>><%= vXlFile %></a>
   </td>
  </tr>
 </table>
 <input type='submit' value='Submit' name='cmdSubmit'> 
 <input type='reset' value='Reset' name='cmdReset'>
</form><hr>
<%
If vXlRange <> Empty Then
Response.Write DataTable(oConn, vXlRange, vXlHasHeadings)
End If
%>
</body>
</html>

相关文章推荐

asp.net2.0导出pdf文件完美解决方案

PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式。这种文件格式与操作系统平台无关,也就是说,PDF文件不管是在Windows,Unix还...

asp.net生成PDF文件参考

这个是例子是网上淘来的,哈哈,很有用的!      首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: using System; using System....

ASP.NET中生成PDF文件

这是看了一下的文章而进行的一个总结,因为最近也在想如何通过程序来生成PDF文件。 Generating PDF files Reponse有个这样的属性ContentType,获取或设置输出流的...

ASP.NET MVC在线预览Excel、Word、TXT、PDF文件

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We...

Asp.net C#制作PDF文件全攻略

StreamWriter pPDF=new StreamWriter(filePath); ArrayList xRefs=new ArrayList(); float yPos =...

Asp.net在线阅读文件(pdf,txt,doc,jpg等)

新建网站default.aspx,添加一个按钮,Text为“阅读文件”。在按钮的Click事件中输入如下代码: Response.Redirect("ReadFileOther.aspx?FileP...

asp.net生成PDF文件参考

TextSharp 是用来生成  PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 HTML 来生成报告,但是,使用 ...

vs2010 asp.net webForm Html表格数据模板转换pdf文件,web打印Pdf

htmlTable数据表模板 转换pdf 用到itextsharp5.4.0 自定义函数 算术运算符,关系运算符,以及逻辑运算符 预定义函数getRMB(double num)getRMBs(stri...

asp中实现pdf文件在线查看

  • 2013-11-01 12:49
  • 2.78MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)