效率比较慢 但是可以使用
#include "Transition.h"
#include <QFileDialog>
#include <QString>
#include <qDebug>
#include <QAxObject>
#include <QVariantList>
#include <QVariant>
#include <QTextStream>
Transition::Transition(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//connect()
}
void Transition::on_btn_xz_clicked()
{
filePath = QFileDialog::getOpenFileName(NULL, "配置文件", ".", "*.xl*");
ui.edit_path->setText(filePath);
}
void Transition::on_btn_zhdw_clicked()
{
if (filePath.isEmpty())
{
return ;
}
else
{
excel = new QAxObject("Excel.Application"); //创建Excel对象连接驱动
excel->dynamicCall("SetVisible(bool)", false); //ture的打开 false不打开Excel表
excel->setProperty("DisplayAlerts", false);
workbooks = excel->querySubObject("WorkBooks");
workbook = workbooks->querySubObject("Open(const QString&)", filePath); //打开指定Excel
worksheets = workbook->querySubObject("WorkSheets"); //当前excel对应的所有sheet集合
int sheet_count = worksheets->property("Count").toInt(); //获取sheet表的数量
for (int num = 1; num <= sheet_count-1; num++)
{
worksheet = worksheets->querySubObject("Item(int)", num); //获取第n个sheet表
usedrange = worksheet->querySubObject("Usedrange"); //获取权限
int iRow = usedrange->property("Row").toInt(); //数据起始行数
int iCol = usedrange->property("Column").toInt(); //列数
int intRow = usedrange->querySubObject("Rows")->property("Count").toInt(); //获取数据总行数
for (int i = iRow+1; i <= intRow; i++)// 逐行读取主表
{
QString id = worksheet->querySubObject("Cells(int,int)", i, 1)->dynamicCall(("Value2()")).value<QString>(); // 编号
QString name = worksheet->querySubObject("Cells(int,int)", i, 2)->dynamicCall(("Value2()")).value<QString>(); //名称
QString type = worksheet->querySubObject("Cells(int,int)", i, 3)->dynamicCall(("Value2()")).value<QString>(); //类型
QString min = worksheet->querySubObject("Cells(int,int)", i, 4)->dynamicCall(("Value2()")).value<QString>(); //最小值
QString max = worksheet->querySubObject("Cells(int,int)", i, 5)->dynamicCall(("Value2()")).value<QString>(); //最大值
QString site = worksheet->querySubObject("Cells(int,int)", i, 6)->dynamicCall(("Value2()")).value<QString>(); //地址
QString explain = worksheet->querySubObject("Cells(int,int)", i, 7)->dynamicCall(("Value2()")).value<QString>(); //说明
QString leixing = worksheet->querySubObject("Cells(int,int)", i, 8)->dynamicCall(("Value2()")).value<QString>(); //类型
QFile file;
file.setFileName("C:\\Users\\zq\\Desktop\\3.ini");//输出到指定文件
if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Append))
{
QTextStream stream(&file);
stream << id << "," << name << "," << type << "," << min << "," << max << "," << site << "," << explain << "," << leixing << "\n";
file.close();
}
}
}
}
workbooks->dynamicCall("close");
excel->dynamicCall("Quit()");
}