this is what I get currently. Most functions in libxl can be exposed in the 
same way

But something still puzzled me, or I have not know yet in nim:

  1. for xlBookAddSheetW, the last arg is SheetHandle initSheet actually. But I 
used int. How to cast a number to SheetHandle?
  2. how to judge the type is cstring or widecstring, so I can convert cstring 
to widecstring


    
    
    import encodings
    
    type
        tagBookHandle* {.final, header: "<handle.h>", importc: "struct 
tagBookHandle".} = object
        BookHandle* = ptr tagBookHandle
    
    type
        tagSheetHandle* {.final, header: "<handle.h>", importc: "struct 
tagSheetHandle".} = object
        SheetHandle* = ptr tagSheetHandle
    
    
    const NUMFORMAT_DATE = 14
    
    const
        xldll* = "libxl.dll"
    
    proc xlCreateBookCW*(): BookHandle {.cdecl, importc: "xlCreateBookCW", 
dynlib: xldll.}
    
    
    #~ proc xlBookAddSheetW*(handle: BookHandle; name: cstring; initSheet: 
SheetHandle): SheetHandle {.cdecl,
        #~ importc: "xlBookAddSheetW", dynlib: xldll.}
    
    proc xlBookAddSheetW*(handle: BookHandle, name: cstring, initSheet: int): 
SheetHandle {.cdecl,
        importc: "xlBookAddSheetW", dynlib: xldll.}
    
    proc xlSheetWriteStrW*(handle: SheetHandle; row: cint; col: cint; value: 
Widecstring;
                          format: cint): cint {.cdecl,
        importc: "xlSheetWriteStrW", dynlib: xldll, discardable.}
    
    proc xlBookSaveW*(handle: BookHandle; filename: Widecstring): cint {.cdecl,
        importc: "xlBookSaveW", dynlib: xldll, discardable.}
    
    proc xlBookReleaseW*(handle: BookHandle) {.cdecl, importc: "xlBookReleaseW",
                                            dynlib: xldll.}
    
    
    var book = xlCreateBookCW()
    
    if not book.isnil:
        var sheet = xlBookAddSheetW(book, "Sheet1", 0);
        if not sheet.isnil:
            xlSheetWriteStrW(sheet,
                    cint(2), cint(1),
                    newWideCString(convert("你好!", "utf8", "gb2312")),
                    cint(0)
                    );
        
        if not (xlBookSaveW(book, newWideCString(convert("测试.xls", "utf8", 
"gb2312")))==0):
            echo "\nFile example.xls has been created.\n";
        
        xlBookReleaseW(book);
    

Reply via email to