c#读取excel

xiaoxiao2021-02-28  35

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.XSSF.UserModel; using System.IO; namespace readerexcel { class Program { static void Main(string[] args) { IWorkbook workbook = null; //新建IWorkbook对象 string fileName = "G:\\规划.xlsx"; FileStream fileStream = new FileStream(@"G:\\规划.xlsx", FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 { workbook = new XSSFWorkbook(fileStream); //xlsx数据读入workbook } else if (fileName.IndexOf(".xls") > 0) // 2003版本 { workbook = new HSSFWorkbook(fileStream); //xls数据读入workbook } ISheet sheet = workbook.GetSheetAt(0); //获取第一个工作表 IRow row;// = sheet.GetRow(0); //新建当前工作表行数据 //row = sheet.GetRow(1); //for (int j = 0; j < row.LastCellNum; j++) //{ // string cellValue = row.GetCell(j).ToString(); //获取i行j列数据 // Console.WriteLine(cellValue); //} //row = sheet.GetRow(3); //for (int j = 0; j < row.LastCellNum; j++) //{ // string cellValue = row.GetCell(j).ToString(); //获取i行j列数据 // Console.WriteLine(cellValue); //} //for (int i = 5; i <= sheet.LastRowNum; i++) //对工作表每一行 //{ // int k = sheet.LastRowNum; // Console.WriteLine(k); // row = sheet.GetRow(i); //row读入第i行数据 // if (row != null) // { // for (int j = 0; j < row.LastCellNum; j++) //对工作表每一列 // { // string cellValue = row.GetCell(j).ToString(); //获取i行j列数据 // Console.WriteLine(cellValue); // } // } //} Console.ReadLine(); fileStream.Close(); } } }
转载请注明原文地址: https://www.6miu.com/read-2614788.html

最新回复(0)