2013-04-25 41 views
3

我在文檔庫中有一個用戶可編輯的Excel文件,它定義了F#程序的一些輸入。我想用F#-y讀取它,所以我想我會試用FSharpX和它們的ExcelFile類型提供程序。F#類型提供者FSharpX.ExcelFile和試圖在不同輸入上構造的奇怪類型錯誤

什麼工作

提供商通過的NuGet安裝並啓用,這一點也適用:

open FSharpX 
type Example = ExcelFile<"example.xlsx", "Worksheet1", true> 
let file = new Example() 
for row in File.Data do ...... 

什麼不

然而,當我嘗試用不同的初始化構造文件(我在運行時從數據庫中取出並保存在臨時位置),我得到了一個非常奇怪的類型錯誤。

let file = new Example(@"c:\temp\path\to.xlsx") 

結果

The type provider 'FSharpx.TypeProviders.ExcelProvider+ExcelProvider' reported an error in the context of provided type 'FSharpx.ExcelFile,filename="example.xlsx",sheetname="Worksheet1",forcestring="True"', member '.ctor'. The error: Type mismatch when splicing expression into quotation literal. The type of the expression tree being inserted doesn't match the type expected by the splicing operation. Expected 'System.String', but received type 'System.Tuple`2[System.String,System.String]'. Consider type-annotating with the expected expression type, e.g., (%% x : string) or (%x : string). Parameter name: receivedType

咦?

我不知道它所說的元組可能來自哪裏,我也沒有任何關於如何初始化它的其他想法。

紅利問題:如果我想在運行時改變工作表名稱,該怎麼辦?現有的FSharpx提供者似乎不允許這樣做。

回答

4

這是提供程序中的錯誤,而不是您的代碼。提供的構造函數使用您傳入的文件名,但底層代碼需要文件名和工作簿名稱(這是元組應該來自的位置)。

+0

OK,只是檢查,我想我是瘋了還是做錯了什麼。將提交一個錯誤。 – 2013-04-26 02:02:02

+2

此錯誤已修復:https://github.com/fsharp/fsharpx/commit/e9912d9cb68cedd723e351748c47f9fefc7bbb3d(請參見行154) – 2013-05-09 15:33:11

4

看起來是一個錯誤。所提供的構造函數接受一個單一的文件名參數不正確調用內部構造的報價:

ty.AddMember(ProvidedConstructor([ProvidedParameter("filename", typeof<string>)], InvokeCode = fun [filename] -> <@@ ExcelFileInternal(%%filename) @@>)) 
.... 
type ExcelFileInternal(filename, sheetorrangename) 

我前一陣子分岔的FSharpx.Excel型供應商,對其進行修改,以便使用ClosedXML(而不是辦公室互操作性),如果文件是> = office 2007(我注意到你的工作簿是)。我還對API進行了一些更改,將工作表(和/或範圍)作爲提供的類型,'行'提供的類型和'Row {n}'類型公開。

type exc = ExcelFile<"C:\\temp\\Template.xlsx",false> 
let file = new exc(@"C:\\temp\\Book.xlsx") 
for row in file.Sheet1.Rows do 
    printfn "%s" row.BID 
let sht1Row1Col20 = file.Sheet1.Row1.BID 

雖然恐怕沒有'表格'提供的類型,所以在運行時不能改變工作表名稱。有一個字符串[]'SheetAndRangeNames'類型,但這對你來說不會有多大用處。然而,實施起來不應該特別棘手。你可以在這裏找到它:

https://github.com/bennylynch/ExcelTypeProvider/

+1

謝謝本!我認爲你應該讓他們來拉你的改變,任何避免互操作的東西都是很好的......這在我的優先級列表中並不是很高,但是也許我會分叉你的叉子並在某些時候繼續添加東西。 – 2013-04-26 14:32:51

+1

總是有意義的,但從來沒有圍繞過它....我懷疑它現在會有點不合格。想想我會分叉最新的,更新它,併發送拉請求....現在我知道它會被使用。只是希望他們不要公開嘲笑我的代碼..... – 2013-04-26 20:35:30