• 利用basicExcel在qt中实现对xls文件的读取和另存为操作。


    项目中需要数据一定格式的xls文件,要求的xls文件的表头部分需要进行单元格的合并转换,包含中文。基于我的认知,有如下几种思路。

    1,xlslib:该插件用于xls文件的生成,支持单元格合并,并可以进行颜色等高级设置。以前有linux的应用经验,于是下载源码参考若干资料,终于qt5+vs2015的环境下编译成功,实现了xls文件的导出功能。但发现输出中文为乱码,尝试一些方法后仍然没有搞定,暂时放弃。

    2,basicExcel:可进行xls文件的读写操作,不支持单元格合并。因项目要求的表格表头固定,数据部分仅仅是填入不同的数字,支持中文。利用basicExcel读取满足格式要求的表格,在表格中写入数据后,再另存为,实现代码如下。

     1 errcode ExcelParse::xlsControl(const char *pathfile,QString outPutPath)
     2 {
     3     //1 路径中文处理
     4     QString cpath =  QString::fromUtf8(pathfile);
     5     //cpath.replace('/', '\');
     6     qDebug()<<"after replace : " <<cpath<<" "<<cpath.length();
     7     //输出文件调试信息
     8     char outbuf[255];
     9     utf8_unicode((unsigned char*)cpath.toUtf8().constData(), (unsigned char*)outbuf, -1);
    10     qDebug()<<"the outbuf " <<QTextCodec::codecForName("gbk")->toUnicode(outbuf);
    11 
    12     QString strT3 = QTextCodec::codecForName("UTF-8")->toUnicode(cpath.toUtf8());
    13     QByteArray gbkstr = QTextCodec::codecForName("GB18030")->fromUnicode(strT3);
    14 
    15     //2 文件打开
    16     if (parse.Load(gbkstr.constData()))
    17     {
    18         int sheetcnt = parse.GetTotalWorkSheets();
    19         qDebug()<<"sheet count"<<sheetcnt;
    20         char sheetname[255];
    21         QByteArray sheetgbkstr;
    22         //3 获取表单名称
    23         wchar_t wsheetname[255] = {0};
    24         if (!parse.GetSheetName(0, sheetname))
    25         {
    26             parse.GetSheetName(0, wsheetname);
    27             sheetgbkstr = QTextCodec::codecForName("GB18030")->fromUnicode((QChar*)&wsheetname[0], 255);
    28             qDebug()<<"sheet gbkstr"<<sheetgbkstr<<QString::fromWCharArray(wsheetname);
    29         }
    30         //4 打开表单
    31         BasicExcelWorksheet* sheet = parse.GetWorksheet(wsheetname);
    32         if(sheet==NULL)
    33         {
    34             qDebug()<<"sheet null"<<parse.GetTotalWorkSheets();
    35             return ERR_SMALL_WORKSEET;
    36         }
    37         //5 写入数据
    38         for(int i=0;i<10;i++)
    39         {
    40             QString cc=QString::number(i);//tableTitle.split(',').at(i).left(tableTitle.split(',').at(i).count()-1);
    41             wchar_t mm[100];
    42             mm[cc.toWCharArray(mm)]=0;
    43             sheet->Cell(7,i)->SetWString(mm);
    44         }
    45         //6 文件保存
    46         QTextCodec *code = QTextCodec::codecForName("gbk");
    47         std::string name = code->fromUnicode(outPutPath).data();
    48         qDebug()<<"xls save="<<parse.SaveAs(name.c_str());
    49         return ERR_NORMAL;
    50     }
    51     else
    52     {
    53         qDebug()<<"bad xls";
    54     }
    55     return ERR_BAD_FILE;
    56 }
    View Code
  • 相关阅读:
    用 Flash 制作留言板
    解决IIS5 HTTP500内部错误
    人体塑造教程+源文件+录象教程
    sql溢出攻击
    美化windows xp 完全教程
    简单手绘背景绘画过程树!
    今天把伊苏6 玩潼关了!庆祝一下!发点图给大家欣赏。
    Rundll.exe 命令详解
    终端服务的日志监控
    用Visual C#实现局域网点对点通讯
  • 原文地址:https://www.cnblogs.com/gethope5/p/14715035.html
Copyright © 2020-2023  润新知