Example #1
0
        public static IEnumerable <ValidationErrorInfo> GetOpenXmlValidationErrors(string fileName,
                                                                                   string officeVersion)
        {
#if !NET35
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2013;
#else
            FileFormatVersions fileFormatVersion = FileFormatVersions.Office2010;
#endif
            try
            {
                fileFormatVersion = (FileFormatVersions)Enum.Parse(fileFormatVersion.GetType(), officeVersion);
            }
            catch (Exception)
            {
#if !NET35
                fileFormatVersion = FileFormatVersions.Office2013;
#else
                fileFormatVersion = FileFormatVersions.Office2010;
#endif
            }

            FileInfo fi = new FileInfo(fileName);
            if (Util.IsWordprocessingML(fi.Extension))
            {
                WmlDocument wml = new WmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(wml))
                    using (WordprocessingDocument wDoc = streamDoc.GetWordprocessingDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsSpreadsheetML(fi.Extension))
            {
                SmlDocument Sml = new SmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Sml))
                    using (SpreadsheetDocument wDoc = streamDoc.GetSpreadsheetDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            else if (Util.IsPresentationML(fi.Extension))
            {
                PmlDocument Pml = new PmlDocument(fileName);
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(Pml))
                    using (PresentationDocument wDoc = streamDoc.GetPresentationDocument())
                    {
                        OpenXmlValidator validator = new OpenXmlValidator(fileFormatVersion);
                        var errors = validator.Validate(wDoc);
                        return(errors.ToList());
                    }
            }
            return(Enumerable.Empty <ValidationErrorInfo>());
        }
Example #2
0
 public static XElement RetrieveRange(SmlDocument smlDoc, string sheetName, int leftColumn, int topRow, int rightColumn, int bottomRow)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(smlDoc.DocumentByteArray, 0, smlDoc.DocumentByteArray.Length);
         using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, false))
         {
             return(RetrieveRange(sDoc, sheetName, leftColumn, topRow, rightColumn, bottomRow));
         }
     }
 }
Example #3
0
 public static string[] TableNames(SmlDocument smlDoc)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(smlDoc.DocumentByteArray, 0, smlDoc.DocumentByteArray.Length);
         using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, false))
         {
             return(TableNames(sDoc));
         }
     }
 }
Example #4
0
 public static XElement RetrieveRange(SmlDocument smlDoc, string sheetName, string range)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(smlDoc.DocumentByteArray, 0, smlDoc.DocumentByteArray.Length);
         using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, false))
         {
             return(RetrieveRange(sDoc, sheetName, range));
         }
     }
 }
 // ***********************************************************************************************************************************
 #region PublicApis
 public static XElement ConvertTableToHtml(SmlDocument smlDoc, SmlToHtmlConverterSettings settings, string tableName)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         ms.Write(smlDoc.DocumentByteArray, 0, smlDoc.DocumentByteArray.Length);
         using (SpreadsheetDocument sDoc = SpreadsheetDocument.Open(ms, false))
         {
             var rangeXml = SmlDataRetriever.RetrieveTable(sDoc, tableName);
             var xhtml    = SmlToHtmlConverter.ConvertToHtmlInternal(sDoc, settings, rangeXml);
             return(xhtml);
         }
     }
 }