OpenXML spreadsheet created in .NET won't open in iPad

后端 未结 5 2039
梦毁少年i
梦毁少年i 2020-12-16 05:59

I am trying to generate a spreadsheet in .NET which will be opened by my manager on his iPad when he\'s out of the office.

The spreadsheet opens fine on a Windows PC

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 06:39

    Comradsky's answer of sending a pdf is a good idea, but in case anybody needs to be able to resolve this, I have come up with a solution. I know this is a horrible hack but it works and I've spent hours trying to find a way to do it "legally" to no avail.

    It involves opening the .rels file and directly editing the xml within the file after the document has been closed.

        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());
            }
        }
    

提交回复
热议问题