2012-11-25 175 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);