如何在C#中动态添加(或)将值插入到数据列中?[英] How to dynamically add (or) insert values to data column in c#?

本文是小编为大家收集整理的关于如何在C#中动态添加(或)将值插入到数据列中?的处理方法,想解了如何在C#中动态添加(或)将值插入到数据列中?的问题怎么解决?如何在C#中动态添加(或)将值插入到数据列中?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在通过单元格迭代Excel文件.每个文件都有自己的列数.基于Excel细胞计数,我们正在动态创建数据表的列.这部分工作正常.

我将必须将每个单元格插入数据列.如何在C#中的数据列中动态添加(或)插入值

在一个假设中,Excel文件有2行和3个colums

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
    }
}

推荐答案

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

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++;
}

其他推荐答案

在这里,我认为您的列已经添加了.您可以尝试一下.

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);
                     }
}

其他推荐答案

由于您不能使用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 ++;
}

本文地址:https://www.itbaoku.cn/post/1794035.html