Search code examples
c#.netwinformsepplusexceldatareader

Invalid file signature


I was working on a winform application, the form contains a plain button and its event handler contains the code to read, modify and create an updated xlsb file. This xlsb file that i am working on contains two tabs, one is for the pivot table analysis and the second tab contains the raw data. I want to change the value in a specific cell of the raw data table (row no. -> 15930 col no. -> 17) to 100. Also I want to check if those changes are reflected in the pivot table analysis. The event handler for the button looks like:

using ExcelDataReader;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExcelModifierNew
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string inputFilePath = @"C:\Users\zzk51\Downloads\North 1 PvsT.xlsb"; // Input XLSB file path
                string outputFilePath = @"C:\Users\zzk51\Downloads\North 1 PvsT_modified.xlsb"; // Output XLSB file path

                // Read the XLSB file using ExcelDataReader
                using (FileStream stream = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(stream))
                    {
                        DataSet result = reader.AsDataSet();

                        // Modifying the specific cell value
                        if (result.Tables.Count > 0)
                        {
                            DataTable worksheet = result.Tables[1]; //For the second worksheet

                            // Modifying the cell value (row 15930, column 17) to 100
                            if (worksheet.Rows.Count >= 15930 && worksheet.Columns.Count >= 17)
                            {
                                worksheet.Rows[15929][16] = 100;
                            }
                            else
                            {
                                MessageBox.Show("Cell not found. Ensure the worksheet has enough rows and columns.");
                                return;
                            }

                            // Saving the changes using EPPlus
                            using (var package = new ExcelPackage(new FileInfo(outputFilePath)))
                            {
                                ExcelWorksheet ws = package.Workbook.Worksheets.Add("Worksheet1");
                                ws.Cells.LoadFromDataTable(worksheet, true);
                                package.Save();
                            }

                            MessageBox.Show("Excel file modified successfully. Saved as: " + outputFilePath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}

The list of libraries that I've added:

  1. ExcelDataReader
  2. ExcelDataReader.Dataset
  3. EPPlus

I was getting this error: "Invalid File Signature" after clicking the button. I am new to working with winforms and xlsb files, I apologize if something sounds dumb. Comment if you need more clarification. Below is the the screenshot of the exception message: Exception Message


Solution

  • You are probably using version 3.6.0 or earlier of ExcelDataReader. This version doesn't support reading the XLSB format.

    Support for XLSB in ExcelDataReader is only introduced with version 3.7.0, which is not yet officially released, there are only pre-release versions. See https://github.com/ExcelDataReader/ExcelDataReader/releases

    So, either use the latest pre-release version of ExcelDataReader or use XLSX instead of XLSB format or use some other library for reading the file.


    Note that when using ExcelDataReader, you must also use CreateReader instead of CreateBinaryReader.

    From C# error With ExcelDataReader:

    The code calls ExcelReaderFactory.CreateBinaryReader which is for the XLS format.

    Instead, change to use ExcelReaderFactory.CreateOpenXmlReader for the XLSX format specifically, or use ExcelReaderFactory.CreateReader to autodetect either XLS or XLSX formats transparently.