首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开现有Excel文件libxl

打开现有Excel文件libxl
EN

Stack Overflow用户
提问于 2012-11-25 17:11:50
回答 3查看 3.3K关注 1票数 0

使用IOS的libxl示例项目,我尝试打开包中现有的xls文件

代码语言:javascript
复制
BookHandle book = xlCreateBook();
xlBookLoad(book,"Beta-1.xls");    
SheetHandle sheet = xlBookGetSheet(book,0);
NSLog(@"%@",sheet);

它总是返回null,有人能告诉我哪里出错了吗,或者这是不是可能的。

有没有其他选择,我花了好几个小时寻找,必须是IOS兼容。

EN

回答 3

Stack Overflow用户

发布于 2014-02-24 07:16:06

在这个旧线程上张贴以供参考,并具体回答使用LibXL读取XLS或XLSX格式文件的问题。下面的代码演示了如何使用LibXL读取excel文件,并打印内容(值)和类型(数字、字符串、布尔值...)每个细胞的。

代码语言:javascript
复制
// 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);
票数 1
EN

Stack Overflow用户

发布于 2012-11-25 17:19:29

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

票数 0
EN

Stack Overflow用户

发布于 2012-11-25 21:02:36

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13549683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档