[英]C# Excel Cut/Paste cells conditionally
如何檢查我的兩列(I列和H列)以查看I列中是否有非空單元格,而H列中的單元格為空,然后將非空單元格從I列剪切並粘貼到空H列中的單元格。
舉一個更清晰的例子:
到目前為止,我的想法是這樣的:
xl.Range ColH = sourceSheet.UsedRange;
xl.Range ColI = sourceSheet.UsedRange;
string occupiedCellsH;
string emptyCellsI;
occupiedCellsH = ColH.ToString();
emptyCellsI = ColI.ToString();
if (string.IsNullOrEmpty(emptyCellsI) && occupiedCellsH != null)
{
ColH.Cells.Copy(ColI);;
}
這將達到目的:
const int hCol = 8;
const int iCol = 9;
//start at 1 as there are column headings
for (int i = 1; i < sourceSheet.UsedRange.Rows.Count; i++)
{
if ((sourceSheet.cells[i, hCol].value??"").ToString()!="" &&
(sourceSheet.cells[i, iCol].value??"").ToString()=="")
{
sourceSheet.cells[i, iCol].value = sourceSheet.cells[i, hCol].value;
sourceSheet.cells[i, hCol].value = "";
}
}
按照您的評論進行相反操作(假設您仍然需要這樣做):
const int hCol = 8;
const int iCol = 9;
//start at 1 as there are column headings
for (int i = 1; i < sourceSheet.UsedRange.Rows.Count; i++)
{
if ((sourceSheet.cells[i, iCol].value??"").ToString()!="" &&
(sourceSheet.cells[i, hCol].value??"").ToString()=="")
{
sourceSheet.cells[i, hCol].value = sourceSheet.cells[i, iCol].value;
sourceSheet.cells[i, iCol].value = "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.