2012-11-25 176 views
0

使用libxl對IOS的示例項目,我想在包打開一個exsisting xls文件打開現有的Excel文件libxl

BookHandle book = xlCreateBook(); 
xlBookLoad(book,"Beta-1.xls");  
SheetHandle sheet = xlBookGetSheet(book,0); 
NSLog(@"%@",sheet); 

它總是返回null,任何人都可以告訴我,我要去哪裏錯誤的,或者這甚至是可能的。

有沒有其他的選擇,我花了好幾個小時看,一定是IOS兼容。

回答

0

問題源於您使用無效文字記錄您新創建的值(您沒有指針)的事實。 %@是ObjC對象字面量,所以你將要使用cout來代替。

+0

不確定如何在xcode中使用cout – blakey87

+0

這是什麼意思? #include 並像普通函數一樣使用cout。 Xcode不只是一種語言。 – CodaFi

+0

我收到一個錯誤,'找不到stdio文件'。 – blakey87

0

你看過sourceforge上的libxls嗎?它有專門針對iOS的objc框架。

+0

嘿大衛,是的,我已經嘗試,但不能得到它編譯,關閉主題,但林目前正在使用您的dhxls讀取excel文件,它的偉大!,你有更新的IOS版本的寫作。 – blakey87

+0

那麼,你沒有說你正在嘗試寫一個文件 - 對,現在xlslib沒有更新ios。從我讀到的關於libxl的文章看來,它定期更新爲閱讀和寫作 - 商業圖書館可以做的一些事情。 –

1

發佈此舊線程以供參考,並專門回答使用LibXL來讀取XLS或XLSX格式文件的問題。下面的代碼演示瞭如何使用LibXL讀取excel文件並打印每個單元格的內容(值)和類型(數字,字符串,布爾...)。

// Get the path to the excel file to be opened 
NSString *path = [[NSBundle mainBundle] pathForResource:@"FILE_NAME" ofType:@"xlsx"]; 

// Convert the path into a const char * from an NSString 
const char *file = [path cStringUsingEncoding:NSASCIIStringEncoding]; 

// Create a handle for the excel book (XLSX) 
// Use xlCreateBook() for XLS file format 
BookHandle book = xlCreateXMLBook(); 
xlBookLoad(book, file); 

// Authorize full use of the library (uncomment below and change values) 
//xlBookSetKey(book, "REGISTERED_NAME", "PROVIDED_KEY"); 

// Determine if the excel file handle is valid 
if(xlBookLoad(book, file)){ 
    // We have the book now get the first sheet in the book 
    SheetHandle sheet = xlBookGetSheet(book, 0); 

    // Ensure the sheet has been opened as is valid 
    if(sheet){ 
     // Get the first and last rows of the sheet (determines the length of the parse) 
     for (int row = xlSheetFirstRow(sheet); row < xlSheetLastRow(sheet); ++row){ 
      // Get the first and last columns of the sheet (determines width of the parse) 
      for (int col = xlSheetFirstCol(sheet); col < xlSheetLastCol(sheet); ++col){ 
       // When reading the cell pull the type (bool, int, string...) 
       enum CellType cellType = xlSheetCellType(sheet, row, col); 
       FormatHandle format = xlSheetCellFormat(sheet, row, col); 
       NSLog(@"(row = %i, col = %i) = ", row, col); 

       // Determine if the cell has a formula or is a value w/ format 
       if (xlSheetIsFormula(sheet, row, col)){ 
        const char* s = xlSheetReadFormula(sheet, row, col, &format); 
        NSLog(@"%s %s", (s ? s : "null"), "[formula]"); 
       } else{ 
        // The cell is not a formula therefore print the value and type 
        switch(cellType){ 
         case CELLTYPE_EMPTY: NSLog(@"%s", "[empty]"); break; 
         case CELLTYPE_NUMBER:{ 
          double d = xlSheetReadNum(sheet, row, col, &format); 
          NSLog(@"%f %s", d, "[number]"); 
          break; 
         } 
         case CELLTYPE_STRING:{ 
          const char* s = xlSheetReadStr(sheet, row, col, &format); 
          NSLog(@"%s %s", (s ? s : "null"), "[string]"); 
          break; 
         } 
         case CELLTYPE_BOOLEAN:{ 
          bool b = xlSheetReadBool(sheet, row, col, &format); 
          NSLog(@"%s %s", (b ? "true" : "false"), "[boolean]"); 
          break; 
         } 
         case CELLTYPE_BLANK: NSLog(@"%s", "[blank]"); break; 
         case CELLTYPE_ERROR: NSLog(@"%s", "[error]"); break; 
        } 
       } 
       NSLog(@"\n"); 
      } 
     } 
    } 
} 

xlBookRelease(book);