【问题标题】:Conditional formatting/Concatenating based on value in excel using c#使用c#根据excel中的值进行条件格式/连接
【发布时间】:2017-03-16 19:32:58
【问题描述】:

我正在尝试连接 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....

请帮忙。

【问题讨论】:

  • 你忘了问问题。您的代码有什么具体问题?
  • 大声笑。我希望我的代码如图所示进行连接。但它不考虑 G1 列中的空白。所以如果 G1 中的值存在,则连接并将结果保存在 H 列中。如果没有,只是忽略连接。

标签: c# excel row concatenation


【解决方案1】:

在连接之前,您需要先检查 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
}

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2012-05-09
    相关资源
    最近更新 更多