Search code examples
.net-corespreadsheetlight

Error message "entries cannot be opened multiple times in update mode." in Spreadsheet Lite SaveAs function


Upon execution of the dBWorksheet.SaveAs(xlsFileSpec), in the code below, I am seeing an exception:

"entries cannot be opened multiple times in update mode."

        SLDocument dBWorksheet = new SLDocument();
        TimeSpan interval = new TimeSpan(0, 0, 2);

        dBWorksheet.SetCellValue(2, 1, "Hour");
        dBWorksheet.SetCellValue(3, 1, "Time");

        int Row = 3;

        // Create the hour and time of day columns.
        for(TimeSpan dBTime = new TimeSpan(0, 0, 0); dBTime.TotalHours < 24; dBTime = dBTime.Add(interval)) 
        {
            dBWorksheet.SetCellValue(Row, 1, dBTime.Hours);
            dBWorksheet.SetCellValue(Row, 2, dBTime.ToString());

            Row++;
        }

        // Save the new worksheet.
        dBWorksheet.SaveAs(xlsFileSpec);

Solution

  • Here's how I solved it.

    1. Downloaded the source code for SpreadsheetLight (version 3.5). http://spreadsheetlight.com/downloads/SpreadsheetLight3.5.zip

    2. Created a .NET Core library project with the name "SpreadsheetLight" and added necessary NuGet packages (DocumentFormat.OpenXML and System.Drawing.Common) to it. Copied and pasted all the downloaded source code files in this project.

    3. Added the project "SpreadsheetLight" to my solution and referenced it in one of the existing projects.

    4. In "SLDocument.cs" file, make the following changes in the method "LoadDocumentProperties()" so that the code looks like the following:

    // XDocument xdoc = XDocument.Load(XmlReader.Create(xl.CoreFilePropertiesPart.GetStream()));
            
    Stream stream = xl.CoreFilePropertiesPart.GetStream();
    XDocument xdoc = XDocument.Load(XmlReader.Create(stream));
            
    foreach (XElement xelem in xdoc.Descendants())
    {
        // Code omitted.
    }
            
    stream.Close();
    
    1. Build your solution and test it.