Das Folgende erhält Objekte aus einer Excel -Tabelle, die übergießen ist string
oder übergossen zu double
. Das Problem ist, dass selbst wenn ich weiß, dass das Objekt sichtbar eine Ganzzahl ist, die in der Tabelle als "allgemein" formatiert ist. double
. Es wäre zwar einfach, das Doppel in das nächste umzuwandeln int
, Gibt es eine Möglichkeit, ein Objekt an einen gießen zu lassen int
?
// returns null if the cell is empty
// returns an object castable to a string if it is a string
// returns an object castable to a double if it is a floating point or integer number
internal object GetCell(int row, int column)
{
if ((row != 0) && (column != 0) && (worksheet != null))
{
string cellName = ExcelCellReference(row, column);
Microsoft.Office.Interop.Excel.Range oneCellRange;
oneCellRange = worksheet.get_Range(cellName, cellName);
object oneObject;
// contrary to MSDN, the value Empty, checkable with method IsEmpty()
// does not work if the cell was empty.
// instead, what happens is that null is returned.
oneObject =
(object)
oneCellRange.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault);
return oneObject;
}
else
{
if (worksheet == null)
throw new Exception("null worksheet reference");
else
throw new Exception("invalid argument");
}
}
Bearbeiten: Wenn jede Zahl in Excel als schwimmende Punktzahl dargestellt wird, auch wenn sie sichtbar als Ganzzahl ist, würde dies dieses Verhalten erklären. In diesem Fall gibt es keine andere Lösung als Konvertierung.
Bearbeiten: Die Leute veröffentlichen weiterhin Konvertierungsantworten. Diese Frage soll idiomatisch ansprechen Interop
Antworten, keine Konvertierungsantworten.
Bearbeiten: Chris 'Antwort in einem Kommentar int i = (int)(double)GetCell(r, c)
ist wohl richtig, aber nicht das, was ich vorhatte, weil die Konvertierung außerhalb von vorhanden ist Interop
.