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;

 

posted @ 2019-10-08 13:33  highlightyys  阅读(31)  评论(0)    收藏  举报