.NET C# 读写Excel及转换DataTable

.NET C# 读写Excel及转换DataTable

1. 依赖库

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

2. Nuget包与版本

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

3. ExcelUtil

3.1 Excel sheet 转 DataTable

/// <summary>
/// Excel sheet 转 DataTable
/// </summary>
/// <param name="excelFilePath">Excel文件路径</param>
/// <param name="sheetName">Sheet名称</param>
/// <returns>结果DataTable</returns>
public static DataTable? FromExcel(string excelFilePath, string sheetName)
{
    DataTable dataTable = new DataTable(sheetName);
    IWorkbook wb = GetWorkbook(excelFilePath);
    if (wb == null)
    {
        return null;
    }
    ISheet ws = wb.GetSheet(sheetName);
    if (ws == null)
    {
        return null;
    }
    if (ws.LastRowNum < 1)
    {
        return dataTable;
    }
    int maxColumnNum = 0;
    int rowNum = ws.LastRowNum + 1;
    for (int rowIdx = 0; rowIdx < rowNum; rowIdx++)
    {
        IRow row = ws.GetRow(0);
        if (row != null && row.LastCellNum > maxColumnNum)
        {
            maxColumnNum = row.LastCellNum;
        }
    }

    IRow headerRow = ws.GetRow(0);

    for (int columnIdx = 0; columnIdx < maxColumnNum; columnIdx++)
    {
        string columnName = string.Empty;
        if (headerRow != null)
        {
            ICell cell = headerRow.GetCell(columnIdx);
            if (cell == null)
            {
                continue;
            }
            columnName = cell.StringCellValue;
        }
        if (string.IsNullOrEmpty(columnName))
        {
            columnName = $"column_{columnIdx + 1}";
        }
        string columnTempName = columnName;
        int tag = 0;
        while (dataTable.Columns.Contains(columnTempName))
        {
            columnTempName = columnName + $"_{++tag}";
        }
        dataTable.Columns.Add(columnTempName);
    }

    if (rowNum <= 1)
    {
        return dataTable;
    }
    for (int rowIdx = 1; rowIdx < rowNum; rowIdx++)
    {
        DataRow dataRow = dataTable.NewRow();
        for (int columnIdx = 0; columnIdx < maxColumnNum; columnIdx++)
        {
            IRow row = ws.GetRow(rowIdx);
            if (row == null)
            {
                continue;
            }
            ICell cell = row.GetCell(columnIdx);
            if (cell == null)
            {
                continue;
            }
            dataRow[columnIdx] = GetCellValue(cell);
        }
        dataTable.Rows.Add(dataRow);
    }
    return dataTable;
}

3.2 Excel sheet 转 DataSet

/// <summary>
/// Excel sheet 转 DataSet
/// </summary>
/// <param name="excelFilePath">Excel文件路径</param>
/// <returns>结果DataSet</returns>
public static DataSet? FromExcel(string excelFilePath)
{
    IWorkbook wb = GetWorkbook(excelFilePath);
    if (wb == null)
    {
        return null;
    }
    DataSet ds = new DataSet();
    for (int i = 0; i < wb.NumberOfSheets; i++)
    {
        ISheet sheet = wb.GetSheetAt(i);
        DataTable? dt = FromExcel(excelFilePath, sheet.SheetName);
        if (dt == null)
        {
            continue;
        }
        ds.Tables.Add(dt);
    }
    return ds;
}

3.3 DataTable 转 Excel sheet

/// <summary>
/// DataTable 转 Excel sheet
/// </summary>
/// <param name="excelFilePath">Excel文件路径</param>
/// <param name="dataTable">DataTable实例</param>
/// <param name="sheetName">Sheet名称</param>
/// <returns>转换结果</returns>
public static bool ToExcel(string excelFilePath, DataTable dataTable, string sheetName = "")
{
    IWorkbook wb = GetWorkbook(excelFilePath);
    if (wb == null)
    {
        return false;
    }
    if (string.IsNullOrEmpty(sheetName))
    {
        if (string.IsNullOrEmpty(dataTable.TableName))
        {
            sheetName = "Sheet";
        }
        else
        {
            sheetName = dataTable.TableName;
        }
    }

    int numberOfSheets = wb.NumberOfSheets;
    if (numberOfSheets > 0)
    {
        List<string> sheetNames = new List<string>();
        for (int sheetIdx = 0; sheetIdx < numberOfSheets; sheetIdx++)
        {
            sheetNames.Add(wb.GetSheetName(sheetIdx).ToLower());
        }
        int tag = 0;
        string sheetTempName = sheetName;
        while (sheetNames.Contains(sheetTempName.ToLower()))
        {
            sheetTempName = $"{sheetName}_{++tag}";
        }
        sheetName = sheetTempName;
    }

    ISheet ws = wb.CreateSheet(sheetName);
    IRow headerRow = ws.CreateRow(0);
    for (int columnIdx = 0; columnIdx < dataTable.Columns.Count; columnIdx++)
    {
        string columnName = dataTable.Columns[columnIdx].ColumnName;
        ICell newCell = headerRow.CreateCell(columnIdx);
        newCell.SetCellValue(columnName);
    }

    for (int rowIdx = 0; rowIdx < dataTable.Rows.Count; rowIdx++)
    {
        IRow row = ws.CreateRow(rowIdx + 1);
        for (int columnIdx = 0; columnIdx < dataTable.Columns.Count; columnIdx++)
        {
            object value = dataTable.Rows[rowIdx][columnIdx];
            string cellStringValue = value?.ToString() ?? string.Empty;
            ICell cell = row.CreateCell(columnIdx);
            cell.SetCellValue(cellStringValue);
        }
    }

    FileStream fs = File.OpenWrite(excelFilePath);
    try
    {
        wb.Write(fs, false);
        return true;
    }
    catch (Exception ex)
    {
        // 异常处理...
        return false;
    }
    finally 
    {
        try { fs?.Close(); } catch { } 
    }
}

3.4 DataSet 转 Excel

/// <summary>
/// DataSet 转 Excel
/// </summary>
/// <param name="excelFilePath">Excel文件路径</param>
/// <param name="dataSet">DataSet实例</param>
/// <returns>转换结果</returns>
public static bool ToExcel(string excelFilePath, DataSet dataSet)
{
    bool allSuccess = true;
    foreach (DataTable dataTable in dataSet.Tables)
    {
        bool success = ToExcel(excelFilePath, dataTable);
        if (!success)
        {
            allSuccess = false;
        }
    }
    return allSuccess;
}

3.5 私有方法

private static IWorkbook GetWorkbook(string excelFilePath)
{
    string extension = Path.GetExtension(excelFilePath);
    IWorkbook wb = null;
    FileStream fs = null;
    try
    {
        if (!File.Exists(excelFilePath))
        {
            if (extension == ".xlsx" || extension == "xlsx")
                wb = new XSSFWorkbook();
            else if (extension == ".xls" || extension == "xls")
                wb = new HSSFWorkbook();
            else
            {
                AppLogger.Instance.Error($"错误文件类型{extension}!");
                return null;
            }
        }
        else
        {
            fs = File.Open(excelFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            if (extension == ".xlsx" || extension == "xlsx")
                wb = new XSSFWorkbook(fs);
            else if (extension == ".xls" || extension == "xls")
                wb = new HSSFWorkbook(fs);
            else
            {
                AppLogger.Instance.Error($"错误文件类型{extension}!");
                return null;
            }
        }
        return wb;
    }
    catch (Exception ex)
    {
        AppLogger.Instance.Error("读取Excel文件失败!", ex);
        return null;
    }
    finally { if (fs != null) try { fs.Close(); } catch { } }
}
static object? GetCellValue(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;
    }
}

cell.NumericCellValue;
case CellType.String: //STRING:
return cell.StringCellValue;
case CellType.Error: //ERROR:
return cell.ErrorCellValue;
case CellType.Formula: //FORMULA:
default:
return “=” + cell.CellFormula;
}
}




  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是 C# CSV 文件并使用 `DataTable` 相互转换的示例代码: ```csharp using System; using System.Data; using System.IO; class Program { static void Main(string[] args) { // 取 CSV 文件到 DataTable DataTable dataTable = new DataTable(); using (var reader = new StreamReader(@"C:\data.csv")) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); // 第一行数据作为列名 if (dataTable.Columns.Count == 0) { foreach (var value in values) { dataTable.Columns.Add(value.Trim()); } } else { // 处理每行数据 DataRow row = dataTable.NewRow(); for (int i = 0; i < values.Length; i++) { row[i] = values[i].Trim(); } dataTable.Rows.Add(row); } } } // 将 DataTable 入 CSV 文件 using (var writer = new StreamWriter(@"C:\output.csv")) { // 入列名 for (int i = 0; i < dataTable.Columns.Count; i++) { writer.Write(dataTable.Columns[i]); if (i < dataTable.Columns.Count - 1) { writer.Write(","); } } writer.WriteLine(); // 入数据 foreach (DataRow row in dataTable.Rows) { for (int i = 0; i < dataTable.Columns.Count; i++) { writer.Write(row[i].ToString()); if (i < dataTable.Columns.Count - 1) { writer.Write(","); } } writer.WriteLine(); } } Console.WriteLine("CSV 文件取和入完成!"); } } ``` 在这个示例代码中,我们使用 `StreamReader` 类来取指定路径下的 CSV 文件,使用 `DataTable` 类来保存取到的数据,同时将其转换为 `DataTable` 对象。我们遍历取到的每行数据,并将第一行数据作为列名,将每行数据添加到 `DataTable` 中。接着我们使用 `StreamWriter` 类来创建一个新的 CSV 文件,并将 `DataTable` 对象中的数据入到文件中。最后,程序输出一条消息表示 CSV 文件操作完成。 如果需要在程序中使用 `DataTable` 对象进行数据处理,可以使用以下代码将 `DataTable` 转换为 CSV 文件: ```csharp // 将 DataTable 入 CSV 文件 using (var writer = new StreamWriter(@"C:\output.csv")) { // 入列名 for (int i = 0; i < dataTable.Columns.Count; i++) { writer.Write(dataTable.Columns[i]); if (i < dataTable.Columns.Count - 1) { writer.Write(","); } } writer.WriteLine(); // 入数据 foreach (DataRow row in dataTable.Rows) { for (int i = 0; i < dataTable.Columns.Count; i++) { writer.Write(row[i].ToString()); if (i < dataTable.Columns.Count - 1) { writer.Write(","); } } writer.WriteLine(); } } ``` 这段代码将 `DataTable` 的列名和每行数据入到 CSV 文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值