【问题标题】:How to copy data from one column to another at the same datatable in c#如何在c#中的同一数据表中将数据从一列复制到另一列
【发布时间】:2017-03-30 12:48:06
【问题描述】:

我有一个数据表,其中包含来自 excel 文件的信息。我有四个以上的专栏,但举个例子,我只写了其中的四个。我必须编写一个程序,如果 C 列中单元格的值为 0,那么我必须将 B 列复制到 A 列。如果 C 列中单元格的值> 0,那么我必须复制B 列到 A 并且应该添加另一行,我必须将 C 列的值复制到 A。

我现在拥有的是

    for (int r = 2; r <= ws.UsedRange.Rows.Count; r++)
    {    if (ws.UsedRange.Cells[r, 3].Text == "0")
            {
                DataRow row = dt.NewRow();

                for (int c = 1; c < ws.UsedRange.Columns.Count; c++)
            {
                string cell = ws.Cells[r, c].Text;
                row[c - 1] = cell;     
          }
     }

所以我的问题是:

如何将一列复制到同一数据表中的另一列?将 B 复制到 A。 如何添加另一行并将 C 的值复制到 A 仅针对该行?

这里是完整的代码:

    public DataTable ReadExcel2(string file)
    {
        ExcelI.Application app = new ExcelI.Application(); //create an excel instance
        ExcelI.Workbook wb = app.Workbooks.Open(file, ReadOnly: true); //open a file
        ExcelI.Worksheet ws = wb.Worksheets[1]; //choose a sheet. The firt one

        var rng = ws.UsedRange; 
        //takes the index of the columns that are going to be filtered
        int service = ColumnIndexByName(ws.Cells[1, 1].EntireRow, "Service");
        int status = ColumnIndexByName(ws.Cells[1, 1].EntireRow, "Status");
        int code = ColumnIndexByName(ws.Cells[1, 1].EntireRow, "Code");
        DataTable dt = new DataTable();
        dt.Columns.Add("A", typeof(string));
        for (int c = 1; c < ws.UsedRange.Columns.Count; c++)
        {
            string colName = ws.Cells[1, c].Text;
            int i = 2;
            while (dt.Columns.Contains(colName))
            {
                colName = ws.Cells[1, c].Text + "{" + i.ToString() + "}";
                i++;
            }
            dt.Columns.Add(colName);
        }
        //do a loop to delete the rows that we dont need
        for (int r = 2; r <= ws.UsedRange.Rows.Count; r++)
        {
                if (ws.UsedRange.Cells[r, 3].Text == "0")
                {
                    DataRow row = dt.NewRow();

                    for (int c = 1; c < ws.UsedRange.Columns.Count; c++)
                    {
                        string cell = ws.Cells[r, c].Text;
                        row[c - 1] = cell;
                    }
                    dt.Rows.Add(row);
                    row["A"] = row["C"];
                }
        }            


//Close the file
        wb.Close();
        //release the excel objects from use
        Marshal.ReleaseComObject(wb);
        Marshal.ReleaseComObject(ws);
        //take the id of excel process
        int pid = app.PID();
        app.Quit();
        StartProc("taskkill", $"/f /pid {pid}");
        return dt;
    }

【问题讨论】:

    标签: c# excel datatable


    【解决方案1】:

    要添加行使用dt.Rows.Add(row);,关于“将B列复制到A”,您的意思是复制值,只需分配row[0] = row[2];,顺便说一下,您的示例缺少括号。

    我认为你应该根据问题中的条件来检查你的代码,你也可以自己做。只需注意您在问题中编写的条件和您在代码中检查的条件运算符即可。

    【讨论】:

    • 谢谢。感谢您的建议,我查看了我的代码并意识到了我的问题。
    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2016-03-15
    • 1970-01-01
    相关资源
    最近更新 更多