如何在将XML转换为XLS时禁用换行文本(使用XSL)

时间:2018-01-13 18:06:36

标签: c# xml xslt xls word-wrap

我正在使用XSL将XML转换为XLS。 当我打开XLS文件时,我看到" Wrap Text"是的。 enter image description here

如何使用" Wrap Text"创建XLS;关?我希望XLS看起来像这样: enter image description here

这是Test.xsl:



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" encoding="utf-8"/>
  <xsl:template match="Tickets">
      <HTML>
        <head>
          <meta http-equiv="Content-Type" CONTENT="application/vnd.ms-excel" />
        </head>
        <body>
          <table style='text-align:left;border:1; cellpadding:0px; cellspacing:0px'>
            <xsl:call-template name="ticket" />
          </table>
        </body>
      </HTML>
    </xsl:template>
    <xsl:template name="ticket">
        <tr style='color:black;font-weight:bold;font:Calibri;font-size:16'>
          <td >This is a very long long text that currently wrap on its cell</td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
&#13;
&#13;
&#13; 这是XML:

&#13;
&#13;
<?xml version="1.0"?>
<?xml-stylesheet href="Test.xsl" type="text/xsl" ?>
<Tickets>
  <Ticket>
    <id>111</id>
  </Ticket>
</Tickets>
&#13;
&#13;
&#13; 这是使用XSL从XML转换为XLS的代码:

            using (StreamWriter sw = new StreamWriter("C:\\MyTest.xls"))
        {
                String xslt = "C:\\Test.xsl";
                var xDocument = XDocument.Load(@"C:\test.xml");
                string sb = xDocument.ToString();
                StreamReader sr = new StreamReader(xslt);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.DtdProcessing = DtdProcessing.Parse;
                settings.MaxCharactersFromEntities = 1024;
                XmlReader reader = XmlReader.Create(sr, settings);
                String transformstring = XMLTransform(sb, reader);
                sw.Write(transformstring);
        }

        protected string XMLTransform(String xml, XmlReader stylesheet)
    {
            StringBuilder sb = new StringBuilder();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreProcessingInstructions = true;
            settings.IgnoreComments = true;
            settings.ValidationType = ValidationType.None;
            TextReader tr = new StringReader(xml) as TextReader;
            XmlReader reader3 = XmlReader.Create(tr, settings);
            XPathNavigator navigator = new XPathDocument(reader3).CreateNavigator();
            XslCompiledTransform transform = new XslCompiledTransform(true);
            transform.Load(stylesheet, new XsltSettings(false, true), new XmlUrlResolver());
            using (MemoryStream stream = new MemoryStream())
            {
                transform.Transform((IXPathNavigable)navigator, null, (Stream)stream);
                stream.Seek(0L, SeekOrigin.Begin);
                using (StreamReader reader4 = new StreamReader(stream))
                {
                    while (!reader4.EndOfStream)
                    {
                        sb.AppendLine(reader4.ReadLine());
                    }
                }
            }
            return sb.ToString();
    }

谢谢。

0 个答案:

没有答案