使用 F# 读取 Excel 文件中的单元格内容并打开 XML SDK
Reading cell contents in an Excel file with F# and Open XML SDK
我无法编译以下从 C# 翻译过来的代码。
编译器产生此错误:错误 FS0010:实现文件中此时或之前的结构化构造不完整。在此点或其他标记处或之前预期不完整的结构化构造。
// 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
我认为问题出在缩进上。在 F# 中,如果代码缩进了相同数量的空格,则代码适合一个块。
您应该 return 值与 ead_value_openxml 函数的其余代码相同的缩进级别。功能似乎也已经缩进了。试试这个方法:
//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
我无法编译以下从 C# 翻译过来的代码。
编译器产生此错误:错误 FS0010:实现文件中此时或之前的结构化构造不完整。在此点或其他标记处或之前预期不完整的结构化构造。
// 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
我认为问题出在缩进上。在 F# 中,如果代码缩进了相同数量的空格,则代码适合一个块。
您应该 return 值与 ead_value_openxml 函数的其余代码相同的缩进级别。功能似乎也已经缩进了。试试这个方法:
//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