本文介绍了F#Excel UsedRange未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:字段,构造函数或成员UsedRange未定义。

Error: "The field, constructor or member 'UsedRange' is not defined".

代码摘录:

open Microsoft.Office.Interop.Excel
open Microsoft.Office.Tools.Excel // Onorio's excellent suggestion didn't fix code until this line was added.
// Once the above line was added, Onorio's suggestion worked,
// but now F# says "The field, constructor or member 'EntireColumn' is not defined".

let xl = ApplicationClass()
xl.Workbooks.OpenText(...
let wb = xl.Workbooks.Item(1)
let ws = wb.ActiveSheet :?> Worksheet // Per Onorio's suggestion.

ws.UsedRange.EntireColumn.AutoFit()
// "The field, constructor or member 'EntireColumn' is not defined".

// Here's another example of UsedRange now working,
// but Properties and Methods of UsedRange not working:
// (uniqueID is simply a Boolean set earlier based on ini file values)
if uniqueID then
   xl.Range("A1").EntireColumn.Insert() |> ignore
   xl.Range("A1").Value <- "UniqueID"
   xl.Range("A2").Value <- 1
   xl.Range("A2").AutoFill(xl.Range("A2:A" + ws.UsedRange.Rows.Count),
                           XlAutoFillType.xlFillSeries) |> ignore
// Here it's "The field, constructor or member 'Rows' is not defined".


推荐答案

尝试添加以下内容:

let wb = xl.Workbooks.Item(1)
let actSheet = wb.ActiveSheet :?> Worksheet  //downcast obj to Worksheet
actSheet.UsedRange.EntireColumn.AutoFit()

wb.ActiveSheet 返回一个obj,您需要将其下载到工作表才能使 UsedRange 方法解析正确。

wb.ActiveSheet returns an "obj" and you need to downcast it to a Worksheet in order for the UsedRange method to resolve correctly.

这篇关于F#Excel UsedRange未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:47