2014-01-16 2 views
3

내 목표는 deedle frame을 효율적으로 복사하여 F #에서 능가하는 것입니다. F #에서 Excel로 일한 적이 없습니다. 나는 this tutorial에 의해 고무되었다.deedle frame to excel

#r "Microsoft.Office.Interop.Excel" 
open Microsoft.Office.Interop.Excel 
/// Export data frame to excel 
let exportFrameToExcel (frame: Frame<DateTime, string>) (x,y) = 
    // Run Excel as a visible application 
    let app = new ApplicationClass(Visible = true) 
    // Create new file and get the first worksheet 
    let workbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet) 
    // Note that worksheets are indexed from one instead of zero 
    let worksheet = (workbook.Worksheets.[1] :?> Worksheet) 

    let mutable col = y+1 
    for key in frame.ColumnKeys do 
     worksheet.Cells.[x, col] <- key 
     col <- col + 1 
    for i = 0 to frame.RowCount-1 do 
     let row = frame.GetRowAt(i) 
     let rowKey = frame.GetRowKeyAt(i) 
     worksheet.Cells.[i+x+1, y] <- rowKey 
     for j = 0 to frame.ColumnCount-1 do 
      let value = row.GetAtAs<float>(j) 
      worksheet.Cells.[i+x+1, j+y+1] <- value 

그것은 느리고 데이터가 복사되는 동안 내가 엑셀 ​​창을 터치하면 충돌이 발생할 수 있습니다 :

이 내 코드입니다. 개선의 여지가 충분하다고 확신합니다. 예를 들어 ApplicationClass을 데이터가 복사 된 경우에만 visible으로 바꾸어야한다고 생각합니다.

효율적이고 관용적 인 방법으로이 작업을 수행하는 데 도움이되는 지침/도움을 보내 주시면 감사하겠습니다. 감사.

답변

5

BlueMountain (Deedle에서 대부분의 작업을 수행)에는 실제로 데이터를 Excel로 전달하는 유틸리티가있었습니다. 우리는 이것을 Deedle에 포함시키고 싶었지만 출판하기를 결코 관리하지 못했습니다. & 문서를 연마하는 데 약간의 시간이 걸리기 때문입니다.

우리는 실제로 내 데모 회담의 일부로 GitHub에 소스 코드를 넣었으므로 can get it here입니다. 이 사용
, 당신은 쓸 수 있어야 :

xl?A1 <- deedleFrame 

는 더 많은 당신이 할 수 있습니다합니다 - Xl 타입의 멤버를 보라.

또한 원래는 Excel COM interop 용 래퍼를 사용하기 쉬운 NetOffice을 사용했습니다. 참조 된 샘플은 사용하지 않지만 아마도 좋은 생각 일 수 있습니다.