Search code examples
biztalkpipelineimport-from-excel

BizTalk SpreadsheetDocument.Open Stream: Index was outside the bounds of the array


I am trying to build a custom pipeline which will be able to read an Excel file. When I run this code on Windows 7 - it works fine. When I run the code on Win 2k8 - I get an error: "Index was outside the bounds of the array." (it fails when trying to open the stream).

if I test the component on both boxes (load a file and process it) - it works fine. The error only happens when it comes directly from BizTalk via File adapter.

if i change the code to load a file (regardless of what is coming in from the pipeline) - it works fine!

Any Ideas?

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
    {            

    var excelAsStream = inmsg.BodyPart.GetOriginalDataStream();

    try
    {

        //excelAsStream.Position = 0;
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(excelAsStream, false))  // Here I get the Error
        {

Solution

  • So far to fix the issue, I have done the following:

    1. read excel file as Stream
    2. save it locally with ".tmp" extension
    3. Read tmp file and covert to xml
    4. delete tmp file
    5. Pass message to MessageBox

    Code:

    var fullFileName = string.Empty; // tmp file name to be loaded via spreadsheet
    IBaseMessageContext context = inmsg.Context;
    
    try
    
    {
    if (inmsg.BodyPart == null)
        throw new ArgumentNullException("inmsg.BodyPart","Incoming Message is not Valid stream");
    
    
    var srcFileName = context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();
    // write file to local folder as tmp
    var fileTmp = Path.GetFileNameWithoutExtension(srcFileName);
    var pathTmp = Path.GetDirectoryName(srcFileName) ?? string.Empty;
    fullFileName = Path.Combine(pathTmp, fileTmp + ".tmp");
    
    var excelAsStream = inmsg.BodyPart.GetOriginalDataStream(); // get the msg as Stream
    using (var fileStream = File.Create(fullFileName))
    {
        excelAsStream.Seek(0, SeekOrigin.Begin);
        excelAsStream.CopyTo(fileStream);
    }
    
    
    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fullFileName, false))
    {   
        // Do code here
    }
    
    } // end try