c# - 在添加到字符串之前捕获空值

标签 c# visual-studio-2008

大家好,我只能为提出这样一个菜鸟问题而道歉,但这是今天我第二次依赖这个很棒的论坛:)

我有一个字符串,它由一个数组中的值组成,取自 Excel 工作表,其中只有一些值是空白的。我正在将这些值输出到 CSV 文件,但不希望输出空白值,这些值以逗号形式输出,因此在我的 CSV 文件中提供了额外的行。

无论如何,这是我的代码,我想在将数组值添加到字符串之前分别比较数组值是否不等于“”。如果这不可能,我想我可以拆分字符串,比较,然后重新加入它。

enter code here//Get a range of data. 

range = objSheet.get_Range(ranges1, ranges2);

//Retrieve the data from the range. 
Object[,] saRet;
saRet = (System.Object[,])range.get_Value(Missing.Value);

//Determine the dimensions of the array. 
long iRows;
long iCols;
iRows = saRet.GetUpperBound(0);
iCols = saRet.GetUpperBound(1);

//Build a string that contains the data of the array. 
String valueString;
valueString = "";

System.IO.StreamWriter OutWrite = new System.IO.StreamWriter("h:\\out.csv");

    for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
    {

        for (long colCounter = 1; colCounter <= iCols; colCounter++)
        {
            //Write the next value into the string.
            valueString = String.Concat(valueString, Convert.ToString(saRet[rowCounter, colCounter]) + ", ");
        }

        //Write in a new line. 
        {
            valueString = String.Concat(valueString, "\n");
        }
    }

    //Report the value of the array.

    MessageBox.Show(valueString, "Array Values");

    OutWrite.WriteLine(valueString);
    OutWrite.Close();
    OutWrite.Dispose();
}

干杯,感谢您的耐心等待!

安迪。

最佳答案

关于c# - 在添加到字符串之前捕获空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4033828/

相关文章:

delphi - Delphi Prism/VS 2008:一键式从代码切换到设计?

c# - 使用任务检查互联网连接

c# - 为什么我不能在我的代码中输入设置?

c# - 将单引号转换为字符串中的转义单引号

c++ - Sizeof 报告类型定义结构的大小不正确 - C++

c - 使用 _sprintf_l 格式化 double 和数千个数

c# - 想要使用 Web 浏览器控件保存网页

c# - 了解 WCF 可靠 session 重试行为

html - 如何在 Visual Basic 2010 中通过 Web 浏览器从嵌入式资源加载 .html 文件

visual-studio-2008 - Visual Studio 风格?