NPOI导入excel

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
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 Excel
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_DragEnter(object sender, DragEventArgs e)
        {
                                                                      
            


        }

        private void Form2_DragDrop(object sender, DragEventArgs e)
        {

            


        }

        private void InputWorkbook(string filePath)
        {
            if (filePath != "")
            {
                try
                {
                    string fileType = filePath.Substring(filePath.LastIndexOf(".") + 1);//取得文件后缀
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);//创建文件流
                    bool isXls = true;//判断文件类型
                    if (fileType == "xlsx")
                    {
                        isXls = false;
                    }
                    IWorkbook workbook = CreateWorkbook(isXls, fs);//创建工作簿
                    ISheet sheet = workbook.GetSheetAt(0);//取得第一个工作表
                    int rowCount = sheet.LastRowNum + 1;//取得行数
                    int colCount = sheet.GetRow(0).LastCellNum;//取得列数
                    //初始化datagridview
                    dataGridView3.Rows.Clear();
                    dataGridView3.Columns.Clear();
                    for (int c = 0; c < colCount; c++)//遍历Excel第一行,生成dataGridView1列名
                    {
                        ICell cell = sheet.GetRow(0).GetCell(c);
                        dataGridView3.Columns.Add(c.ToString() + cell.ToString(), cell.ToString());
                    }

                    for (int r = 1; r < rowCount; r++)//遍历Excel其他行,生成dataGridView1单元格内容
                    {//遍历Excel行,从第二行开始
                        IRow row = sheet.GetRow(r);
                        int index = dataGridView3.Rows.Add();
                        colCount = row.LastCellNum;
                        for (int c = 0; c < colCount; c++)
                        {//遍历每个单元格,将单元格内容填入dataGridView1单元格中
                            ICell cell = row.GetCell(c);
                            if (cell == null)//如果该单元格没有内容,跳过
                            {
                                continue;
                            }
                            dataGridView3.Rows[index].Cells[c].Value = cell.ToString();
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("导入失败: " + ex.Message);

                }
            }
            else
            {
                MessageBox.Show("请选择Excel文件");
            }
        }
        //创建工作簿
        private static IWorkbook CreateWorkbook(bool isXLS, FileStream fs)
        {
            if (isXLS)
            {
                return new HSSFWorkbook(fs);
            }
            else
            {
                return new XSSFWorkbook(fs);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView3_DragDrop(object sender, DragEventArgs e)
        {
            string filepath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            string extension = System.IO.Path.GetExtension(filepath);//文件后缀名
            if (extension == ".xls" || extension == ".xlsx")
            {
                textBox2.Text = filepath;
            }
        }

        private void dataGridView3_DragEnter(object sender, DragEventArgs e)
        {
            string filepath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//文件完整路径
            string extension = System.IO.Path.GetExtension(filepath);//文件后缀名
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else e.Effect = DragDropEffects.None;
            if (extension == ".xls" || extension == ".xlsx")
            {
                InputWorkbook(filepath);
            }
        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值