libxl能修改.xlsx文件,
但是有要求,这个文件必须是用xlsx文件创建的,且不能被windows的WPS修改,这是测试的时候遇到的问题。也不知道这些问题是不是都会遇到
然后先看看你这个代码问题出在哪里了,你这代码几乎没有问题,改完初始化函数就OK了:
void test_2()
{
//String file_name = "my.xlsx";
char file_name[100] = "my.xlsx";
Book *book = xlCreateXMLBook();//主要是改这里,其他的几乎都没动。
book->load(file_name);
Sheet *sheet = book->getSheet(0);
sheet->writeStr(5, 6, "World");
book->save(file_name);
book->release();
}
int main()
{
test_2();
return 0;
}
下面是C语言实现的。C语言也可以写。
char *file_name = "hello.xlsx";
void test_3()
{
BookHandle book = xlCreateXMLBook();
DEBUG_INFO("hello world book = %p\n",book);
if(book)
{
DEBUG_INFO("xlCreateBook ok");
if(xlBookLoad(book, file_name))
{
DEBUG_INFO("xlBookLoad ok");
SheetHandle sheet = xlBookGetSheet(book, 0);
if(sheet)
{
xlSheetWriteStr(sheet, 5,5 , "hello", 0);
xlSheetWriteStr(sheet, 5,6, "world", 0);
}else{
DEBUG_INFO("error");
exit(-1);
}
if(xlBookSave(book, file_name)){
DEBUG_INFO("save %s ok",file_name);
}else{
DEBUG_INFO("save %s error",file_name);
}
}else{
DEBUG_INFO("xlBookLoad error");
}
xlBookRelease(book);
}else{
DEBUG_INFO("xlCreateBook error");
}
}
int main()
{
test_3();
return 0;
}
再考虑你的代码:
string file_name = "/home/demo/Documents/test_libxl.xlsx";
Book *book = xlCreateBook();
book->load(file_name.c_str());
Sheet *sheet = book->getSheet(0);
sheet->writeStr(5, 6, "World");
book->save(file_name.c_str());
book->release();