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.