我正在尝试连接excel文件中的两列并将其保存到新列。 但是如果一个值为空,我想忽略连接。 并转移到下一个单元格。
Excel.Worksheet exampst = (Excel.Worksheet)Sh.get_Item("Sheet2");
Excel.Range rng2 = (Excel.Range)exampst.get_Range("H1", Type.Missing);
rng2.EntireColumn.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight, Microsoft.Office.Interop.Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);
exampst.Range["H1", Type.Missing].Value2 = "CombineID";
Excel.Range rgcon2 = exampst.Range["H2"];
//Excel.Range rng3 = (Excel.Range)RFCst.get_Range("G2", Type.Missing);
//object obj = rng3.Value2;
rgcon2.Formula = String.Format("=CONCATENATE(A2,G2)");
我的Excel表格如下所示。
A1 G1 H1
1 a 1a //this is the concatenated value.
2 //This value b as null,as G1 column has missing row value.
3 b 3b
4 c 4c
5 d 5d
6
7 e 7e
8 f 8f
9 and so on....
请帮忙。
答案 0 :(得分:0)
在连接之前,如果它们为空或空,则需要首先检查A列和G列中单元格中的值。 你可以这样做:
object rangeObject = activeWorksheet.Cells[3, "A"];
Range range = (Range)rangeObject;
object rangeValue = range.Value2;
string cellValue1 = rangeValue.ToString();
rangeObject = activeWorksheet.Cells[3, "G"];
range = (Range)rangeObject;
rangeValue = range.Value2;
string cellValue2 = rangeValue.ToString();
if (cellValue1 != string.Empty && cellValue2 != string.Empty)
{
//concatenate cells
}