【问题标题】:Get values from dataset without column name从没有列名的数据集中获取值
【发布时间】:2014-05-22 04:22:27
【问题描述】:

我有一个用这个 excelsheet 填充的数据集:

方法:

[HttpPost]
public ActionResult ShowExcelFile(GetExcel model)
{
    DataSet result = null;
    var file = model.Files[0];
    if (file != null && file.ContentLength > 0)
    {
        // .xlsx
        IExcelDataReader reader = ExcelReaderFactory.CreateOpenXmlReader(file.InputStream);

        reader.IsFirstRowAsColumnNames = true; // if your first row contains column names
        result = reader.AsDataSet();
        reader.Close();
    }

    for (int i = 1; i < result.Tables[0].Rows.Count; i++)
    {
        DataRow data = result.Tables[0].Rows[i];
        System.Diagnostics.Debug.Write(data.Table.Rows[i]["Column header name"]);
    }
    return View("ShowExcelFile");
}

如果我用“摊销”替换“列名”,我会在 F 列中获得所有值。但我想要的值在 K 列中。如果我在 K1 中写一个“列标题名称”,我将以这种方式获得值.但是有什么方法可以不用这个吗?我不想在该列中使用值(对我来说大多数设计问题)。

所以只是为了澄清一下:如何使用列标题名称获取单元格 K5:K10 中的值?

【问题讨论】:

    标签: c# asp.net-mvc excel dataset


    【解决方案1】:

    你能用索引代替列名吗?行[i][列索引]

    如果您只能使用索引,请尝试使用 Rows[4][10]

    result = reader.AsDataSet();
    string CellValue=result.Tables[0].Rows[4][10];
    reader.Close();
    

    【讨论】:

    • 哦,一定是我早上的咖啡还没喝完。很简单。谢谢!
    猜你喜欢
    • 2011-11-01
    • 2015-01-11
    • 2015-03-19
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多