zoukankan      html  css  js  c++  java
  • 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工程里的内容。

  • 相关阅读:
    【转载】要有一流的科研 也要有一流的教学(在2011年教师节复旦大学青年教师座谈会上的发言)
    【转载】谢启鸿老师访谈录之一(10级,撰稿人:侯灵子)
    【转载】谢启鸿老师访谈录之二(12级,撰稿人:陈筠臻)
    [问题2014S15] 解答
    [问题2014S14] 解答
    判断是否关注了微信公众号 subscribe 0=未关注 1=已关注
    PHP获取今天、昨天、明天的日期
    lnmp安装fileinfo扩展
    Linux kill和kill-9区别
    单列索引、多列索引和最左前缀原则
  • 原文地址:https://www.cnblogs.com/zhangnianyong/p/13869854.html
Copyright © 2011-2022 走看看