读取ecxel中数据——NPOI.Excel和Aspose

1、使用  Aspose.Cells.dll

  using Aspose.Cells;   命名空间

  public static DataTable AsposeReadExcel(string path)
        {
            Workbook workbook1 = new Workbook(path);
            Worksheet sourceSheet1 = workbook1.Worksheets[0];
            Cells cells1 = sourceSheet1.Cells;
            DataTable DtExcel = cells1.ExportDataTableAsString(0, 0, cells1.MaxDataRow + 1, cells1.MaxDataColumn + 1, false);//起始行、起始列,结束行,结束列,是否有表头
            return DtExcel;
        }

2、在NuGet中导入NPOI.Excel管理器

(非原创不记得从哪个博主处看到的了,所以没有写转载地址)

 /// </summary>
        /// 读取Excel,放入DataTable中
        /// <param name="fileName">文件路劲</param>
        /// <param name="sheetName">表名</param>
        /// <param name="isFirstRowColumn">是否有表头</param>
        /// <returns></returns>
public static DataTable ReadExcelToDataTable(string fileName, string sheetName = null, bool isFirstRowColumn = false)
        {
            //定义要返回的datatable对象
            DataTable data = new DataTable();
            NPOI.SS.UserModel.ISheet sheet = null;
            //数据开始行(排除标题行)
            int startRow = 0;

            if (!File.Exists(fileName))
            {
                return null;
            }
            //根据指定路径读取文件
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs);
            //IWorkbook workbook = new HSSFWorkbook(fs);
            //如果有指定工作表名称
            if (!string.IsNullOrEmpty(sheetName))
            {
                sheet = workbook.GetSheet(sheetName);
                //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                if (sheet == null)
                {
                    sheet = workbook.GetSheetAt(0);
                }
            }
            else
            {
                //如果没有指定的sheetName,则尝试获取第一个sheet
                sheet = workbook.GetSheetAt(0);
            }
            if (sheet != null)
            {
                NPOI.SS.UserModel.IRow firstRow = sheet.GetRow(0);
                //一行最后一个cell的编号 即总的列数
                int cellCount = firstRow.LastCellNum;
                //如果第一行是标题列名

                for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                {
                    NPOI.SS.UserModel.ICell cell = firstRow.GetCell(i);
                    if (cell != null)             //遍历Excel表
                    {

                        //     string cellValue = cell.StringCellValue;
                        if (cell != null)
                        {
                            DataColumn column = new DataColumn(cell.ToString());
                            data.Columns.Add(column);
                        }
                    }
                }
                if (isFirstRowColumn)
                {
                    startRow = sheet.FirstRowNum + 1;      //有表头时不读取表头
                }
                else
                {
                    startRow = sheet.FirstRowNum;
                }
                //最后一列的标号
                int rowCount = sheet.LastRowNum;
                for (int i = startRow; i <= rowCount; ++i)
                {
                    NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
                    if (row == null) continue; //没有数据的行默认是null       

                    DataRow dataRow = data.NewRow();
                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                    {
                        if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                            dataRow[j] = row.GetCell(j).ToString();
                    }
                    data.Rows.Add(dataRow);
                }
            }
            return data;

        }

 

posted @ 2020-03-13 19:41  越吃越瘦  阅读(516)  评论(0编辑  收藏  举报