0

使用适用于 IOS 的 libxl 示例项目,我试图在包中打开一个现有的 xls 文件

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

它总是返回 null,谁能告诉我哪里出错了,或者这是否可能。

有没有替代品,我花了好几个小时寻找,必须是IOS兼容的。

4

3 回答 3

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);
于 2014-02-23T23:16:05.900 回答
0

问题源于您使用无效文字来记录新创建的值(您没有指针)。%@ 是 ObjC 对象文字,因此您将要使用它cout

于 2012-11-25T09:19:28.827 回答
0

你在 sourceforge 上看过 libxls 吗?有一个专门针对 iOS 的 objc 框架。

于 2012-11-25T13:02:35.593 回答