zoukankan      html  css  js  c++  java
  • C# NPOI 读取Excel数据,附案例源码

    项目结构

    注意:需要引入NPOI类库

    C#代码

    Form1.cs

    using NPOI.HSSF.UserModel;
    using NPOI.SS.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 NPOIDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private const int DEFAULT_CHECK_CELL_NUM = 4;
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    DataTable dt=ReadExcelData(@"C:UsersappleDesktopTest.xls");
                    MessageBox.Show("ok");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
               
            }
            /// <summary>
            /// 读取Excel中数据
            /// </summary>
            /// <param name="filePath"></param>
            private DataTable ReadExcelData(object filePath)
            {
                try
                {
                    if (!File.Exists(filePath.ToString()))
                    {
                        throw new Exception("文件不存在!");
                    }
                    DataTable dtExcel = InitDataTable();
                    FileStream fsRead = new FileStream(filePath.ToString(), FileMode.Open);
                    //创建工作薄
                    IWorkbook workBook = new HSSFWorkbook(fsRead);
                    //获取Sheet
                    ISheet sheet = workBook.GetSheetAt(0);
                    //获取Excel中的行数
                    int ExcelRowsCount = sheet.LastRowNum;
                    Assert.IsTrue(ExcelRowsCount == 1, "未读到Excel数据!");
                    IRow currentRow;
                    DataRow dr;
                    for (int i = 1; i < ExcelRowsCount; i++)
                    {
                        dr = dtExcel.NewRow();
                        //当前行数据
                        currentRow = sheet.GetRow(i);
                        SetCurrentRowValue(dtExcel, dr, currentRow, currentRow.LastCellNum);
                    }
                    return dtExcel;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            /// <summary>
            /// 初始化DataTable
            /// </summary>
            /// <returns></returns>
            private DataTable InitDataTable()
            {
                DataTable dt_excel = new DataTable();
                dt_excel.Columns.Add("A");
                dt_excel.Columns.Add("B");
                dt_excel.Columns.Add("C");
                dt_excel.Columns.Add("D");
                return dt_excel;
            }
            /// <summary>
            /// 读取到的Excel的单元格数量
            /// </summary>
            /// <param name="currentCellNum"></param>
            private void CheckExcelCellNum(int readCurrentRowCellNum)
            {
                Assert.IsTrue(readCurrentRowCellNum > DEFAULT_CHECK_CELL_NUM, "Excel单元格列数超过:"+DEFAULT_CHECK_CELL_NUM+"");
            }
            /// <summary>
            /// 给DataTable动态赋值
            /// </summary>
            /// <param name="dr">DataTable当前行</param>
            /// <param name="currentRow">Excel当前行数据</param>
            /// <param name="currentCellNum">Excel的列数</param>
            private void SetCurrentRowValue(DataTable dtExcel, DataRow dr,IRow currentRow, int currentCellNum)
            {
                dr.BeginEdit();
                for (int j = 0; j < currentCellNum; j++)
                {
                    if (j >= DEFAULT_CHECK_CELL_NUM) break;
                    dr[j]= currentRow.GetCell(j).ToString().Trim();
                }
                dr.EndEdit();
                dtExcel.Rows.Add(dr);
            }
        }
    }

    Assert.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace NPOIDemo
    {
        public static class Assert
        {
            public static void IsTrue(bool flag,string msg)
            {
                if (flag)
                {
                    throw new Exception(msg);
                }
            }
        }
    }

    演示

      演示过程中,提示另外一个进程xxxx的,是因为NPOI读取Excel的时候,Excel不可以打开,我们关闭,然后再次执行即可

    项目下载

    链接:https://pan.baidu.com/s/1YLer2fgV6QhJIQVsxqozJQ 
    提取码:30a9 
  • 相关阅读:
    浅谈值对象
    循环一个节点列表(NodeList)或者数组,并且绑定事件处理函数引发对闭包的理解
    当前窗口和Iframe之间的相互访问(图片上传成功后立刻显示在当前页面上)
    网页动态加载图片 通过JS和jquery实现。
    javascript拖动层函数封装
    javascript中变量声明提升(Hoisting)
    运动框架必备的运动算法 留着用!
    CSS3特性之改变在浏览器上选中文字时,默认的背景颜色和文字颜色
    仿淘宝商品图片放大镜效果(鼠标移动上去会出现放大的图片,并且可以移动)
    与PHP交互中文编码的几个函数 decodeURIComponent,encodeURIComponent,encodeURI,decodeURI
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13300297.html
Copyright © 2011-2022 走看看