简体   繁体   English

如何在C#中向数据列动态添加(或)插入值?

[英]How to dynamically add (or) insert values to data column in c#?

I am iterating an excel file cell by cell. 我正在逐个单元地迭代一个excel文件。 Each file has its own number of columns. 每个文件都有其自己的列数。 Based on excel cell count, we are dynamically creating the columns to the data table. 基于excel单元格计数,我们正在动态创建数据表的列。 This part is working fine. 这部分工作正常。

I will have to insert each cell value to the data column. 我将必须将每个单元格值插入数据列。 How to dynamically add (or) insert values to data column in c#? 如何在C#中向数据列动态添加(或)插入值?

In an assumption, excel file has 2 rows and 3 colums 假设一个excel文件有2行3列

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

I will have to populate the data table / data column with these cell values. 我将不得不使用这些单元格值填充数据表/数据列。 Foreach loop iteration is working fine and picking up each cell value. Foreach循环迭代工作正常,并获取每个单元格值。 I am stuck on inserting each cell value to the data column / data row. 我一直坚持将每个单元格值插入数据列/数据行。

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

Here I assume your columns are already added. 在这里,我假设您的列已添加。 You can try this. 你可以试试看

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

You need to add all the columns (make your method independent of the column name, so no hard coded strings) and then add all the respective 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++;
}

Since you cant use linq then here us another solution 既然您不能使用linq,那么我们这里提供另一个解决方案

First you must have a list of rows from the excel. 首先,您必须具有excel中的行列表。 so your first thing to do is generate the columns which is the first row in the excel file 所以您要做的第一件事就是生成列,这是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 ++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM