f# - 在 F# 中写入 Excel

标签 f# excel-dna microsoft.office.interop.excel

有人有使用 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/

相关文章:

Excel DNA - 我可以避免一起重高潮吗?

c# - 检测 xll 是否已永久安装或 xll 文件是否只是临时打开?

f# - 在 F# 中,如何生成具有 Func<obj> 类型的表达式?

f# - 如何在执行异步任务时保持按钮启用

generics - F# 嵌套泛型类型与实现的类型不兼容

f# - 如何在远程机器上使用 FSharp.Management WMI Provider

c# - Excel-DNA——模板化的 CustomTaskPane 异常