C# SpreadsheetLight corrupts excel file after save

1.2k Views Asked by At

Due to some issues by running .xlsm files over the network it was decided not to use VBA anymore and to develop standalone apps that will edit regular excel files. Since I have a some C# and Visual Studio knowledge I decided to use those tools. Since Iterop.Excel is really slow I decided to use SpreadsheetLight.

Everything went smooth during while reading and analyzing data but after I added some records and save the file the file become corrupted: when trying to open with excel I got the following message:

"We found A problem with some content. Do you want us to recover as much as we can? If you trust the source of this workbook, click yes". After click yes got the message that it cannot be recovered because is corrupt.

Even if I don't add any records and just save the file got corrupted.

The thing is that the file opens without any issues in OpenOffice, all the records are there.

Any help will be appreciated!

Below the class that implements the r/w of the excel file:

class SPREADSHEET_TOOLS
{
    public string file_name;

    public SLDocument doc;

    public List<string> sheets;

    MemoryStream ms;

    public SPREADSHEET_TOOLS()
    {
        
        
    }   

    public bool init(string _file_name)
    {
        this.file_name = _file_name;
        ms = new MemoryStream();

        try
        {
            FileStream stream = File.Open(this.file_name, FileMode.Open);
            
            this.doc = new SLDocument(stream);
            this.sheets = doc.GetSheetNames();

            stream.Close();

            
        }
        catch (IOException)
        {
            MessageBox.Show("Fisierul este deschis de un alt utilizator. Nu poate fi accesat!!!!");
            return false;
        }
        return true;
    }

    public List<string>getUniqeRowValues(string sheet,int row)
    {
        List<string> values = new List<string>();

        if (this.sheets.Contains(sheet))
        {
            this.doc.SelectWorksheet(sheet);
            while (this.doc.GetCellValueAsString(row, 1) != "")
            {
                if (values.Count == 0)
                {
                    values.Add(this.doc.GetCellValueAsString(row, 1));
                }
                else
                {
                    if (!values.Contains(this.doc.GetCellValueAsString(row, 1)))
                    {
                        values.Add(this.doc.GetCellValueAsString(row, 1));
                    }
                }

                row++;
            }

        }

        return values;
    }

    public List<string>getChildValues(string sheet, string parent, int row, int column_parent, int column_child)
    {
        List<string> values = new List<string>();
        if (this.sheets.Contains(sheet))
        {
            this.doc.SelectWorksheet(sheet);
            while (this.doc.GetCellValueAsString(row, column_parent) != "")
            {
                if (this.doc.GetCellValueAsString(row, column_parent) == parent)
                {
                    values.Add(this.doc.GetCellValueAsString(row, column_child));
                }
                row++;
            }
        }
            return values;
    }
    public int getLastRow(string sheet)
    {
        int row=0;
        
        if (this.sheets.Contains(sheet))
        {
            this.doc.SelectWorksheet(sheet);
            row = 1;
            while (this.doc.GetCellValueAsString(row, 1) != "")
            {
                row++;
            }
        }
        return row;
    }
    
    public bool writeRow(string[] data, string sheet,int row)
    {
        if (this.sheets.Contains(sheet))
        {
            this.doc.SelectWorksheet(sheet);
            for (int i=0; i < data.Length; i++)
            {
                InlineString str = new InlineString();

                
                //bool a = this.doc.SetCellValue(row,i+1,data[i]);

            }
            //this.doc.SaveAs(this.ms);
            foreach (string s in this.sheets)
            {
                this.doc.SelectWorksheet(s);
                
            }
            this.doc.DocumentProperties.Creator = "CP";
            this.doc.SaveAs("E:\\C-SHARP\\PONTAJ\\PONTAJ\\BUBU.XLSX");
            MessageBox.Show("Saved!");

            return true;
        } 

        return false;
    }
}
3

There are 3 best solutions below

0
On

I also faced the same problem, Excel file gets corrupted after downloading. So I have done some fixes and update SpreadSheetLight code to .NET 6.

You can download source code from here: https://github.com/bhavinvachhani403/SpreadSheetLight_Net6.0

I hope this will helps you to solve your problem.

0
On

I had the same error and I solved it by changing the version of DocumentFormat.OpenXml to version 2.5

0
On

I had this same error when trying to modify an existing document.

It was due to a file being loaded in Protected view. You need to disable that.

Fix for me: Go to File -> Options -> Trust Center -> Trust Center Settings -> Protected View -> Uncheck all boxes