如何使用互操作在 xlsx 中获取 all/total 工作表?

how can i GET all/total worksheet in a xlsx by using interop?

我怎样才能把总共10个'ROW'的所有作品sheet(比如3个)组合成另一个新的sheet?问题:我得到的结果是循环 through/read 到最后一个 sheet/ NO.3,它只显示 10 行。无法在 30 行的 sheet 中显示总行的所有 sheet。

我尝试了什么:我尝试循环遍历 sheet 3 并循环遍历 第 10 行。

           for (int sheet = 1; sheet <= 3; sheet++)
         {
    xlworkSheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(sheet);

        //rw is equal to 10 rows
            for (int row = 0; row <= rw; row++)
            {


                //extract out excel value and store it into empID variable

var empID=(string)(xlworkSheet.Cells[row,1]as(Excel.Range).Value.ToString();

                //store empID into employeeobj               
                employeeobj.employeeID = empID;
   xlNewSheet2.Cells[row, 1].Value = employeeobj.employeeID;
             }
           }

我得到的:新 sheet 中最后 sheet 的 10 行。 我的目标:新 sheet.

中总 sheet 的 30 行

您可以创建包含所有行值的数据列表或使用计数变量。 希望对你有帮助,我的朋友:))

//方案一:使用计数变量

int k = 0;
for (int sheet = 1; sheet <= 3; sheet++)
{
    xlworkSheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(sheet);

    //rw is equal to 10 rows
    for (int row = 0; row <= rw; row++)
    {    

       //extract out excel value and store it into empID variable    
       var empID=(string)(xlworkSheet.Cells[row,1]as(Excel.Range).Value.ToString();

       //store empID into employeeobj               
       employeeobj.employeeID = empID;

       xlNewSheet2.Cells[k, 1].Value = employeeobj.employeeID;
       k++;
     }
}

//方案二:使用列表

List<string> data = new List<string>();
for (int sheet = 1; sheet <= 3; sheet++)
{
    xlworkSheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(sheet);

        //rw is equal to 10 rows
     for (int row = 0; row <= rw; row++)
     {    

         //extract out excel value and store it into empID variable    
         var empID=(string)(xlworkSheet.Cells[row,1]as(Excel.Range).Value.ToString();

         //store empID into employeeobj               
         employeeobj.employeeID = empID;
         //xlNewSheet2.Cells[row, 1].Value = employeeobj.employeeID;
         data.Add(employeeobj.employeeID);
       }
 }

//最后就是绑定数据到新的sheet.