QT下使用BasicExcel 并支持中文

来源:互联网 发布:淘宝假冒扣分 编辑:程序博客网 时间:2024/06/01 08:11

同事一个QT程序,需要导出excel表格。  但是导出xlsx时,要不就是响应慢,要不就是程序崩溃,而且电脑还必须安装MS office才行,不同的电脑现象还不一样。

看着一堆堆的QAxObject 代码,感觉头大。 所以就在网上搜索了BasicExcel,库文件也就两个文件,一个cpp一个hpp,给的DEMO看起来也比较简单,

虽然没有复杂的功能,但是感觉足够用了。 而且电脑上也不用安装什么插件。

下载地址:

http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-and-Write-to-Microsoft


PC环境:

MingwQT5


使用方法:

1. 把BasicExcel.hpp和BasicExcel.cpp拷贝到你的project中。

2. 包含头文件  并添加  usingnamespaceYExcel;

3. 自己改的QT例程代码, 支持中文路径和中文数值。(值测试了保存,没有测试打开和读取)

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,
            tr("另存为"),
            "",
            tr("Excel工作簿(*.xls)"));
        if (!fileName.isNull())
        {
            BasicExcel e;
            qDebug()<<"文件名为:"<<fileName;
            // Create a new workbook with 2 worksheets and write some contents.
            e.New(2);
            e.RenameWorksheet("Sheet1", "test1");
            BasicExcelWorksheet* sheet = e.GetWorksheet("test1");
            sheet->Rename(reinterpret_cast<const wchar_t *>(QString("表格1").utf16()));
            BasicExcelCell* cell;
            if (sheet)
            {
                for (size_t c=0; c<4; ++c)
                {
                    cell = sheet->Cell(0,c);
                    cell->Set((int)c);
                }
                cell = sheet->Cell(1,3);
                cell->SetDouble(3.141592654);
                sheet->Cell(1,4)->SetWString(reinterpret_cast<const wchar_t *>(QString("测试1").utf16()));
                sheet->Cell(2,0)->SetString("Test str2");
                sheet->Cell(2,5)->SetString("Test str1");
                sheet->Cell(4,0)->SetDouble(1.1);
                sheet->Cell(4,1)->SetDouble(2.2);
                sheet->Cell(4,2)->SetDouble(3.3);
                sheet->Cell(4,3)->SetDouble(4.4);
                sheet->Cell(4,4)->SetDouble(5.5);
                sheet->Cell(4,4)->EraseContents();
            }
            sheet = e.AddWorksheet(reinterpret_cast<const wchar_t *>(QString("表格2").utf16()), 1);
            sheet = e.GetWorksheet(1);
            if (sheet)
            {
                sheet->Cell(1,1)->SetDouble(1.1);
                sheet->Cell(2,2)->SetDouble(2.2);
                sheet->Cell(3,3)->SetDouble(3.3);
                sheet->Cell(4,4)->SetDouble(4.4);
                sheet->Cell(70,2)->SetDouble(5.5);
            }
            QTextCodec *code = QTextCodec::codecForName("gbk");
            std::string name = code->fromUnicode(fileName).data();
            e.SaveAs(name.c_str());
            qDebug()<<"保存成功。。";
        }
        else
        {
            //点的是取消
        }
}

代码注释:

BasicExcel支持uni编码,因此使用中文的时候,参数要使用wchar_t类型。

例如下面的代码,一个是中文,一个是英文。  使用的时候,看看对应的API即可。

sheet->Cell(1,4)->SetWString(reinterpret_cast<const wchar_t *>(QString("测试1").utf16()));
sheet->Cell(2,0)->SetString("Test str2");

reinterpret_cast<const wchar_t *>(QString("测试1").utf16())//是为了把QString 转为wchar_t。


刚创建的新表,会自动添加两个表,Sheet1和Sheet2。因为没有对应的API,所以先获取表test1,然后再重新命名为中文。

e.RenameWorksheet("Sheet1", "test1");
BasicExcelWorksheet* sheet = e.GetWorksheet("test1");
sheet->Rename(reinterpret_cast<const wchar_t *>(QString("表格1").utf16()));


参考资料:

http://blog.csdn.net/icyfox_bupt/archive/2011/05/23/6440874.aspx     博客简绍了BasicExcel的用法,里面有读取的功能。


推荐:

这个我没有测试过,QtXlx是专门给QT写的,功能和用法上应该会更好一些吧。

QtXlsx使用方法(强大的Excel)

http://blog.csdn.net/c3060911030/article/details/51560239


0 0
原创粉丝点击