[英]Excel cell value displayed incorrectly with Excel.Interop and C#
[英]C#: Getting a cell's value with Excel.interop
我希望用C#中的軟件從excel文件中獲取單元格的值; 不是文本,因為文本取決於列的大小,我不能自己更改,軟件不應該受到干擾。 所以我的選擇是獲得價值。
我將我的單元格作為1:1的范圍,因此我有一個Range對象,但語言無法識別Value字段和Value2字段。 我嘗試使用get_Value(),但它需要一個參數,我不知道要放入什么,我沒有找到有關它的文檔。 這是我的代碼(從循環中提取):
if((string)(ws_Varsheet.get_Range("L" + iIndex, "L" + iIndex).Text) != "")
{
rng_ResolutionCell = ws_Varsheet.get_Range("L" + iIndex, "L" + iIndex);
float iResolution;
rng_ResolutionCell.Cells.get_Value(&iResolution); //what to do here?
str_Resolution = ((string)iResolution.toString()).Replace(",",".");
str_Resolution = str_Resolution.Replace(" ","");
mObj.Str_Resolution="1";
}
你能幫幫我嗎? 謝謝你提前。
我經常發現代碼比全部需要的代碼復雜一點。 基本上讀單元格就像:
Xl.Range rng = sheet.Cells[row, column] as Xl.Range;
return rng.Value2;
從MSDN上的這篇文章看起來你會想要這樣的東西:
var cellRangeValue = rng_ResolutionCell.get_Value(System.Type.Missing);
也:
newWS = (Microsoft.Office.Interop.Excel.Worksheet)newWB.Worksheets[2];
newWS.Select();
string ExcelCellContent = (string)newWS.Cells[2, 1].Value2;
用法:
if((string)(ws_Varsheet.Cells[x, y].Value2 != "")
{
// process
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.