using (var fs = new FileStream("a.xlsx", FileMode.Open, FileAccess.Read)) { wb = new XSSFWorkbook(fs); for (int i = 0; i < wb.Count; i++) { comboBox1.Items.Add(wb.GetSheetAt(i).SheetName); } } // clear grid before filling dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear(); // get sheet sh = (XSSFSheet)wb.GetSheet(comboBox1.SelectedItem.ToString()); int i = 0; while (sh.GetRow(i) != null) { // add necessary columns if (dataGridView1.Columns.Count < sh.GetRow(i).Cells.Count) { for (int j = 0; j < sh.GetRow(i).Cells.Count; j++) { dataGridView1.Columns.Add("", ""); } } // add row dataGridView1.Rows.Add(); // write row value for (int j = 0; j < sh.GetRow(i).Cells.Count; j++) { var cell = sh.GetRow(i).GetCell(j); if (cell != null) { // TODO: you can add more cell types capability, e. g. formula switch (cell.CellType) { case NPOI.SS.UserModel.CellType.Numeric: dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).NumericCellValue; break; case NPOI.SS.UserModel.CellType.String: dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).StringCellValue; break; } } } i++; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)