打开现有的 Excel 文件 libxl
Opening existing Excel file libxl
使用 libxl for IOS 的示例项目,我正在尝试在捆绑包中打开一个现有的 xls 文件
BookHandle book = xlCreateBook();
xlBookLoad(book,"Beta-1.xls");
SheetHandle sheet = xlBookGetSheet(book,0);
NSLog(@"%@",sheet);
它总是返回 null,谁能告诉我哪里出错了,或者这甚至可能。
有没有其他选择,我花了几个小时寻找,必须与IOS兼容。
针对这个旧线程发布以供参考,并专门回答使用 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);
问题源于您使用无效的文本来记录新创建的值(您没有指针)。 %@ 是 ObjC 对象文字,因此您需要改用cout
。
你有没有在sourceforge上看过libxls?有一个专门针对iOS的objc框架。
相关文章:
- 如何从excel文件中读取数据并存储到变量中?
- 错误写入Excel文件-C++
- C++使用 adodb 在 64 位 Windows 7 中读取 Excel 文件?
- 在C J2ME中读取Excel文件,并从ITQ获取数据
- 使用C 在Excel文件中阅读的建议
- 打开现有的 Excel 文件 libxl
- 使用 MFC 时导出到 Excel 文件
- C 如何通过同一MFC应用程序通过System Command打开Excel文件后关闭CMD EXE
- 使用visualstudio在c++中读取excel文件
- 如何用C++将数据写入Excel文件
- 用c++读取excel文件(通用方向)
- 如何在 C++ 上使用互操作创建 Excel 文件
- 如何创建-编辑-删除 excel 文件
- 在C C++中比较和匹配单词/excel文件中的数字
- C#程序,用于使用关闭前事件处理程序自动处理excel,而不保存编辑后的excel文件
- 用c++读写excel文件
- 使用OleContainer保存时损坏的Excel文件
- 在Windows应用程序中从rdl/rdlc文件中写入Excel文件
- 在mac上使用system()打开excel文件
- Libxl从excel文件中读取整数