gpt4 book ai didi

c++ - 打开现有的 Excel 文件 libxl

转载 作者:行者123 更新时间:2023-11-28 07:52:13 28 4
gpt4 key购买 nike

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

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

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

是否有任何替代方案,我花了好几个小时寻找,必须与 IOS 兼容。

最佳答案

针对这个旧线程发帖以供引用,并专门回答使用 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);

关于c++ - 打开现有的 Excel 文件 libxl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549683/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com