6

.NET でスプレッドシートを生成しようとしています。マネージャーが不在のときに iPad で開くことができます。

スプレッドシートは Windows PC では正常に開きますが、iPad で開こうとすると、「ドキュメントの読み取り中にエラーが発生しました」と表示されます (非常に便利です!)

OpenXML SDK Productivity ツールの「比較」機能を iPad で開くドキュメントで使用し、問題のあるドキュメントの XML ファイルをメモ帳で手動で編集することにより、ファイルxl/_rels/workbookに絞り込みました。ワークブック内のパーツの関係を格納する.xml.rels 。

これは、WorkbookPart と参照を生成するために使用しているコードです。

    WorkbookPart workbookPart1 = document.AddWorkbookPart();

    WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId3");
    ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId2");
    WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");

私のコードは次の出力を生成しますが、iPad では開きません。

      <?xml version="1.0" encoding="utf-8" ?> 
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="/xl/styles.xml" Id="rId3" /> 
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="/xl/theme/theme.xml" Id="rId2" /> 
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="/xl/worksheets/sheet.xml" Id="rId1" /> 
      </Relationships>

Target 属性の値を相対参照パスを使用するように変更すると、次の出力が得られ、iPad で開きます。

      <?xml version="1.0" encoding="utf-8" ?> 
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml" Id="rId3" /> 
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme.xml" Id="rId2" /> 
          <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet.xml" Id="rId1" /> 
      </Relationships>

問題は
、.NET コードを変更して、XML の 2 番目のバージョンを相対パスで出力するようにするにはどうすればよいかということです。

すべての助けに感謝します!

4

5 に答える 5

6

私はこれを調査するのに多くの時間を費やし、結果を共有したいと考えました. OpenXML は 2 つのことを行っているようです。1. content_types.xml ファイルにワークブックのエントリがありません。 2. xl/_rels/workbook.xml.rels ファイルが完全相対パスを使用しています。

Excel自体はファイルを正常に開きますが、iPadでさまざまなアプリを試しましたが、すべて失敗します。そのため、次のコードを使用して自分でファイルを手動で修正する必要がありました。ファイルのコンテンツ全体がストリームとして渡されると想定し、DotNetZip を使用して開いて操作します。このコードが他の人に役立つことを願っています!

    private Stream ApplyOpenXmlFix(Stream input)
    {
        const string RELS_FILE = @"xl/_rels/workbook.xml.rels";
        const string RELATIONSHIP_ELEMENT = "Relationship";
        const string CONTENT_TYPE_FILE = @"[Content_Types].xml";
        const string XL_WORKBOOK_XML = "/xl/workbook.xml";
        const string TARGET_ATTRIBUTE = "Target";
        const string SUPERFLUOUS_PATH = "/xl/";
        const string OVERRIDE_ELEMENT = "Override";
        const string PARTNAME_ATTRIBUTE = "PartName";
        const string CONTENTTYPE_ATTRIBUTE = "ContentType";
        const string CONTENTTYPE_VALUE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";

        XNamespace contentTypesNamespace = "http://schemas.openxmlformats.org/package/2006/content-types";
        XNamespace relsNamespace = "http://schemas.openxmlformats.org/package/2006/relationships";
        XDocument xlDocument;
        MemoryStream memWriter;

        try
        {
            input.Seek(0, SeekOrigin.Begin);
            ZipFile zip = ZipFile.Read(input);

            //First we fix the workbook relations file
            var workbookRelations = zip.Entries.Where(e => e.FileName == RELS_FILE).Single();
            xlDocument = XDocument.Load(workbookRelations.OpenReader());

            //Remove the /xl/ relative path from all target attributes
            foreach (var relationship in xlDocument.Root.Elements(relsNamespace + RELATIONSHIP_ELEMENT))
            {
                var target = relationship.Attribute(TARGET_ATTRIBUTE);

                if (target != null && target.Value.StartsWith(SUPERFLUOUS_PATH))
                {
                    target.Value = target.Value.Substring(SUPERFLUOUS_PATH.Length);
                }
            }

            //Replace the content in the source zip file
            memWriter = new MemoryStream();
            xlDocument.Save(memWriter, SaveOptions.DisableFormatting);
            memWriter.Seek(0, SeekOrigin.Begin);
            zip.UpdateEntry(RELS_FILE, memWriter);

            //Now we fix the content types XML file
            var contentTypeEntry = zip.Entries.Where(e => e.FileName == CONTENT_TYPE_FILE).Single();
            xlDocument = XDocument.Load(contentTypeEntry.OpenReader());

            if (!xlDocument.Root.Elements().Any(e =>
                e.Name == contentTypesNamespace + OVERRIDE_ELEMENT &&
                e.Attribute(PARTNAME_ATTRIBUTE) != null &&
                e.Attribute(PARTNAME_ATTRIBUTE).Value == XL_WORKBOOK_XML))
            {
                //Add in the missing element
                var overrideElement = new XElement(
                    contentTypesNamespace + OVERRIDE_ELEMENT,
                    new XAttribute(PARTNAME_ATTRIBUTE, XL_WORKBOOK_XML),
                    new XAttribute(CONTENTTYPE_ATTRIBUTE, CONTENTTYPE_VALUE));

                xlDocument.Root.Add(overrideElement);

                //Replace the content
                memWriter = new MemoryStream();
                xlDocument.Save(memWriter, SaveOptions.DisableFormatting);
                memWriter.Seek(0, SeekOrigin.Begin);
                zip.UpdateEntry(CONTENT_TYPE_FILE, memWriter);
            }

            Stream output = new MemoryStream();

            //Save file
            zip.Save(output);

            return output;
        }
        catch
        {
            //Just in case it fails, return the original document
            return input;
        }
    }
于 2013-11-21T15:08:25.017 に答える
2

PDFを送信するというComradskyの答えは良い考えですが、誰かがこれを解決できる必要がある場合に備えて、私は解決策を考え出しました. 私はこれが恐ろしいハックであることを知っていますが、それは機能し、「合法的に」それを行う方法を見つけるのに何時間も費やしましたが、役に立ちませんでした.

ドキュメントを閉じた後、.rels ファイルを開き、ファイル内の xml を直接編集する必要があります。

    public static void MakeRelativePaths(string filepath)
    {
        // Get the namespace strings
        const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
        const string relationshipSchema = "http://schemas.openxmlformats.org/package/2006/relationships";

        string documentUri = null;
        string documentDirectory = null;
        string documentName = null;

        Uri relDocUri = null;

        XName targetAttributeName = null;
        string targetValue = null;

        //  Open the package
        using (Package xlPackage = Package.Open(filepath, FileMode.Open, FileAccess.ReadWrite))
        {
            // Get the directory and filename of the main document part (e.g. /xl/workbook.xml).
            foreach (System.IO.Packaging.PackageRelationship relationship in xlPackage.GetRelationshipsByType(documentRelationshipType))
            {
                documentUri = relationship.TargetUri.ToString();

                documentName = System.IO.Path.GetFileName(documentUri);
                documentDirectory = documentUri.Substring(0, documentUri.Length - documentName.Length);

                //  There should only be document part in the package, but break out anyway.
                break;
            }

            // Load the relationship document
            relDocUri = new Uri(documentDirectory + "_rels/" + documentName + ".rels", UriKind.Relative);
            XDocument relDoc = XDocument.Load(xlPackage.GetPart(relDocUri).GetStream());

            // Loop through all of the relationship nodes
            targetAttributeName = XName.Get("Target");
            foreach (XElement relNode in relDoc.Elements(XName.Get("Relationships", relationshipSchema)).Elements(XName.Get("Relationship", relationshipSchema)))
            {
                // Edit the value of the Target attribute
                targetValue = relNode.Attribute(targetAttributeName).Value;

                if (targetValue.StartsWith(documentDirectory))
                    targetValue = targetValue.Substring(documentDirectory.Length);

                relNode.Attribute(targetAttributeName).Value = targetValue;
            }

            // Save the document
            relDoc.Save(xlPackage.GetPart(relDocUri).GetStream());
        }
    }
于 2012-06-08T07:17:51.183 に答える
2

私もしばらくの間、これに似た問題に苦しんでいます。私は最終的に機能する解決策を思いつきました。これは私が問題を解決するために書いたコードです

        // Add a new worksheet part to the workbook.
        WorksheetPart newWorksheetPart = _document.WorkbookPart.AddNewPart<WorksheetPart>();
        newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());

        Sheets sheets = _document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
        string relationshipId = _document.WorkbookPart.GetIdOfPart(newWorksheetPart);

        //This bit is required for iPad to be able to read the sheets inside the xlsx file. The file will still work fine in Excel
        string relationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet";
        _document.Package.GetPart(_document.WorkbookPart.Uri).CreateRelationship(new Uri(newWorksheetPart.Uri.OriginalString.Replace("/xl/", String.Empty).Trim(), UriKind.Relative), TargetMode.Internal, relationshipType);
        _document.Package.GetPart(_document.WorkbookPart.Uri).DeleteRelationship(relationshipId);
        PackageRelationshipCollection sheetRelationships = _document.Package.GetPart(_document.WorkbookPart.Uri).GetRelationshipsByType(relationshipType);

        relationshipId = sheetRelationships.Where(f => f.TargetUri.OriginalString == newWorksheetPart.Uri.OriginalString.Replace("/xl/", String.Empty).Trim()).Single().Id;


        // Get a unique ID for the new sheet.
        uint sheetId = 1;
        if (sheets.Elements<Sheet>().Count() > 0)
            sheetId = sheets.Elements<Sheet>().Max(s => s.SheetId.Value) + 1;

        // Append the new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
        sheets.Append(sheet);

        _worksheets.Add(new Worksheet(newWorksheetPart.Worksheet, sheetId));

_document と _worksheets は、ソリューション クラスのプライベート変数です。

于 2013-03-20T13:01:30.193 に答える
0

OpenXML スプレッドシートが作成されたら、検証を試みることができます。

using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;

using (OpenXmlPackage document = SpreadsheetDocument.Open(spreadsheetPathToValidate, false))
{
    var validator = new OpenXmlValidator();
    IEnumerable<ValidationErrorInfo> errors = validator.Validate(document);
    foreach (ValidationErrorInfo info in errors)
    {
        try
        {
            Console.WriteLine("Validation information: {0} {1} in {2} part (path {3}): {4}",
                        info.ErrorType,
                        info.Node.GetType().Name,
                        info.Part.Uri,
                        info.Path.XPath,
                        info.Description);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Validation failed: {0}", ex);
        }
    }
}

それが役立つことを願って、

于 2012-06-07T13:24:57.917 に答える