【问题标题】:How to dynamically add (or) insert values to data column in c#?如何在 C# 中动态地向数据列添加(或)插入值?
【发布时间】:2019-06-18 19:30:57
【问题描述】:

我正在逐个单元格地迭代一个 excel 文件。每个文件都有自己的列数。基于 excel 单元格计数,我们正在动态创建数据表的列。这部分工作正常。

我必须将每个单元格值插入数据列。如何在c#中动态添加(或)插入值到数据列?

假设excel文件有2行3列

FirstName LastName Location
---------------------------
Albert     B         Miami
Jackson    C         Tampa

我必须用这些单元格值填充数据表/数据列。 Foreach 循环迭代工作正常并获取每个单元格值。 我坚持将每个单元格值插入数据列/数据行。

int iCellCount = 3;  // In this example, i am defining cell count = 3 as static value. In the real time, cell count is picked from the excel file.
var cellValue = string.Empty;
DataTable dt = new DataTable();
foreach (ExcelReportCell excelCell in excelRow) // Iterating the excel cell row by row in the Excel Sheet
{
    cellValue = excelCell.GetText().ToString(); // cellValue is assigned dynamically through excel file cell by cell iteration logic
    for (int i = 1; i <= iCellCount; i++)
    {
        dt.Columns.Add();
        // Expected to assign each column values
    }
}

【问题讨论】:

  • 当您在代码中说// Expected to assign each column values 时,您的意思是您希望DataTable 中有多个行来代表您的Excel 文件吗?
  • DataRow dr = dt.NewRow(); 然后设置每个单元格的dr值,添加到表格中:dt.Rows.Add(dr);
  • 基于迭代,单元格值被分配给列。列在哪里分配给数据行
  • 嗯,首先你需要设置列。 然后,您设置行。

标签: c# datacolumn


【解决方案1】:

您需要添加所有列(使您的方法独立于列名,因此没有硬编码字符串),然后添加所有相应的值。

DataTable dt = new DataTable();
List<string> colList = new List<string>();

// Loop through column collection 
// Add all your columns to data table first
foreach (ExcelReportColumn eachColumn in excelColumn)
{
    dt.Columns.Add(eachColumn, typeof(string));
    colList.Add(eachColumn);
}

DataRow newRow;
int currentCol = 0;
// Then add your data rows
foreach (ExcelReportCell excelCell in excelRow)
{
    newRow = dt.NewRow();
    // Magic Method: You need to know the column name for the the current cell
    string columnName = colList[currentCol]; 
    newRow[columnName] = excelCell;
    dt.Rows.Add(newRow);
    currentCol++;
}

【讨论】:

  • ,谢谢……我同意你的看法……我们需要在分配记录之前添加所有列
  • 如果您对此答案有解决方案,请将其标记为已回答。如果您需要更多信息/帮助,请开火。
  • 列名未映射到数据行,因此,我无法获取数据中的列.. //缺少添加到行的列。 dt.Rows.Add(newRow);
  • 不确定您的意思,但想法是在数组中索引列名的严格顺序。并在插入行时使用它。
  • 稍微更改了答案,以便更容易理解我在上面评论中的意思。注意 - 使用Infragistics.dll 可能有更简单的方法来执行此操作,可能需要阅读他们的文档。但在 C# 中,本答案中提到的方式应该可以处理它。
【解决方案2】:

在这里,我假设您的列已经添加。你可以试试这个。

int iCellCount = 3;  // In this example, i am defining cell count = 3 as static value. In the real time, cell count is picked from the excel file.
var cellValue = string.Empty;
DataTable dt = new DataTable();
DataRow row;  //Create a DataRow
foreach (ExcelReportCell excelCell in excelRow) // Iterating the excel cell row by row in the Excel Sheet
{
                    cellValue = excelCell.GetText().ToString(); // cellValue is assigned dynamically through excel file cell by cell iteration logic
                    for (int i = 1; i <= iCellCount; i++)
                    {
                        row = dt.NewRow();
                        row["Your_Column_Name_Here"] = cellValue;
                        dt.Rows.Add(row);
                     }
}

【讨论】:

  • 谢谢Cujoey,我正在研究语法。 .. 感谢那 ..!!你的语法和 alen 的语法有一些相似之处.. 有帮助
【解决方案3】:

既然你不能使用 linq,那么这里有另一个解决方案

首先,您必须拥有 excel 中的行列表。所以你要做的第一件事就是生成excel文件中第一行的列

var rowIndex =0;
forech (var row in excelrows){
DataRow dtRow= null;
var cellIndex = 0;
foreach (ExcelReportCell cell in row){
if (rowIndex ==0) // generate the columns
{
dt.Columns.Add(new ColumnData(cell.GetText().ToString()));
}else { // the columns has already been generated then generate the row
 dtRow = dtRow?? dt.NewRow();
 dtRow[cellIndex] = cell.GetText(); 
 }
cellIndex++;
}
if (dtRow != null)
  dt.Rows.Add(dtRow);

rowIndex ++;
}

【讨论】:

  • 谢谢..我正在使用现有的代码逻辑逐个单元地迭代。 Excel 组件来自 Infragistics dll。我想,我们可能无法在我的场景中使用 linq 查询 .. 我试过了,不能
  • 好的,我已经更新了我的答案,阅读并尝试理解这个概念。我在这里写了这段代码,所以当你在 vs 中传递它时可能会遇到一些错误。
  • 谢谢 alen,我正在研究语法。 ..谢谢你..!!
  • 如果它可以帮助您解决问题,请不要忘记将其标记为 asnwer
猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多