简体   繁体   中英

Conditional formatting/Concatenating based on value in excel using c#

I am trying to concatenate two columns in excel file and save it to new column. but i want to ignore the concatenation if one value is blank. and move to next cell.

            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)");

My excel sheet is like below. 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....

please help.

Before concatenating, you need to check the value in cells in columns A and G first if they are null or empty. You can do something like this:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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