#include <iostream>
#include "libxl.h"
#include <stdio.h>
#include <string>
using namespace libxl;
using namespace std;
#define DEBUG_INFO(format,...) printf("%s:%d : " format "\n", \
__func__,__LINE__,##__VA_ARGS__)
//char *name = "my.xlsx";
void edit(char *file_name,int r,int c,char *content)
{
//string file_name = string("new.xlsx");
//char file_name[100] = "my.xlsx";
Book *book = xlCreateXMLBook();
if(book == NULL){
DEBUG_INFO("error");
return;
}
DEBUG_INFO("file_name.c_str()=%s",file_name);
//book->load(file_name.c_str());
book->load(file_name);
Sheet *sheet = book->getSheet(0);
if(sheet != NULL){
sheet->writeStr(r, c, content);
// book->save(file_name.c_str());
book->save(file_name);
}
book->release();
DEBUG_INFO("write end");
}
void create_new_file(char *file_name)
{
remove(file_name);
Book* book = xlCreateXMLBook();
if(book)
{
Font* boldFont = book->addFont();
boldFont->setBold();
Font* titleFont = book->addFont();
titleFont->setName("Arial Black");
titleFont->setSize(16);
Format* titleFormat = book->addFormat();
titleFormat->setFont(titleFont);
Format* headerFormat = book->addFormat();
headerFormat->setAlignH(ALIGNH_CENTER);
headerFormat->setBorder(BORDERSTYLE_THIN);
headerFormat->setFont(boldFont);
headerFormat->setFillPattern(FILLPATTERN_SOLID);
headerFormat->setPatternForegroundColor(COLOR_TAN);
Format* descriptionFormat = book->addFormat();
descriptionFormat->setBorderLeft(BORDERSTYLE_THIN);
Format* amountFormat = book->addFormat();
amountFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
amountFormat->setBorderLeft(BORDERSTYLE_THIN);
amountFormat->setBorderRight(BORDERSTYLE_THIN);
Format* totalLabelFormat = book->addFormat();
totalLabelFormat->setBorderTop(BORDERSTYLE_THIN);
totalLabelFormat->setAlignH(ALIGNH_RIGHT);
totalLabelFormat->setFont(boldFont);
Format* totalFormat = book->addFormat();
totalFormat->setNumFormat(NUMFORMAT_CURRENCY_NEGBRA);
totalFormat->setBorder(BORDERSTYLE_THIN);
totalFormat->setFont(boldFont);
totalFormat->setFillPattern(FILLPATTERN_SOLID);
totalFormat->setPatternForegroundColor(COLOR_YELLOW);
Format* signatureFormat = book->addFormat();
signatureFormat->setAlignH(ALIGNH_CENTER);
signatureFormat->setBorderTop(BORDERSTYLE_THIN);
Sheet* sheet = book->addSheet("config");
if(sheet)
{
sheet->setCol(1, 1, 4);
sheet->setCol(2, 2, 4);
}
if(book->save(file_name)) {
std::cout << "File "<<file_name<<" has been created." << std::endl;
}
book->release();
}
}
void add_bmp_test(char *file_name,int r,int c,char *bmp_name)
{
//string file_name = string("new.xlsx");
//char file_name[100] = "my.xlsx";
Book *book = xlCreateXMLBook();
if(book == NULL){
DEBUG_INFO("error");
return;
}
DEBUG_INFO("file_name.c_str()=%s",file_name);
//book->load(file_name.c_str());
book->load(file_name);
Sheet *sheet = book->getSheet(0);
if(sheet != NULL){
//sheet->writeStr(r, c, content);
//virtual void XLAPIENTRY setPicture(int row, int col, int pictureId, double scale = 1.0, int offset_x = 0, int offset_y = 0, Position pos = POSITION_MOVE_AND_SIZE) = 0;
int pictureId = book->addPicture(bmp_name);
cout << "pictureId = " << pictureId << endl;
sheet->setPicture(r,c,pictureId);
// book->save(file_name.c_str());
book->save(file_name);
}
book->release();
DEBUG_INFO("write end");
}
int main()
{
char buf[100];
char name[10] = "new.xlsx";
char file_name[20] = "24_bit.bmp";
create_new_file(name);
int r;
int c;
for(r = 0;r <= 3;r++){
for(c = 0;c <= 4;c++){
sprintf(buf,"r=%d,c=%d",r,c);
edit(name,r,c,buf);
}
}
add_bmp_test(name,5,5,(char*)"/big/libxl/src/libxl-4.0.4.0/examples/c++/big.png");
return 0;
}
libxl添加文字和图片测试
于 2022-12-05 17:05:47 首次发布