C# 中使用 ClosedXML,第一行被视为标题行。

huangapple go评论68阅读模式
英文:

C# with ClosedXML, First row is Treated as Header row

问题

我试图打印A1单元格,应该打印'Tomer',但出于某种原因它打印'Noam'。我是否错误地使用了ClosedXML?还是有其他方法可以将所有数据获取到DataTable中,以便我可以使用正确的单元格地址获取值?

test.xlsx 结构:

C# 中使用 ClosedXML,第一行被视为标题行。

我构建了一个简单的C#控制台应用程序来演示我的观点。

程序:

using c__closedxml;
using ClosedXML.Excel;
using System.Data;

string path = Path.Combine(AppContext.BaseDirectory, "test.xlsx");

using (var wb = new XLWorkbook(path)) {

    var worksheet = wb.Worksheets.First();
    DataTable dt = worksheet.RangeUsed().AsTable().AsNativeDataTable();
    Console.WriteLine(Service.getValueByCell(dt, "A1"));

}

用于记录单元格内容的自定义函数:

public static string getValueByCell(DataTable dt, string cell) {
    string refLetter = "";
    string refNum = "";
    for (int i = 0; i < cell.Length; i++) {
        char curChar = cell[i];
        if (ValidateNumber(curChar)) {
            refNum += curChar;
        } else {
            refLetter += curChar;
        }
    }

    int col = GetNumberFromExcelColumn(refLetter);
    int row = Convert.ToInt32(refNum);

    Console.WriteLine($"在第 {col} 列按行 {row} 搜索");
    Console.WriteLine(JsonConvert.SerializeObject(dt.Rows[0].Table)); 

    return dt.Rows[row - 1][col - 1].ToString();
}

DataTable中的表确认第一行被视为标题:

Console.WriteLine(JsonConvert.SerializeObject(dt.Rows[0].Table)); // 打印如下内容:

[
    {
        "Tomer": "Noam",
        "Cohen": "Aharoni",
        "31": 33.0
    }
]

感谢任何帮助!
1: https://i.stack.imgur.com/gOGgI.png

英文:

I'm trying to print A1 cell, it should print 'Tomer' but for some reason it prints 'Noam'.
Am I using the ClosedXML wrong? Or is there a different way in which I can get all data to DataTable as I can get the value with the correct cell address?

test.xlsx structure:

C# 中使用 ClosedXML,第一行被视为标题行。

I've built a simple minimal C# console app demonstrating my point.

Program:

using c__closedxml;
using ClosedXML.Excel;
using System.Data;

string path = Path.Combine(AppContext.BaseDirectory, &quot;test.xlsx&quot;);

using (var wb = new XLWorkbook(path)) {

    var worksheet = wb.Worksheets.First();
    DataTable dt = worksheet.RangeUsed().AsTable().AsNativeDataTable();
    Console.WriteLine(Service.getValueByCell(dt, &quot;A1&quot;));

}

Custom function to log the cell content:

public static string getValueByCell(DataTable dt, string cell) {
    string refLetter = &quot;&quot;;
    string refNum = &quot;&quot;;
    for (int i = 0; i &lt; cell.Length; i++) {
        char curChar = cell[i];
        if (ValidateNumber(curChar)) {
            refNum += curChar;
        } else {
            refLetter += curChar;
        }
    }

    int col = GetNumberFromExcelColumn(refLetter);
    int row = Convert.ToInt32(refNum);

    Console.WriteLine($&quot;Searching for column {col} by row {row}&quot;);
    Console.WriteLine(JsonConvert.SerializeObject(dt.Rows[0].Table)); 

    return dt.Rows[row - 1][col - 1].ToString();
}

The table inside DataTable confirms that the first row is treated as a header:

Console.WriteLine(JsonConvert.SerializeObject(dt.Rows[0].Table)); // prints the following:

[
    {
        &quot;Tomer&quot;: &quot;Noam&quot;,
        &quot;Cohen&quot;: &quot;Aharoni&quot;,
        &quot;31&quot;: 33.0
    }
]

Any help is appreciated!

答案1

得分: 1

AsTable() 使用您范围的第一行作为列标题创建一个Excel表格。这类似于在Excel中按下Ctrl+T。AsNativeDataTable() 将其转换为一个 System.Data.DataTable 实例。其余的行填充了数据。

要么首先插入实际的标题,要么使用不同的方法,避免使用这2个转换方法来引用数据。

英文:

AsTable() creates an Excel table using the first row of your range as column headers. This is like pressing Ctrl+T in Excel. AsNativeDataTable() converts that to a System.Data.DataTable instance. The rest of the rows populate the data.

Either first insert actual headers or use a different approach, avoiding the 2 conversion methods, to reference the data.

huangapple
  • 本文由 发表于 2023年6月25日 22:42:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定