libxl添加文字和图片测试

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值