0

The requirement is that the client will send the Excel data in the form of Byte Array (will provide API link for the same to the client)

Now when the API receives the Byte Array, i want to convert into datatable for converting into XML.

Below is the code i have tried

 public string ConvertExcelByteArraytoXML()
        {

            byte[] Excelbytes = null;
            FileStream fs = File.OpenRead("C:\\PRATAP FOLDER\\test.xlsx");            
            BinaryReader binaryReader = new BinaryReader(fs);
            Excelbytes = binaryReader.ReadBytes((int)fs.Length);
            string CreateXMLFILE = string.Empty;

       // the above code was to get byte array from excel 

            DataSet tempDataSet = new DataSet();
            DataTable dt;
            // Deserializing into datatable    
            using (MemoryStream stream = new MemoryStream(Excelbytes))
            {
                BinaryFormatter bformatter = new BinaryFormatter();
                dt = (DataTable)bformatter.Deserialize(stream);

            }
            // Adding DataTable into DataSet    
            tempDataSet.Tables.Add(dt);

           using (StringWriter sw = new StringWriter())
            {
                dt.WriteXml(sw);
                CreateXMLFILE = sw.ToString();
            }
          return CreateXMLFILE;

}

Throwing error at

dt = (DataTable)bformatter.Deserialize(stream);

{"The input stream is not a valid binary format. The starting contents (in bytes) are: 50-4B-03-04-14-00-06-00-08-00-00-00-21-00-EB-7A-D2 ..."}

Can someone suggest what am i doing wrong here.

5
  • Based on my test, I can reproduce your problem. However, I find it hard for me to convert the byte array from excel to datatable successfully. Because binaryformatter doesn't know the structure of datatable. Therefore, I suggest that you could convert excel to datatable directly instead of using byte array. Commented Nov 28, 2019 at 2:35
  • Yeah that is easy way..but the client is sending in byte array format Commented Nov 28, 2019 at 5:04
  • If so, I suggest that you can convert byte array to excel file first. Then, you could convert the excel file to datatable. Commented Nov 28, 2019 at 5:38
  • I dont want to save the excel sheet in server/locally ...looking for an alternate approach..with this approach i have already written the code using IExcelDataReader Commented Nov 28, 2019 at 6:18
  • If you don't want to save the excel sheet in server/locally, I suggest that you can delete them after you using it. Commented Nov 29, 2019 at 1:26

1 Answer 1

1

Like I mentioned in the comment, you can try the following code to convert the byte array

to datatable.

Code:

class Program
    {
        static void Main(string[] args)
        {
            byte[] b = File.ReadAllBytes("D:\\2.xlsx");      // you get from the client
            File.WriteAllBytes("test.xlsx", b);             //Create an hourly file
            DataTable table = Exceltodatatable("test.xlsx");  // convert it to datatable
            File.Delete("test.xlsx");                         // delete the file
        }

        public static DataTable Exceltodatatable(string path)
        {
            DataTable dt = new DataTable();
            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";" + "Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;MAXSCANROWS=0'";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                using (OleDbCommand comm = new OleDbCommand())
                {
                    string sheetName = "sheet1";
                    comm.CommandText = "Select * from [" + sheetName + "$]";
                    comm.Connection = conn;
                    using (OleDbDataAdapter da = new OleDbDataAdapter())
                    {
                        da.SelectCommand = comm;
                        da.Fill(dt);
                        return dt;
                    }
                }

            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.