Akshay Parekh
Akshay Parekh

Reputation: 41

Validate Excel file in c# using Documentformat.OpenXML

I am getting this error("Exception thrown: 'Xunit.Sdk.EqualException' in xunit.assert.dll") while validating the Excel file using DocumentFormat.OpenXML. I want to validate excel file using # and I am using DocumentFormat.OpenXML

using System;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using Xunit;
using P = DocumentFormat.OpenXml.Presentation;
using S = DocumentFormat.OpenXml.Spreadsheet;
using W = DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;


namespace ExcelValidation2
{
    class Program
    {
        static void Main(string[] args)
        {
            var excelFile = "ExcelValidation.xlsx";
            var readFile = File.ReadAllBytes(excelFile);
            using(MemoryStream ms = new MemoryStream())
            {
                ms.Write(readFile, 0, readFile.Length);
                using (SpreadsheetDocument doc = SpreadsheetDocument.Open(ms, true))
                {
                    //var corePart = doc.CoreFilePropertiesPart;
                    //var appPart = doc.ExtendedFilePropertiesPart;
                    //doc.DeletePart(corePart);
                    //doc.DeletePart(appPart);
                    //doc.AddCoreFilePropertiesPart();
                    //doc.AddExtendedFilePropertiesPart();
                    //doc.AddCustomFilePropertiesPart();
                    //doc.AddDigitalSignatureOriginPart();
                    //doc.AddExtendedPart("realType", "contentType/xml", ".xml");
                    //var tnPart = doc.AddThumbnailPart(ThumbnailPartType.Jpeg);
                    //doc.DeletePart(tnPart);
                    //tnPart = doc.AddThumbnailPart("image/jpg");
                    OpenXmlValidator v = new OpenXmlValidator(DocumentFormat.OpenXml.FileFormatVersions.Office2013);
                    var errs = v.Validate(doc);
                    Assert.Equal(1, errs.Count());
                }
            }
        }
    }
}

Upvotes: 3

Views: 4335

Answers (2)

sopcce
sopcce

Reputation: 19

if excel File is not read in SpreadsheetDocument or Damaged documents

DocumentFormat.OpenXml.Packaging.OpenXmlPackageException:“The specified package is invalid. The main part is missing.”

if Assert.Equal(1, errs.Count());

System.InvalidOperationException:“Assert.Equals should not be used for Assertions, use Assert.AreEqual(...) instead.”

you can try

Assert.Equal(0, errs.Count());

I use

<package id="DocumentFormat.OpenXml" version="2.8.1" targetFramework="net45" /> <package id="NUnit" version="3.10.1" targetFramework="net45" />

Upvotes: 0

Anoop J
Anoop J

Reputation: 267

Can check this method to validate :

private void ValidateExcel()
{
    try
    {
        var validator = new OpenXmlValidator();
        int count = 0;
        foreach (ValidationErrorInfo error in validator.Validate(SpreadsheetDocument.Open(openFileDialog1.FileName, true)))
        {

            count++;
            lblError.Text += ("Error Count : " + count) + "\r\n";
            lblError.Text += ("Description : " + error.Description) + "\r\n";
            lblError.Text += ("Path: " + error.Path.XPath) + "\r\n";
            lblError.Text += ("Part: " + error.Part.Uri) + "\r\n";
        }
        Console.ReadKey();
    }
    catch (Exception ex)
    {
        lblError.Text += (ex.Message);
    }
}

Upvotes: 3

Related Questions