-1

我有一个问题,当我从列中检索一个 txt 文件时,使用:

scanObject = scanWorksheet.Cells[i, scanCols[j]]; 

scanRange = (Excel.Range)scanObject;

scanRangeValue = scanRange.Value2;

它传递换行,LF,命令。从那里我生成一个文本文件,用作另一个程序的输入,但是这个 LF 命令使导入系统跳过该字段。检索文本列时如何删除它们?

4

1 回答 1

0

在步骤中,您可以看到每个步骤的作用:

// get the value in the cell (which is an object from Value2)
object scanRangeValue = scanRange.Value2;
// cast to string (you may need to check for errors if it is not a string)
string scanStringValue = (string)scanRangeValue;
// Replace both \r\n and \r and \n
scanStringValue = scanStringValue.Replace("\r", "").Replace("\n", "");

NoteValue2是一种对象类型,因此您需要强制转换为string. 如果返回的值不是 a ,这可能会引发异常string,因此您可能需要使用try ... catch ...或其他测试,例如if (scanRangeValue is string)(参见MSDN - How to: Safely Cast by Using as and is Operators (C# Programming Guide)

于 2013-05-23T17:21:48.320 回答