1.操作页面:PurchaseDetail.apsx

 

代码
1 /// <summary>
2 /// 导出excel
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6   protected void btnExport_Click(object sender, EventArgs e)
7 {
8 try
9 {
10 string json = GridData.Value.ToString();
11
12 CommonUtils.GetDataToExcel(json, "../template/xsl/Excel.xsl", "采购明细列表");
13 }
14 catch (Exception ex)
15 {
16 PageExceptionHander.SetErrorMessage(this.lblMsg, Constants.COMMONMSG_KEY);
17 }
18 }

 

 

 

2.工具类:CommonUtils

 

代码
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="json"></param>
public static void GetDataToExcel(string json, string path, string fileName)
{
System.Text.RegularExpressions.Regex reg
= new System.Text.RegularExpressions.Regex(@"(?i)</?a\b[^>]*>");
string result = reg.Replace(json, "");
//没有数据时弹出提示信息
if (result == "[]")
{
AlertMessage(HttpContext.Current.CurrentHandler
as Page, "Common_0037");
return;
}

Coolite.Ext.Web.StoreSubmitDataEventArgs eSubmit
= new Coolite.Ext.Web.StoreSubmitDataEventArgs(result, null);
XmlNode xml
= eSubmit.Xml;
//xml.InnerXml =
Page page = HttpContext.Current.CurrentHandler as Page;
page.Response.Clear();
page.Response.ContentType
= "application/vnd.ms-excel";
page.Response.AddHeader(
"Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
XslCompiledTransform xtExcel
= new XslCompiledTransform();
xtExcel.Load(page.Server.MapPath(path));
xtExcel.Transform(xml,
null, page.Response.OutputStream);
page.Response.End();
}

 

 

 

3.XSL文件:Excel.xsl

 

代码
<xsl:stylesheet version="1.0"
xmlns
="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl
="urn:schemas-microsoft-com:xslt"
xmlns:user
="urn:my-scripts"
xmlns:o
="urn:schemas-microsoft-com:office:office"
xmlns:x
="urn:schemas-microsoft-com:office:excel"
xmlns:ss
="urn:schemas-microsoft-com:office:spreadsheet" >

<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">
<xsl:apply-templates/>
</Workbook>
</xsl:template>


<xsl:template match="/*">
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select="local-name(/*/*)" />
</xsl:attribute>
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="*[position() = 1]/*">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()" />
</Data>
</Cell>
</xsl:for-each>
</Row>
<xsl:apply-templates/>
</Table>
</Worksheet>
</xsl:template>


<xsl:template match="/*/*">
<Row>
<xsl:apply-templates/>
</Row>
</xsl:template>


<xsl:template match="/*/*/*">
<Cell>
<xsl:choose>
<xsl:when test="name()='单价'">
<Data ss:Type="String">
<xsl:value-of select="." />
</Data>
</xsl:when>
<xsl:otherwise>
<Data ss:Type="String">
<xsl:value-of select="." />
</Data>
</xsl:otherwise>
</xsl:choose>
</Cell>
</xsl:template>


</xsl:stylesheet>

 

 

 

posted on 2010-08-19 21:46  bihuchen  阅读(775)  评论(0编辑  收藏  举报