您的位置:首页 > 其它

XML结合XSLT生成Excel(导入到多个Worksheet)

2008-08-05 22:12 435 查看
1、.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click" Text="Export" /></div>

</form>

</body>

</html>

2、code-behide

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

using System.Text;

public partial class ExportAsExcel : System.Web.UI.Page

3、ExcelBuilder

using System;

using System.Collections.Specialized;

using System.Text;

using System.Text.RegularExpressions;

using System.Xml;

using System.Xml.Xsl;

using System.Xml.XPath;

using System.Data;

using System.IO;

public class ExcelBuilder

4、XSLT(demo.xsl)

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<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">

<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">

<ActiveSheet>1</ActiveSheet>

<ProtectStructure>False</ProtectStructure>

<ProtectWindows>False</ProtectWindows>

</ExcelWorkbook>

<Styles>

<Style ss:ID="Default" ss:Name="Normal">

<Alignment ss:Vertical="Center"/>

<Borders/>

<Font ss:FontName="Arial" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>

<Interior/>

<NumberFormat/>

<Protection/>

</Style>

<Style ss:ID="s67">

<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"/>

<Font ss:FontName="Arial" x:CharSet="134" ss:Size="11" ss:Color="#000000"

ss:Bold="1"/>

<NumberFormat ss:Format="@"/>

</Style>

<Style ss:ID="s68">

<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"/>

<NumberFormat ss:Format="@"/>

</Style>

</Styles>

<Worksheet ss:Name="employee">

<Table ss:ExpandedColumnCount="2" x:FullColumns="1"

x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">

<Column ss:StyleID="s68" ss:Width="85.5"/>

<Column ss:StyleID="s68" ss:Width="99"/>

<Row>

<Cell ss:StyleID="s67">

<Data ss:Type="String">employeeId</Data>

</Cell>

<Cell ss:StyleID="s67">

<Data ss:Type="String">title</Data>

</Cell>

</Row>

<xsl:for-each select="NewDataSet/Table">

<Row>

<Cell>

<Data ss:Type="String">

<xsl:value-of select="employeeId" />

</Data>

</Cell>

<Cell>

<Data ss:Type="String">

<xsl:value-of select="title" />

</Data>

</Cell>

</Row>

</xsl:for-each>

</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="sale">

<Table ss:ExpandedColumnCount="2" x:FullColumns="1"

x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="13.5">

<Column ss:StyleID="s68" ss:Width="85.5"/>

<Column ss:StyleID="s68" ss:Width="99"/>

<Row>

<Cell ss:StyleID="s67">

<Data ss:Type="String">productId</Data>

</Cell>

<Cell ss:StyleID="s67">

<Data ss:Type="String">productName</Data>

</Cell>

</Row>

<xsl:for-each select="NewDataSet/Table1">

<Row>

<Cell>

<Data ss:Type="String">

<xsl:value-of select="productId" />

</Data>

</Cell>

<Cell>

<Data ss:Type="String">

<xsl:value-of select="productName" />

</Data>

</Cell>

</Row>

</xsl:for-each>

</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>

</Workbook>

</xsl:template>

</xsl:stylesheet>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: