幽梦紫曦

使用NPOI插件读取Excel数据

电脑版发表于:2019/11/28 16:14

方法步奏:

                  1.导入NPOI插件和文件IO

                  2.获取文件路径

                  3.打开文件

                  4.通过文件夹打开表格

                  5.读取指定的表

                  6.通过for循环依次取出数据

示例:

          

             //获取文件路径

            string url = Server.MapPath("~/Excel/students.xls");

            //打开文件

            FileStream file = new FileStream(url, FileMode.Open);

            //通过文件夹打开表格

            HSSFWorkbook hssf = new HSSFWorkbook(file);

            //读取指定的表

            HSSFSheet sheet = hssf.GetSheet("Sheet1") as HSSFSheet;

            for (int i = 0; i < sheet.LastRowNum; i++)

            {

                HSSFRow row = sheet.GetRow(i) as HSSFRow;

                string num = "";

                if (row.GetCell(0).CellType==NPOI.SS.UserModel.CellType.STRING)

                {

                    num= row.GetCell(0).StringCellValue;

                }

                if (row.GetCell(0).CellType == NPOI.SS.UserModel.CellType.NUMERIC)

                {

                    num = row.GetCell(0).NumericCellValue+"";

                }

                string name= row.GetCell(1).StringCellValue;

                Response.Write(num + "   " + name + "<br>");

            }





                   

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢