方法步奏:
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>");
}