Reading cell contents in an Excel file with F# and Open XML SDK

天涯浪子 提交于 2021-02-10 13:16:55

问题


I can't compile the following code, which has been translated from C#.

The compiler produced this error: error FS0010: Incomplete structured construct at or before this point in implementation file. Expected incomplete structured construct at or before this point or other token.

// Assemblies
open System
open System.Linq
open System.Data
open System.Windows
open DocumentFormat.OpenXml // Also install DocumentFormat.OpenXml from nuget
open DocumentFormat.OpenXml.Packaging
open DocumentFormat.OpenXml.Spreadsheet
open Microsoft.Office.Interop.Excel


//Read the value of a single cell by string coordinates (Open XML SDK)
    let read_value_openxml file_path_and_name column row =
        let stream = new System.IO.FileStream(file_path_and_name, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)

        // Open the spreadsheet document for read-only access.
        let document = SpreadsheetDocument.Open(stream, false)

        // Retrieve a reference to the workbook part.
        let wbPart = document.WorkbookPart

        // Find the sheet with the supplied name, and then use that sheet object to retrieve a reference to the first worksheet.
        let firstSheet:Sheet = wbPart.Workbook.Descendants<Sheet>().First()
        let theSheet:Worksheet = ((WorksheetPart.wbPart.GetPartById(firstSheet.Id)).Worksheet

        // Retrieve a reference to the worksheet part.
        let wsPart = wbPart.GetPartById(firstSheet.Id)

        // Use its Worksheet property to get a reference to the cell whose address matches the address you supplied.
        let theCell = wsPart.Worksheet.Descendants<Cell>().Where(fun c -> c.CellReference = column + row).FirstOrDefault()

            // Return a value
            theCell.InnerText

回答1:


I think the problem is with indentation. In F# the code fits in one block if it's indented by the same amount of spaces.

You should return value at the same level of indentation like the rest code of the ead_value_openxml function. Also function seems to be indented already. Try this way:

//Read the value of a single cell by string coordinates (Open XML SDK)
let read_value_openxml file_path_and_name column row =
    let stream = new System.IO.FileStream(file_path_and_name, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)

    // Open the spreadsheet document for read-only access.
    let document = SpreadsheetDocument.Open(stream, false)

    // Retrieve a reference to the workbook part.
    let wbPart = document.WorkbookPart

    // Find the sheet with the supplied name, and then use that sheet object to retrieve a reference to the first worksheet.
    let firstSheet:Sheet = wbPart.Workbook.Descendants<Sheet>().First()
    let theSheet:Worksheet = ((WorksheetPart.wbPart.GetPartById(firstSheet.Id)).Worksheet

    // Retrieve a reference to the worksheet part.
    let wsPart = wbPart.GetPartById(firstSheet.Id)

    // Use its Worksheet property to get a reference to the cell whose address matches the address you supplied.
    let theCell = wsPart.Worksheet.Descendants<Cell>().Where(fun c -> c.CellReference = column + row).FirstOrDefault()

    // Return a value
    theCell.InnerText


来源:https://stackoverflow.com/questions/34267464/reading-cell-contents-in-an-excel-file-with-f-and-open-xml-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!