web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to convert an excel file into byte array and perform some validation checks on the data

(0) ShareShare
ReportReport
Posted on by 37

Hi Friends,

Can someone help me on writing a C# code to convert the data from an excel file into byte array and then perform some validation check to the various columns in the excel like checking the date format, length of data, not null etc

Thanks

JB

*This post is locked for comments

I have the same question (0)
  • braddev Profile Picture
    62 on at

    Hi JB,

    You need to use File.ReadAllBytes command to do this.

    Here is the Microsoft documentation docs.microsoft.com/.../system.io.file.readallbytes

    In practice you will end up with something like this...

    -----

    using System;

    using System.IO;

    class Program

    {

       static void Main()

       {

           byte[] array = File.ReadAllBytes("YOUREXCELFILELOCATIONHERE");

           Console.WriteLine(array.Length);

       }

    }

    ----

    Hope this helps...

  • Suggested answer
    Saad Kabarousse Profile Picture
    734 on at

    Hello,

    This is the code i use:

           static public byte[] FileToByteArray(string path)

           {

               FileStream fs = new FileStream(path, FileMode.Open);

               byte[] binary = StreamtoByteArray(fs);

               fs.Close();

               return binary;

           }

  • CRM Reports Profile Picture
    37 on at

    Thanks Braddev for your response. Converting the excel data into byte array was working with your code but how can i do validations on the value from the excel. For example, I have date of birth column in which I need to check the length of the value is 6 and if its 5 then add a '0' at the front and if it less than 5 or more than 6 just throw some error. How can I implement this.

  • Verified answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    The safest and the easiest way would be creating WebApi to handle converting the file into byte array and perform some validation. You can using third party libs like ClosedXML. In there you can convert excel file become c# POCO and do some validation. If the validation pass, then you can return byte array as result then your plugin can just use the value that already clean.
    Why WebApi? Because it will help you to ease your development (no need doing merge) and will be more easy in the future if you want to upgrade to online version.

  • CRM Reports Profile Picture
    37 on at

    Thanks Temmy for your response, I am very new to WepApi, could you provide me some sample code for performing this.

    Thanks

    JB

  • Suggested answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    You can check this url: stackoverflow.com/.../7920473

  • CRM Reports Profile Picture
    37 on at

    Hi Temmy,

    I tried wiriting a c# code like below for converting into byte array

     private static void GetExcelData()

           {

               try

               {

                   var fileBytes = File.ReadAllBytes(@"filepath");

                   using (var stream = new MemoryStream(fileBytes))

                   using (var document = SpreadsheetDocument.Open(stream, true))

                   {

                   var workbookpart = document.WorkbookPart;

                   WorksheetPart worksheetPart = GetWorksheetFromSheetName(workbookpart, "Sheet1");

                   Worksheet sheet = worksheetPart.Worksheet;

                    var cells = sheet.Descendants<Cell>();

                    var rows = sheet.Descendants<Row>();

                       var sharedStringPart = workbookpart.SharedStringTablePart;

                       var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray();

                       var count = 1;

                       var rowindex = "";

                       foreach (Row row in rows)

                       {

                           if (row.RowIndex == 1)

                               continue;

                           if (row.RowIndex == 2)

                          count++;

                          rowindex = "A" + count;

                   }  }  } }

    I am stuck here on how to proceed to check whether a particular cell is empty or a whole A coulmn is empty. Can you guide me how to write this.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans