using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; namespace CheckWork { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnInput_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Excel|*.xlsx|Excel|*.xls"; if (ofd.ShowDialog() == DialogResult.OK) { //打開選擇的Excel文件 using (FileStream file = new FileStream(ofd.FileName, FileMode.Open)) { //根據現有的Excel文檔建立工做簿 XSSFWorkbook workbook = new XSSFWorkbook(file); //得到工做表 ISheet sheet0 = workbook.GetSheetAt(0); //得到全部行的集合 System.Collections.IEnumerator rows = sheet0.GetRowEnumerator(); //跳過第一行 rows.MoveNext(); //向下移動 while (rows.MoveNext()) { //得到當前行 XSSFRow row = rows.Current as XSSFRow; //獲取第一個格子的值 MessageBox.Show(row.GetCell(0).ToString()); } } } } } }