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();
}
}
}
使用NPOI读取Excel文件
最新推荐文章于 2024-04-11 13:59:26 发布