Net Core教程

C# Excel To DataTable

本文主要是介绍C# Excel To DataTable,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

要引用NPOI,引用方法:项目引用那儿右键 => 管理NuGet程序包 => 游览 =>输入NPOI =>选中NPOI后安装(一般是第一个)

      

        /// <summary>
        /// Excel转DataTable
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="sheetName">sheet名</param>
        /// <param name="FirstRowIndex">第一行索引</param>
        /// <param name="isFirstRowColumn">是否第一行列</param>
        /// <returns></returns>
        public System.Data.DataTable ExcelToDataTable(string fileName, string sheetName,int FirstRowIndex, bool isFirstRowColumn)
        {
            int startRow = 0;
            ISheet sheet = null;
            IWorkbook workbook = null;
            FileStream fs = null;
            
            System.Data.DataTable data = new System.Data.DataTable();
            //int startRow = 0;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook(fs);
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook(fs);
                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }
                
                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(FirstRowIndex);  //表头第几行开始
                    int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
                    startRow = FirstRowIndex + 1;
                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                cell.SetCellType(CellType.String);//Cell.CELL_TYPE_STRING);
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        //startRow = sheet.FirstRowNum + 1;
                    }
                    else
                    {
                      //  startRow = sheet.FirstRowNum;
                    }

                    //最后一列的标号
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue; //没有数据的行默认是null

                        if (row.Cells.Count > 2)
                        {
                            if (row.GetCell(0) != null&& row.GetCell(1) != null)
                            {
                                if (row.GetCell(0).ToString() == ""&& row.GetCell(1).ToString() == "")
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }

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

                                if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell))
                                    dataRow[j] = cell.DateCellValue.ToString("yyyy-MM-dd");
                                else
                                {
                                    dataRow[j] = row.GetCell(j).ToString();
                                }
                            }
                              
                        }
                        data.Rows.Add(dataRow);
                    }
                }
                return data;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
                return null;
            }
        }

这篇关于C# Excel To DataTable的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!