从excel文件

时间:2016-06-28 11:48:47

标签: excel f#

这是一个简单的问题,但我没有成功。 我需要在行和列上运行一个带有值的循环 - 以选择值。

我编写了一个函数来执行此操作但是通过调试我看到行计数为1048576且文件只有32行的值(列中也是如此)

功能代码:

let functionParseXmlToCsv (xmlFile:string , excelFormatFile:string , excelPath:string) =

    let xlApp = new Excel.ApplicationClass()
    let xlWorkBookInput = xlApp.Workbooks.Open(excelPath  + DateTime.Today.ToString("yyyy_dd_MM")+".xlsx")
    let listOfRows = ResizeArray<string>()

    for tabs in 1 .. xlWorkBookInput.Worksheets.Count do

        let listOfCells = ResizeArray<string>()

        let xlWorkSheetInput = xlWorkBookInput.Worksheets.[tabs] :?> Excel.Worksheet

        let filePath =  excelPath + DateTime.Today.ToString("yyyy_dd_MM")+"_"+xlWorkSheetInput.Name+".csv"

        //let csvFile = File.Create(filePath)

        use csvFile = StreamWriter(filePath, true)

        for rowNumber in 1 .. xlWorkSheetInput.Rows.Count do


            for columnNumber in 1.. xlWorkSheetInput.Columns.Count do

                let cell = xlWorkSheetInput.Cells.[rowNumber,columnNumber]

                let value = string cell

                listOfCells.Add(value)

            let s = String.Join(", ", listOfCells)

            csvFile.WriteLine(s)

    File.Exists(excelPath  + DateTime.Today.ToString("yyyy_dd_MM")+".xlsx")

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

尝试UsedRange修复您的问题。因此,将行/列循环更改为

for rowNumber in 1 .. xlWorkSheetInput.UsedRange.Rows.Count do

for columnNumber in 1.. xlWorkSheetInput.UsedRange.Columns.Count do