csharp 通过使用XSLT来导出Excel的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 通过使用XSLT来导出Excel的数据相关的知识,希望对你有一定的参考价值。

public void Export()
{
    int yearMonth;
    if (int.TryParse(ddlPeriod.SelectedValue, out yearMonth))
    {
        AnalyzeReports = _projectFeeService.GetProjectFeeAnalyze(txtMixCondition.Text, int.Parse(ddlPeriod.SelectedValue));
    }
    else
    {
        AnalyzeReports = new List<ProjectFeeAnalyzeReport>(0);
    }
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(Server.MapPath("~/ExportTemplate/ProjectFeeAnalyze.xslt"), new XsltSettings(true, true), new XmlUrlResolver());

    XmlSerializer serializer = new XmlSerializer(AnalyzeReports.GetType());
    using (MemoryStream stream = new MemoryStream())
    {
        XmlWriterSettings xmlWritersettings = new XmlWriterSettings()
        {
            Indent = true,
            Encoding = Encoding.UTF8,
            IndentChars = " ",
            OmitXmlDeclaration = true,
        };
        using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWritersettings))
        {
            serializer.Serialize(xmlWriter, AnalyzeReports);
        }

        stream.Position = 0;
        using (XmlReader reader = XmlReader.Create(stream))
        {
            reader.MoveToContent();
            using (MemoryStream results = new MemoryStream())
            {
                xslt.Transform(reader, null, results);

                Response.Clear();
                Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode("项目预算执行情况分析.xls")));
                Response.ContentType = "application/ms-excel";
                Response.Charset = "utf-8";
                Response.ContentEncoding = Encoding.UTF8;
                Response.BinaryWrite(results.GetBuffer());
                Response.End();
            }
        }
    }
}
//更多关于将实体实例化为xml的文章,参看:http://www.cnblogs.com/weblogical/archive/2013/05/22/3093295.html

以上是关于csharp 通过使用XSLT来导出Excel的数据的主要内容,如果未能解决你的问题,请参考以下文章

使用 Xslt 将 Xml 中类似节点的数据导出到 excel

XSLT 将 FMPXMLRESULT xml 文档导出到 Excel

php控制导出excel的单元格格式

csharp .Net Excel导出

可以使用 XSLT 样式表填充现有 Excel 工作表吗?

csharp Excel导出EP Plus C类#