c# NPOI讀取excel2010


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());

                    }
                   
                }
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM