将xslt转换为SpreedsheetDocument

时间:2015-12-07 20:10:28

标签: c# xml xslt openxml

我目前正在开展一个项目,我正在研究在RDLC中生成的旧样式报告,并将其转换为通过OpenXml构建。我一直在手工制作报告,但由于样式问题,我建议使用xslt。

到目前为止,下面是第一页的xslt。

<xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:n2="urn:hl7-org:v3"
     exclude-result-prefixes="n2 xs xsi xsl"
     >
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  <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">
      <worksheet ss:Name="Goal"
        xmlns:cdr="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing"
        xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
        xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
        xmlns:s="http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes"
        xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
        xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
        <sheetPr>
          <outlinePr/>
        </sheetPr>
        <sheetViews>
          <sheetView showGridLines="0" workbookViewId="0"/>
        </sheetViews>
        <cols>
          <col width="0.15234375" customWidth="1" max="1" min="1"/>
          <col width="6.5234375" customWidth="1" max="2" min="2"/>
          <col width="7.421875" customWidth="1" max="3" min="3"/>
          <col width="6.6328125" customWidth="1" max="4" min="4"/>
          <col width="4.8515625" customWidth="1" max="5" min="5"/>
          <col width="84.5234375" customWidth="1" max="6" min="6"/>
          <col width="11.53125" customWidth="1" max="7" min="7"/>
          <col width="0.0234375" customWidth="1" max="8" min="8"/>
          <col width="0.40234375" customWidth="1" max="9" min="9"/>
          <col width="0.0234375" customWidth="1" max="10" min="10"/>
          <col width="0.15234375" customWidth="1" max="11" min="11"/>
          <col width="28.51171875" customWidth="1" max="12" min="12"/>
        </cols>
        <sheetData>
          <row r="1" customHeight="1" ht="25.25"/>
          <row r="2" customHeight="1" ht="18">
            <c s="1" r="A2" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="GoalDS/@GoalReportName" /></t>
              </is>
            </c>
          </row>
          <row r="3" customHeight="1" ht="2"/>
          <row r="4" customHeight="1" ht="19.45">
            <c s="2" r="A4" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="GoalDS/@ReportTypeDesc" /></t>
              </is>
            </c>
          </row>
          <row r="5" customHeight="1" ht="5.05"/>
          <row r="6" customHeight="1" ht="18">
            <c s="1" r="A6" t="inlineStr">
              <is>
                <t xml:space="preserve">Fossil Energy Coal &amp; Power RD&amp;D Portfolio</t>
              </is>
            </c>
          </row>
          <row r="7" customHeight="1" ht="125.5"/>
          <row r="8" customHeight="1" ht="67.5">
            <c s="3" r="A8" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="GoalDS/@GoalDescription"/></t>
              </is>
            </c>
          </row>
          <row r="9" customHeight="1" ht="3"/>
          <row r="10" customHeight="1" ht="15.75">
            <c s="4" r="A10" t="inlineStr">
              <is>
                <t xml:space="preserve">DOE Mission - Discovering the solutions to power and secure America's future</t>
              </is>
            </c>
            <c s="5" r="B10" t="str"/>
            <c s="5" r="C10" t="str"/>
            <c s="5" r="D10" t="str"/>
            <c s="5" r="E10" t="str"/>
            <c s="5" r="F10" t="str"/>
            <c s="5" r="G10" t="str"/>
            <c s="5" r="H10" t="str"/>
            <c s="6" r="I10" t="str"/>
          </row>
          <row r="11" customHeight="1" ht="1"/>
          <row r="12" customHeight="1" ht="16.95">
            <c s="4" r="B12" t="inlineStr">
              <is>
                <t xml:space="preserve">DOE Strategic Theme 1: </t>
              </is>
            </c>
            <c s="5" r="C12" t="str"/>
            <c s="5" r="D12" t="str"/>
            <c s="5" r="E12" t="str"/>
            <c s="5" r="F12" t="str"/>
            <c s="5" r="G12" t="str"/>
            <c s="5" r="H12" t="str"/>
            <c s="5" r="I12" t="str"/>
            <c s="5" r="J12" t="str"/>
            <c s="6" r="K12" t="str"/>
          </row>
          <row r="13" customHeight="1" ht="2.05"/>
          <row r="14" customHeight="1" ht="16.2">
            <c s="4" r="C14" t="inlineStr">
              <is>
                <t xml:space="preserve">Goal 1.2 - Environmental Impacts of Energy:</t>
              </is>
            </c>
            <c s="5" r="D14" t="str"/>
            <c s="5" r="E14" t="str"/>
            <c s="5" r="F14" t="str"/>
            <c s="5" r="G14" t="str"/>
            <c s="5" r="H14" t="str"/>
            <c s="6" r="I14" t="str"/>
          </row>
          <row r="15" customHeight="1" ht="1.05"/>
          <row r="16" customHeight="1" ht="15.5">
            <c s="4" r="D16" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="GoalDS/@ReportTypeDesc"/></t>
              </is>
            </c>
            <c s="5" r="E16" t="str"/>
            <c s="5" r="F16" t="str"/>
            <c s="5" r="G16" t="str"/>
            <c s="5" r="H16" t="str"/>
            <c s="6" r="I16" t="str"/>
          </row>
          <row r="17" customHeight="1" ht="1"/>
          <xsl:for-each select="Report/TechAreaDS">
            <row customHeight="1" ht="12.8">
              <c s="7" t="inlineStr">
                <is>
                  <t xml:space="preserve"><xsl:value-of select="@TechAreaName"/></t>
                </is>
              </c>
              <c s="5" t="str"/>
              <c s="5" t="str"/>
              <c s="5" t="str"/>
              <c s="6" t="str"/>
            </row>
          </xsl:for-each>
          <row customHeight="1" ht="0.05"/>
          <row customHeight="1" ht="2"/>
          <row customHeight="0" ht="18">
            <c s="7" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="CountDS/@HeadingCountOfMeasures"/></t>
              </is>
            </c>
            <c s="8">
              <v><xsl:value-of select="CountDS/@CountOfMeasures"/></v>
            </c>
          </row>
          <row customHeight="0" ht="18">
            <c s="7" t="inlineStr">
              <is>
                <t xml:space="preserve"><xsl:value-of select="CountDS/@HeadingCountOfMilestones"/></t>
              </is>
            </c>
            <c s="8">
              <v><xsl:value-of select="CountDS/@CountOfMilestones"/></v>
            </c>
          </row>
        </sheetData>
        <mergeCells>
          <mergeCell ref="A2:I2"/>
          <mergeCell ref="A4:I4"/>
          <mergeCell ref="A6:I6"/>
          <mergeCell ref="A8:I8"/>
          <mergeCell ref="A10:I10"/>
          <mergeCell ref="B12:K12"/>
          <mergeCell ref="C14:I14"/>
          <mergeCell ref="D16:I16"/>
          <mergeCell ref="E18:I18"/>
          <!--<mergeCell ref="E19:I19"/>
          <mergeCell ref="E20:I20"/>-->
        </mergeCells>
        <pageMargins footer="0.5" header="0.5" bottom="0.5" top="0.5" right="0.75" left="0.75"/>
        <pageSetup verticalDpi="300" horizontalDpi="300" orientation="landscape"/>
        <headerFooter alignWithMargins="0"/>
      </worksheet>
    </workbook>
  </xsl:template>
</xsl:stylesheet>

我的问题是,如何将我的xslt转换为C#中的OpenXml可用的东西?这是我到目前为止所做的,但我不确定如何继续进行,因为我现在所有的都是错误,无法找到sheet1.xml。

public byte[] GetGPRA_ARRA_FA_Qtr_Report(int fiscalYear, string reportType, int officeId, out string mimeType)
        {
            ...
            #region Create Spreadsheet Document
            using (SpreadsheetDocument package = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
            {
                uint sheetId = 1;

                //Create the Workbook
                var workbookPart = package.AddWorkbookPart();
                var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                var relId = workbookPart.GetIdOfPart(worksheetPart);

                var nsManager = new XmlNamespaceManager(mainDoc.NameTable);
                nsManager.AddNamespace("default", mainDoc.DocumentElement.NamespaceURI);
                var nodeList = mainDoc.SelectNodes("//default:sheets/default:sheet", nsManager);

                foreach (XmlNode node in nodeList)
                {
                    var sheetName = node.Attributes["name"].Value;
                    if (sheetName == "Sheet1")
                        node.Attributes["r.id"].Value = relId;
                }

                var stream = workbookPart.GetStream();
                mainDoc.Save(stream);

                var worksheetXml = Path.Combine(fileName, @"..\..\sheet1.xml");
                var xsheet = new XmlDocument();
                xsheet.Load(worksheetXml);
                var stream1 = worksheetPart.GetStream();
                xsheet.Save(stream1);

workbookPart.Workbook.Save();
            }

...

            return buffer;
        }

private string GetQuarterlyReportXml(int fiscalYear, string reportType, int officeId)
        {
            var util = new esUtility();
            string output = util.ExecuteScalar<string>(esQueryType.Text, @"
                EXEC [dbo].[proc_QPT_GetGPRA_ARRA_FA_QuarterlyReport] {0}, {1}, {2}",
                fiscalYear, reportType, officeId);

            // if we got nothing then return null
            if (string.IsNullOrEmpty(output))
                return null;

            return output;
        }

        private bool TransformXmlToOpenXml(string xml, string outputPath)
        {
            //There is no xml
            if (string.IsNullOrEmpty(xml))
                return false;

            var documentTransform = "NETL.AOP.Data.ProcessingDocument.reportTransform.xslt";
            using (Stream strm = Assembly.GetExecutingAssembly().GetManifestResourceStream(documentTransform))
            {
                using (XmlReader rdr = XmlReader.Create(strm))
                {
                    var transform = new XslCompiledTransform();
                    transform.Load(rdr);

                    var outputFileName = Path.Combine(outputPath, "workbook.xml");
                    var writer = XmlWriter.Create(outputFileName);
                    XmlDocument dom = new XmlDocument();
                    dom.LoadXml(xml);

                    transform.Transform(dom, writer);
                    writer.Close();
                }
            }

            return true;
        }

0 个答案:

没有答案