【发布时间】:2015-08-10 13:02:50
【问题描述】:
我正在尝试从 excel 中读取一些数据。该值被存储为 obj 类型,但是当我尝试将其转换为 int 或 double 类型时,出现类型错误。
我的代码:
open System
open System.Data
#r "Microsoft.Office.Interop.Excel"
open Microsoft.Office.Interop
let xl = new Excel.ApplicationClass()
let wb = xl.Workbooks.Open(@"Y:\Test.xlsx")
let Sheet1 = wb.Worksheets.["Sheet1"] :?> Excel.Worksheet
let tr= Sheet1 .Cells.[1,3]:?> int
错误:
System.InvalidCastException: Specified cast is not valid.
at <StartupCode$FSI_0018>.$FSI_0018.main@() in Y:\Script2.fsx:line 20
Stopped due to error
【问题讨论】:
-
如果我将单元格更改为范围,则投射工作正常
let tr:double = unbox(Sheet1.Range("c1").Value2) -
Cells.[1,3]返回什么(哪种数据类型? -
WorksheetClass.Cells 返回 Microsoft.Office.Interop.Excel.Range 请参阅doc
-
@HaagenDaz 最好直接使用 range,Cells 返回 Range,所以
let tr:double = (Sheet1.Cells.[1,3] :?> Microsoft.Office.Interop.Excel.Range).Value2 |> unbox正在转圈
标签: f# excel-interop f#-interactive f#-3.0 f#-fake