NPOI读取excel

 protected void Page_Load(object sender, EventArgs e)
        {
            //1.读取excel数据,存入list中
            List<User> list = new List<User>();
            //读取文件
            using (FileStream stream = new FileStream(@"C:\Users\zewei.cao\Desktop\UserInfo.xlsx", FileMode.Open))
            {
                //创建workbook
                //HSSFWorkbook workbook = new HSSFWorkbook(stream);
                XSSFWorkbook workbook = new XSSFWorkbook(stream);
                //读取sheet
                NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
                //读取数据
                int rowIndex = 1;
                NPOI.SS.UserModel.IRow row = sheet.GetRow(rowIndex++);

                while(row != null)
                {
                    //读取一行中的对象
                    User u = new User();

                    if (row.GetCell(0) != null)
                    {
                        u.id = (int)row.GetCell(0).NumericCellValue;
                    }
                    if (row.GetCell(1) != null)
                    {
                        row.GetCell(1).SetCellType(NPOI.SS.UserModel.CellType.String);
                        u.name = row.GetCell(1).StringCellValue;
                    }

                    if (row.GetCell(2) != null)
                    {
                        u.age = (int)row.GetCell(2).NumericCellValue;
                    }
                    if (row.GetCell(3) != null)
                    {
                        row.GetCell(3).SetCellType(NPOI.SS.UserModel.CellType.String);
                        u.gender = row.GetCell(3).StringCellValue;
                    }

                    if (row.GetCell(4) != null)
                    {
                        row.GetCell(4).SetCellType(NPOI.SS.UserModel.CellType.String);
                        u.nationality = row.GetCell(4).StringCellValue;
                    }

                    if (row.GetCell(5) != null)
                    {
                        row.GetCell(5).SetCellType(NPOI.SS.UserModel.CellType.String);
                        u.phone = row.GetCell(5).StringCellValue;
                    }

                    if (row.GetCell(6) != null)
                    {
                        row.GetCell(6).SetCellType(NPOI.SS.UserModel.CellType.String);
                        u.address = row.GetCell(6).StringCellValue;
                    }
                    list.Add(u);
                    row = sheet.GetRow(rowIndex++);
                }

                StringBuilder s = new StringBuilder();
                foreach(User u in list)
                {
                    s.Append(u.id + "--" + u.name + "--" + u.age + "--" + u.gender + "--" + u.nationality + "--" + u.phone + "--" + u.address + ";");
                }

                Response.Write(s);
            }
        }

 

using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值