简体   繁体   中英

creating multi sheet excel file using classic asp

I am working on a project and I need to create a multi sheet excel file using classic asp.

Here is my code. It makes a multi sheet excel but I cant write anything on sheet 2 or 3:

<%
  response.charset = "UTF-8"
  Response.Buffer=true

  Response.AddHeader "Content-Disposition", "attachment;filename=aaa.xls"  
  Response.ContentType = "application/vnd.ms-excel" 
%>
<HTML xmlns:x="urn:schemas-microsoft-com:office:excel">
<HEAD>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>sheet1</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
<x:ExcelWorksheet>
<x:Name>sheet2</x:Name>
<x:WorksheetOptions>
<x:Print>
<x:ValidPrinterInfo>
</x:Print>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
</HEAD>
<BODY>
<TABLE>
<%
dim  oConn
Set oConn = Server.CreateObject("ADODB.Connection")
Dim rs, ds
Set rs = Server.CreateObject("ADODB.Recordset")
Response.Write "<tr>"
Response.Write "<td style='border-left:none;border:.5pt solid windowtext;' bgcolor='#CCCCCC' bordercolor='#000000'><b>"
Response.Write "aa"
Response.Write "</b></td>"
Response.Write "</tr>"
Response.Write "<tr>"
Response.Write "<td>"
Response.Write "bb"
Response.Write "</td>"
Response.Write "</tr>"
%>
</TABLE>
</BODY>
</HTML>

I don't think it's possible to create actual.xls /.xlsx files in Classic ASP without a third party component (such as EasyXLS.ExcelDocument).

But what you can do is generate your Excel file using XML markup within Classic ASP, export it with a.xls extension and Excel will convert the file when you open it (after a warning message).

For example:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%

    Response.Charset = "UTF-8"
    Response.AddHeader "Content-Disposition", "attachment;filename=aaa.xls"  
    Response.ContentType = "application/vnd.ms-excel" 

%><?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Author>Author Name</Author>
  <LastAuthor>Author Name</LastAuthor>
  <Created>2020-05-20T13:19:37Z</Created>
  <LastSaved>2020-05-20T13:21:04Z</LastSaved>
  <Company>Company Name</Company>
  <Version>14.00</Version>
 </DocumentProperties>
 <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
  <AllowPNG/>
 </OfficeDocumentSettings>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>4695</WindowHeight>
  <WindowWidth>14355</WindowWidth>
  <WindowTopX>360</WindowTopX>
  <WindowTopY>105</WindowTopY>
  <ActiveSheet>2</ActiveSheet>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Row>
    <Cell><Data ss:Type="String">1A - Sheet 1</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet2">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Row>
    <Cell><Data ss:Type="String">1A - Sheet 2</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
 <Worksheet ss:Name="Sheet3">
  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Row>
    <Cell><Data ss:Type="String">1A - Sheet 3</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>6</ActiveRow>
     <ActiveCol>3</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>

The above example has 3 populated sheets. Best thing to do would be to create a dummy template of the type of Excel layout you're looking to generate, populate the sheets with dummy data and export from Excel using the "XML Spreadsheet" format (which is all I did for the above example). Then use Classic ASP to replace the dummy data with actual data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM