繁体   English   中英

如何用 c# 编写 excel 公式直到 excel 的最后一行(互操作)?

[英]How to write excel formula with c# until last row of the excel (interop)?

我是一名 RPA 开发人员,我正在尝试学习如何管理读取 c# 代码的“调用代码”活动。

我需要在 excel 中做一些简单的 excel 事情,比如插入列、编写公式和执行“粘贴值”

我是 c# 和 .Net 编码的新手。

我写了以下代码:

// Define Excel Application

Microsoft.Office.Interop.Excel.Application  xlApp=new Microsoft.Office.Interop.Excel.Application();
// Define Excel Workbook
try{
    Microsoft.Office.Interop.Excel.Application.Workbook xlWorkbook= xlApp.Workbooks.Open(Path);
    
    
// Define Excel Worksheet
    
    Microsoft.Office.Interop.Excel.Application.Worksheet xlWorksheet= (Microsoft.Office.Interop.Excel.Application.Worksheet)xlWorkbook.Sheets["EXTR"];
    
//Activate Worksheet
    xlWorksheet.Activate();
    
}

现在我需要写一个公式,直到 excel 的最后一行。

我怎样才能做到这一点? 另外,如果您能帮助我如何从具有公式的列中复制值并仅将值粘贴到其他列中。

保护你

以下文章说明了如何在 Excel 中处理 Range 对象:

      Excel.Workbooks objBooks;
      Excel.Sheets objSheets;
      Excel._Worksheet objSheet;
      Excel.Range range;

try
      {
         // Instantiate Excel and start a new workbook.
         objApp = new Excel.Application();
         objBooks = objApp.Workbooks;
         objBook = objBooks.Add( Missing.Value );
         objSheets = objBook.Worksheets;
         objSheet = (Excel._Worksheet)objSheets.get_Item(1);

//Get the range where the starting cell has the address
         //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
         range = objSheet.get_Range("A1", Missing.Value);
         range = range.get_Resize(5, 5);

if (this.FillWithStrings.Checked == false)
         {
            //Create an array.
            double[,] saRet = new double[5, 5];

//Fill the array.
            for (long iRow = 0; iRow < 5; iRow++)
            {
               for (long iCol = 0; iCol < 5; iCol++)
               {
                  //Put a counter in the cell.
                  saRet[iRow, iCol] = iRow * iCol;
               }
            }

//Set the range value to the array.
            range.set_Value(Missing.Value, saRet );
         }

else
         {
            //Create an array.
            string[,] saRet = new string[5, 5];

//Fill the array.
            for (long iRow = 0; iRow < 5; iRow++)
            {
               for (long iCol = 0; iCol < 5; iCol++)
               {
                  //Put the row and column address in the cell.
                  saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
               }
            }

//Set the range value to the array.
            range.set_Value(Missing.Value, saRet );
         }

//Return control of Excel to the user.
         objApp.Visible = true;
         objApp.UserControl = true;
      }
      catch( Exception theException ) 
      {
         String errorMessage;
         errorMessage = "Error: ";
         errorMessage = String.Concat( errorMessage, theException.Message );
         errorMessage = String.Concat( errorMessage, " Line: " );
         errorMessage = String.Concat( errorMessage, theException.Source );

MessageBox.Show( errorMessage, "Error" );
      }

如果您只处理 Open XML 文档,您还可以考虑使用 Open XML SDK 来处理 Excel 文件,有关详细信息,请参阅 欢迎使用 Open XML SDK 2.5 for Office

暂无
暂无

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

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