簡體   English   中英

迭代excel表中特定列號的所有行,並對每個行值進行一些處理

[英]Iterate all rows of a specific column number in excel sheet and do some processing on each row value

我試圖創建一個循環,遍歷excel表中特定列號的所有行,比如列號16,並對每行中每個單元格的值進行一些處理。 例如,循環將迭代單元格1,16,然后下一個循環到單元格2,16,然后下一個單元格3,16 ....然后一直到那個特定列號中有多少行表單在這種情況下,列號16.到目前為止,我能夠使用如下語句來獲取和設置單個excel單元格的值:

string cellValue = excelSheet.Cells[1, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[1, 16] = cellValue; 

但是我希望在我的循環中將行號循環到這樣的音調:

string cellValue = excelSheet.Cells[n, 16].Value.ToString();
//Do some processing.
excelSheet.Cells[n, 16] = cellValue; 

有什么想法嗎?

你需要一個for loop

for(int n = 1; n <= excelSheet.Columns.Count; n++)
{
    string cellValue = excelSheet.Cells[n, 16].Value.ToString();
    //Do some processing.
    excelSheet.Cells[n, 16] = cellValue; 
}

我假設你正在使用C#和Microsoft.Office.Interop.Excel COM +庫。

嘗試類似的東西

Microsoft.Office.Interop.Excel.Range range = excelSheet.UsedRange;
for (int index = 0; index < range.Rows.Count; index++)
{
    string cellValue = excelSheet.Cells[index, 16].Value.ToString();
    //Do some processing.
    excelSheet.Cells[index, 16] = cellValue; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM