如何将 Excel 转换为 DataTable 数据

/// 
/// Excel 帮助类
/// 
public static class ExcelHelper
{
    /// 
    /// 将 Excel 转换为 DataTable
    /// 
    /// 文件路径
    /// 表下标 从0开始
    /// 行下标 从1开始
    ///  是否排除excel中第一行
    /// DataTable
    public static DataTable ExcelToDataTable(string filePath, int sheetIndex, int rowIndex, bool header = true)
    {
        var ext = CheckExt(filePath);

        if (ext == "208207")//xls
            return ToDataTable2003(filePath, sheetIndex, rowIndex, header);

        if (ext == "8075")//xlsx
            return ToDataTable2007(filePath, sheetIndex, header);

        return new DataTable();
    }

    #region Private Method

    /// 
    /// 通过文件头信息判断文件类型
    /// 
    /// 
    /// 
    private static string CheckExt(string path)
    {
        var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        var r = new BinaryReader(fs);

        var bx = string.Empty;

        try
        {
            byte buffer = r.ReadByte();
            bx = buffer.ToString();
            buffer = r.ReadByte();
            bx += buffer.ToString();

            // 208207 xls
            // 8075 xlsx
        }
        catch (Exception)
        {
            //throw;
        }
        finally
        {
            r.Close();
            fs.Close();
        }
        return bx;
    }

    /// 
    /// 将 2003 Excel 转换为 DataTable
    /// 
    /// 文件路径
    /// 表下标 从0开始
    /// 行下标 从1开始
    ///  excel中第一行是否属于列
    /// DataTable
    private static DataTable ToDataTable2003(string filePath, int sheetIndex, int rowIndex, bool header)
    {
        var dt = new DataTable(Path.GetFileNameWithoutExtension(filePath) + "_Sheet" + sheetIndex);

        using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            var workbook = new HSSFWorkbook(file);
            var sheet = workbook.GetSheetAt(sheetIndex);

            sheet.SetColumnHidden(0, false);
            sheet.SetColumnHidden(1, false);
            sheet.SetColumnHidden(2, false);

            var rows = sheet.GetRowEnumerator();
            for (int i = 0; i < rowIndex; i++)
            {
                rows.MoveNext();
            }

            var row = (HSSFRow)rows.Current;

            for (var i = 0; i < row.LastCellNum; i++)
            {
                var columnName = header ? row.GetCell(i).StringCellValue : i.ToString();
                dt.Columns.Add(columnName, typeof(string));
            }

            if (!header)
            {
                var dataRow = dt.NewRow();
                for (var i = 0; i < row.LastCellNum; i++)
                {
                    var item = row.GetCell(i);

                    dataRow[i] = GetRow(item);
                }
                dt.Rows.Add(dataRow);
            }

            while (rows.MoveNext())
            {
                row = (HSSFRow)rows.Current;
                var dataRow = dt.NewRow();
                bool isNull = false;//是否空行
                for (var i = 0; i < row.LastCellNum; i++)
                {
                    var item = row.GetCell(i);
                    var _row = GetRow(item);
                    if (_row != null)
                    {
                        dataRow[i] = _row;
                        if (!string.IsNullOrWhiteSpace(dataRow[i].ToString()))
                        {
                            isNull = true;//设置非空
                        }
                    }
                }
                if (isNull)
                {
                    dt.Rows.Add(dataRow);
                }
            }
        }

        return dt;
    }

    /// 
    /// 将 2007 Excel 转换为 DataTable
    /// 
    /// 文件路径
    /// 表下标 从0开始
    ///  excel中第一行是否属于列
    /// DataTable
    private static DataTable ToDataTable2007(string filePath, int index, bool header)
    {
        var dt = new DataTable(Path.GetFileNameWithoutExtension(filePath) + "_Sheet" + index);

        using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            var workbook = new XSSFWorkbook(file);
            var sheet = workbook.GetSheetAt(index);

            var rows = sheet.GetRowEnumerator();
            rows.MoveNext();
            var row = (XSSFRow)rows.Current;

            for (var i = 0; i < row.LastCellNum; i++)
            {
                var columnName = header ? row.GetCell(i).StringCellValue : i.ToString();
                dt.Columns.Add(columnName, typeof(string));
            }

            if (!header)
            {
                var dataRow = dt.NewRow();
                for (var i = 0; i < row.LastCellNum; i++)
                {
                    var item = row.GetCell(i);

                    dataRow[i] = GetRow(item);
                }
                dt.Rows.Add(dataRow);
            }

            while (rows.MoveNext())
            {
                row = (XSSFRow)rows.Current;
                var dataRow = dt.NewRow();
                bool isNull = false;//是否空行
                for (var i = 0; i < row.LastCellNum; i++)
                {
                    var item = row.GetCell(i);
                    var _row = GetRow(item);
                    if (_row != null)
                    {
                        dataRow[i] = _row;
                        if (!string.IsNullOrWhiteSpace(dataRow[i].ToString()))
                        {
                            isNull = true;//设置非空
                        }
                    }
                }
                if (isNull)
                {
                    dt.Rows.Add(dataRow);
                }
            }
        }

        return dt;
    }

    /// 
    /// 
    /// 
    /// 
    /// 
    private static object GetRow(ICell cell)
    {
        if (cell == null)
            return null;
        switch (cell.CellType)
        {
            case CellType.Blank: //BLANK:
                return null;
            case CellType.Boolean: //BOOLEAN:
                return cell.BooleanCellValue;
            case CellType.Numeric: //NUMERIC:
                return cell.NumericCellValue;
            case CellType.String: //STRING:
                return cell.StringCellValue;
            case CellType.Error: //ERROR:
                return cell.ErrorCellValue;
            case CellType.Formula: //FORMULA:
            default:
                return "=" + cell.CellFormula;
        }
    }

    #endregion
}

调用时使用,内中默认处理了 xls 与 xlsx 类型的 Excel 格式,使两种格式可以兼容。

ExcelHelper.ExcelToDataTable(path, 0, 1);

 

你可能感兴趣的:(代码,使用心得,Excel,获取Excel数据,Excel数据转换)