QT文件保存--excel、txt
程序员文章站
2022-03-31 19:17:59
...
对excel数据保存,一下为部分代码:
void savedatatoexcel(QString &fileName, QString &ss)
{
newExcel(fileName);
setCellValue(j - y + 1, i - x + 1, ss);
saveExcel(fileName);
freeExcel();
}
void Thread::newExcel(const QString &fileName)
{
HRESULT r = OleInitialize(0);
CoInitialize(0);
if (r != S_OK && r != S_FALSE) {
qWarning("Qt: Could not initialize OLE (error %x)\n", (unsigned int)r);
}
pApplication = new QAxObject("Excel.Application");
if (pApplication == NULL) {
qWarning("pApplication\n"); return;
}
pApplication->dynamicCall("SetVisible(bool)", false); //false不显示窗体
pApplication->setProperty("DisplayAlerts", false); //不显示任何警告信息。
pWorkBooks = pApplication->querySubObject("Workbooks");
QFile file(fileName);
if (file.exists())
{
pWorkBook = pWorkBooks->querySubObject("Open(const QString &)", fileName);
}
else
{
pWorkBooks->dynamicCall("Add");
pWorkBook = pApplication->querySubObject("ActiveWorkBook");
}
pSheets = pWorkBook->querySubObject("Sheets");
pSheet = pSheets->querySubObject("Item(int)", 1);
}
void Thread::appendSheet(const QString &sheetName, int cnt)
{
QAxObject *pLastSheet = pSheets->querySubObject("Item(int)", cnt);
pSheets->querySubObject("Add(QVariant)", pLastSheet->asVariant());
pSheet = pSheets->querySubObject("Item(int)", cnt);
pLastSheet->dynamicCall("Move(QVariant)", pSheet->asVariant());
pSheet->setProperty("Name", sheetName);
}
void Thread::setCellValue(int row, int column, const QString &value)
{
QAxObject *pRange = pSheet->querySubObject("Cells(int,int)", row, column);
pRange->dynamicCall("Value", value);
QAxObject *interior = pRange->querySubObject("Interior");
//设置单元格字体颜色
interior->setProperty("Color", QColor(cl.r, cl.g, cl.b));
}
void Thread::saveExcel(const QString &fileName)
{
pWorkBook->dynamicCall("SaveAs(const QString &)",
QDir::toNativeSeparators(fileName));
}
void Thread::freeExcel()
{
if (pApplication != NULL)
{
pApplication->dynamicCall("Quit()");
delete pApplication;
pApplication = NULL;
}
emit over();
}
对txt数据保存,一下为部分代码:
#include <iostream>
#include <QVector>
#include <string>
#include <fstream>
using namespace std;
void savetxt(QVector<double> vec,string sr)
{
//将最终结果保存为xml或txt文档
ofstream outfile;
outfile.open(sr);
for (int i = 0; i < vec.size(); i++)
{
outfile << vec[i] << endl;
}
outfile.close();
}
以及涉及一些文件名和系统时间命名规则,部分代码如下:
char Outname[200];
QDateTime dt;
QTime time;
QDate date;
dt.setTime(time.currentTime());
dt.setDate(date.currentDate());
QString qcurrentDate = dt.toString("yyyy.MM.dd.hh.mm.ss");
sprintf(Outname, "%s.xlsx", qcurrentDate.toLatin1().data());
QString vbvb = QString("%1").arg(Outname);
//其中前两个为参数,可随实际更改
QString fileNametmp = telecentricity1 + telecentricitypara1 + vbvb;
上一篇: Qt之QListView使用
下一篇: C++ 布尔类型和引用的用法详解
推荐阅读
-
python将pandas datarame保存为txt文件的实例
-
使用Python在Excel中嵌入附件(txt文件,zip压缩包)对象
-
详解使用Node.js 将txt文件转为Excel文件
-
python操作excel文件并输出txt文件的实例
-
Asp.net实现直接在浏览器预览Word、Excel、PDF、Txt文件(附源码)
-
Python将文本去空格并保存到txt文件中的实例
-
python实现Excel文件转换为TXT文件
-
laravel excel 上传文件保存到本地服务器功能
-
将TXT文件中的内容正确导入到Excel表格中不混乱的方法
-
python 读取txt中每行数据,并且保存到excel中的实例