This project has moved and is read-only. For the latest updates, please go here.

List of Worksheets in a ListBox or ComboBox

Feb 13, 2012 at 7:51 PM

Hello all,

I'd like to read the names of all worksheets of an existing xls-sheet into a ListBox or ComboBox.

Presently, I have:

private void btnOpen_Click(object sender, EventArgs e)
        {
            // Displays an OpenFileDialog so the user can select an Excel File.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Excel Files|*.xlsx";
            openFileDialog1.Title = "Select an Excel File";

            // Show the Dialog.
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
                lblFileName.Text  = openFileDialog1.FileName;
        }

        if (openFileDialog1.FileName == String.Empty)
        {
            lblFileName.Text = "";
            return;//user didn't select a file
        }

        // Open Excel File
        using (ExcelPackage package = new ExcelPackage(openFileDialog1.OpenFile()))

        // get the worksheets in the workbook
        lBoWorksheet.Items.Add(package.Workbook.Worksheets[1]);

}

 

However, this only give me the name for the first worksheet...

Thanks,

Benjamin

Feb 16, 2012 at 8:34 PM

Worksheets is a collection. Use a foreach or for loop to iterate the worksheets.