今天有个刚学开发的哥们问我,用libxl写问题件的时候,需要判断这个文件是否存在,不存在的话就创建,存在的话就直接写入。其实我对这个libxl也不怎么熟悉使用,后来我还是答应帮忙给他写各例子。
废话不多说,直接上代码:
头文件部分
#include "libxl.h"
#pragma comment(lib,"libxl.lib")
#include <iostream>
#include <sstream>
#include <io.h>
using namespace libxl;
代码部分:
const char* filePath = "G:\\一个大象的项目.xlsx";
//获得book
Book* book = xlCreateXMLBook();
book->setKey("Halil Kural", "windows-2723210a07c4e90162b26966a8jcdboe");//注册
//大象怎么放进冰箱
if (book)
{
if (access(filePath, 0) == -1)//判断有没有冰箱,如果没有冰箱
{
//1、买一个冰箱,打开第一个门
Sheet* sheet = book->addSheet("第一页", 0);
if (sheet)
{
//2、把大象放进去
sheet->writeStr(3, 3, "一只250斤的猴子");
//3、把冰箱门关上
book->save(filePath);
}
}
else
{
//家里有冰箱的情况
if (book->load(filePath)) //1、找到这个冰箱
{
Sheet* sheet = book->getSheet(0);//2、打开第一个门
if (sheet)
{
//3、放猴子
sheet->writeStr(4, 3, "尼玛,搞了半天,终于放进去了");//调用规则:writeStr(行,列,内容);
}
//4、关门
book->save(filePath);
}
}
book->release();
}