有人有使用 Microsoft.Office.Interop 从 F# 将值写入 Excel 电子表格的经验吗?我可以很好地读取值,但是当我尝试更改单元格的值时,我总是会收到错误。我见过出现此错误的帖子,其中列或行为 0,但在我的情况下,这些值始终为正。我正在从 excel-dna 获取我的 Excel 应用程序对象。
let getCell (sheet: Excel.Worksheet) (row: int, col:int) =
let rng = sheet.Cells.Item(row, col) :?> Excel.Range
match isNull rng with
| true ->
None
| false ->
match isNull rng.Value2 with
| true -> None
| false ->
let s = "Hello"
let o = s :> obj // passing in unboxed string does not help
rng.Value2 <- o // this line throws an error Exception from HRESULT: 0x800A03EC Error
Some rng.Value2
最佳答案
我使用 NetOffice(https://netoffice.io/ - 小心获取正确的 Nuget 软件包)。
例如,在打开新的空白工作簿后从功能区处理程序调用此代码时,此代码将起作用:
open NetOffice.ExcelApi
...
use app = new Application (null, ExcelDnaUtil.Application)
let wb = app.ActiveWorkbook
let sheet = wb.Sheets.["Sheet1"] :?> Worksheet
let range = sheet.Cells.[1, 1]
range.Value2 <- "Hello"
关于f# - 在 F# 中写入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76080351/