xlslib开源库的使用

Posted zhangnianyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xlslib开源库的使用相关的知识,希望对你有一定的参考价值。

1、为了更好地使用xlslib开源库,本篇对其作了一个简单的封装,类为QMyExcel

  头文件:qmyexcel.h

  #ifndef QMYEXCEL_H
  #define QMYEXCEL_H

  #include <stdio.h>
  #include <string.h>
  #include <wchar.h>
  #include <stdbool.h>
  #include <errno.h>
  #include <iostream>
  #include <QObject>

  #include "xlslib.h"

  using namespace std;
  using namespace xlslib_core;

  class QMyExcel : public QObject
  {
  Q_OBJECT
  public:
    QMyExcel(QObject *parent = 0);
    ~QMyExcel(){}

  public:
  worksheet *createSheet(std::string sheetname);
  void SetCellNumber(worksheet *ws,unsigned32_t row,unsigned32_t col,unsigned32_t numval,std::string fontname,unsigned16_t fontheight,boldness_option_t fontboldness);
  void SetCellText(worksheet *ws,unsigned32_t row,unsigned32_t col,std::string text,std::string fontname,unsigned16_t fontheight,boldness_option_t fontboldness);
  void MergeCells(worksheet *ws,unsigned32_t first_row,unsigned32_t first_col,unsigned32_t last_row, unsigned32_t last_col);
  void Setcolwidth(worksheet *ws,unsigned32_t col,unsigned16_t width);
  void Setrowheight(worksheet *ws,unsigned32_t row,unsigned16_t heightInTwips);
  void SetCellAlign(worksheet *ws,unsigned32_t row,unsigned32_t col,halign_option_t ha_option,valign_option_t va_option);
  void SetCellBorder(worksheet *ws,unsigned32_t row,unsigned32_t col,border_style_t style,color_name_t color,bool up,bool down,bool left,bool right);
  void SetCellBgColor(worksheet *ws,unsigned32_t row,unsigned32_t col,color_name_t color);
  int SaveExcelFile(std::string filePath);

  private:
  workbook m_wb;
  };

#endif // QMYEXCEL_H

 

  源文件:qmyexcel.cpp

  #include "qmyexcel.h"

  QMyExcel::QMyExcel(QObject *parent) :
  QObject(parent)
  {
  }

  worksheet *QMyExcel::createSheet(std::string sheetname)
  {
    worksheet *ws = m_wb.sheet(sheetname);
    return ws;
  }

  void QMyExcel::MergeCells(worksheet *ws,unsigned32_t first_row,unsigned32_t first_col,unsigned32_t last_row,unsigned32_t last_col)
  {
    if(ws != NULL)
    {
      ws->merge(first_row,first_col,last_row,last_col);
    }
  }

  void QMyExcel::SetCellAlign(worksheet *ws,unsigned32_t row,unsigned32_t col,halign_option_t ha_option,valign_option_t va_option)
  {
    if(ws != NULL)
    {
      cell_t *pcell = ws->FindCellOrMakeBlank(row, col);
      pcell->halign(ha_option);
      pcell->valign(va_option);
    }
  }

  void QMyExcel::SetCellBgColor(worksheet *ws,unsigned32_t row,unsigned32_t col,color_name_t color)
  {
    if(ws != NULL)
    {
      cell_t *pcell = ws->FindCellOrMakeBlank(row,col);
      pcell->fillstyle(FILL_SOLID);
      pcell->fillbgcolor(color);
      pcell->fillfgcolor(color);
    }
  }

  void QMyExcel::SetCellBorder(worksheet *ws,unsigned32_t row,unsigned32_t col,border_style_t style,color_name_t color,bool up,bool down,bool left,bool right)
  {
    if(ws != NULL)
    {
      cell_t *pcell = ws->FindCellOrMakeBlank(row, col);
      if(up)
      {
        pcell->borderstyle(BORDER_TOP, style);
        pcell->bordercolor(BORDER_TOP, color);
      }
      if(down)
      {
        pcell->borderstyle(BORDER_BOTTOM, style);
        pcell->bordercolor(BORDER_BOTTOM, color);
      }
      if(left)
      {
        pcell->borderstyle(BORDER_LEFT, style);
        pcell->bordercolor(BORDER_LEFT, color);
      }
      if(right)
      {
        pcell->borderstyle(BORDER_RIGHT, style);
        pcell->bordercolor(BORDER_RIGHT, color);
      }
    }
  }

  void QMyExcel::SetCellNumber(worksheet *ws,unsigned32_t row,unsigned32_t col,unsigned32_t numval,std::string fontname,unsigned16_t fontheight,boldness_option_t fontboldness)
  {
    if(ws != NULL)
    {
      xf_t *pxf = m_wb.xformat();
      font_t *pfont = m_wb.font(fontname);
      pfont->SetHeight(20*fontheight);//fontheight号字体
      pfont->SetBoldStyle(fontboldness);
      pxf->SetFont(pfont);
      ws->number(row,col,numval,pxf);
    }
  }

  void QMyExcel::SetCellText(worksheet *ws,unsigned32_t row,unsigned32_t col,std::string text,std::string fontname,unsigned16_t fontheight,boldness_option_t fontboldness)
  {
    if(ws != NULL)
    {
      xf_t *pxf = m_wb.xformat();
      font_t *pfont = m_wb.font(fontname);
      pfont->SetHeight(20*fontheight);//fontheight号字体
      pfont->SetBoldStyle(fontboldness);
      pxf->SetFont(pfont);
      ws->label(row,col,text,pxf);
    }
  }

  void QMyExcel::Setcolwidth(worksheet *ws, unsigned32_t col, unsigned16_t width)
  {
    if(ws != NULL)
    {
      if(0 != width)
        ws->colwidth(col,width);
    }
  }

  void QMyExcel::Setrowheight(worksheet *ws, unsigned32_t row, unsigned16_t heightInTwips)
  {
    if(ws != NULL)
    {
      if(0 != heightInTwips)
        ws->rowheight(row, heightInTwips);
    }
  }

 

  int QMyExcel::SaveExcelFile(std::string filePath)
  {
    int err = m_wb.Dump(filePath);
    if(err != 0)
    {
      fprintf(stderr, "Dump xls file failed: I/O failure %d. ", err);
      return -1;
    }
    return 0;
  }

2、qmyexcel类的使用

  QMyExcel excel;
  worksheet *wscar = excel.createSheet("cars");
  worksheet *wsname = excel.createSheet("name");
  excel.MergeCells(wscar,0,0,0,4);

  excel.SetCellText(wscar,0,0,"汽车信息表","Microsoft Sans Serif",16,BOLDNESS_BOLD);
  excel.SetCellAlign(wscar,0,0,HALIGN_CENTER,VALIGN_CENTER);
  excel.Setrowheight(wscar,0,500);
  excel.SetCellNumber(wscar,0,1,65535,"Dotum",12,BOLDNESS_NORMAL);
  excel.SetCellText(wscar,1,1,"Das","Arial Black",12,BOLDNESS_BOLD);
  excel.SetCellBgColor(wscar,2,1,CLR_DARK_BLUE);
  excel.SetCellBorder(wscar,2,2,BORDER_MEDIUM, CLR_DARK_RED,true,true,true,true);

  excel.SetCellText(wsname,0,0,"PEPLE NAME","Microsoft Sans Serif",12,BOLDNESS_BOLD);
  excel.SetCellAlign(wsname,0,0,HALIGN_CENTER,VALIGN_CENTER);
  excel.SetCellNumber(wsname,0,1,12300,"Dotum",12,BOLDNESS_NORMAL);
  excel.SetCellText(wsname,1,1,"Das","Arial",10,BOLDNESS_NORMAL);
  excel.SetCellBgColor(wsname,2,1,CLR_DARK_BLUE);
  excel.SetCellBorder(wsname,2,2,BORDER_THIN,CLR_DARK_RED,true,true,true,true);
  excel.Setcolwidth(wsname,0,5500);

  int ret = excel.SaveExcelFile("mytest.xls");
  qDebug()<<ret;

3、出于对源著的尊重,本文有引用联合开发网作者:淡足迹发布的xlslib_qtest工程里的内容。





























































































































































以上是关于xlslib开源库的使用的主要内容,如果未能解决你的问题,请参考以下文章

linux 上使用libxls读和使用xlslib写excel的方法简介

xlslib and libxls

寻找开源C++操作Excel表格库?

Android常用开源库的使用——————————————LitePal的使用方法

tiny-cnn开源库的使用(MNIST)

开源UI类库的使用