使用libxl的示例项目进行IOS,我试图在包中打开一个现有的xls文件
BookHandle book = xlCreateBook();
xlBookLoad(book,"Beta-1.xls");
SheetHandle sheet = xlBookGetSheet(book,0);
NSLog(@"%@",sheet);
它总是返回null,有人能告诉我哪里出错了,或者这是否可能。
有没有其他选择,我花了好几个小时看,必须与IOS兼容。
答案 0 :(得分:1)
针对此旧线程发布以供参考,并具体回答使用LibXL读取XLS或XLSX格式文件的问题。下面的代码演示了如何使用LibXL读取excel文件并打印每个单元格的内容(值)和类型(数字,字符串,bool ...)。
// 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 :(得分:0)
问题源于您使用无效文字来记录新创建的值(您没有指针)。 %@是ObjC对象文字,因此您将要使用cout
代替。
答案 2 :(得分:0)
你看过sourceforge上的libxls吗?它有专门针对iOS的objc框架。