使用NPOI读取Excel文件

来源:互联网 发布:运动会班服创意知乎 编辑:程序博客网 时间:2024/05/17 09:20
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Office.Interop.Excel;using NPOI.HSSF.UserModel;using System.IO;namespace NPOIDemo{    class Program    {        static void Main(string[] args)        {            //读取Excel文件            using (Stream s = File.OpenRead("11.xls"))            {                HSSFWorkbook work = new HSSFWorkbook(s);//获取表                HSSFSheet sheet = work.GetSheetAt(0);//获取页                int rowNum = sheet.LastRowNum;//获取有多少行                StringBuilder sb = new StringBuilder();                for (int i = 0; i <= rowNum; i++)//小于等于                {                    HSSFRow row = sheet.GetRow(i);                    string str = "";                    for (int j = 0; j < row.LastCellNum; j++)//小于                    {                        HSSFCell cell = row.GetCell(j);                        //判断这个列是不是数字类型                        if (cell.CellType == HSSFCell.CELL_TYPE_NUMERIC)                        {                            str = cell.NumericCellValue + " ";                        }                        else if (cell.CellType == HSSFCell.CELL_TYPE_STRING)                        {                            str = cell.StringCellValue + " ";                        }                        sb.Append(str + " ");                    }//end for                }//end for                Console.WriteLine(sb.ToString());            }            Console.ReadKey();        }    }}

原创粉丝点击