读取excel函数
void Widget::getExcelContent(QVector<QVector<QString>> &map)
{
QAxObject *excel = NULL;
QAxObject *workbooks = NULL;
QAxObject *workbook = NULL;
QString filePath = QFileDialog::getOpenFileName(
this, QStringLiteral("选择Excel文件"),"",
QStringLiteral("Excel file(*.xls *.xlsx)"));
if(filePath.isEmpty())return;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
excel = new QAxObject("Excel.Application");
if(!excel)
{
qDebug()<<"EXCEL 对象丢失!";
}
workbooks = excel->querySubObject("Workbooks");//所有excel文件
if(0==workbooks)
{
qDebug()<<"Kong000000000";
return;
}
workbook = workbooks->querySubObject("Open (const QString &)",filePath);//按路径获取文件
if(0==workbook)
{
qDebug()<<"workbook000000000";
return;
}
QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", 1);//读取第一个表
QAxObject *usedRange = worksheet->querySubObject("UsedRange");//有数据的矩形区域
QAxObject * rows = usedRange->querySubObject("Rows");
QAxObject * columns = usedRange->querySubObject("Columns");
intRows = rows->property("Count").toInt();
intCols = columns->property("Count").toInt();
//qDebug()<<"行数:"<<intRows;
//qDebug()<<"列数:"<<intCols;
QVariant var = usedRange->dynamicCall("Value");
foreach (QVariant varRow, var.toList())
{
QVector<QString> vecDataRow;
foreach(QVariant var, varRow.toList())
{
vecDataRow.push_back(var.toString());
}
map.push_back(vecDataRow);
}
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
if(excel)
{
delete excel;
excel = NULL;
}
}
点击按钮调用getExcelContent函数
首先在widget.h中定义全局变量,并设置excel和Qchart所用的头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QtCharts>
#include<QChartView>
#include<QSplineSeries>
#include<QLineSeries>
#include<QPushButton>
#include<QFileDialog>
#include<QAxObject>
#include<QList>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
QVector<QVector<QString>> exc_res;
int intRows;
int intCols;
//QMap<QString, float> exc_res;
void getExcelContent(QVector<QVector<QString>>& result);
private:
Ui::Widget *ui;
QChart *mChart;
};
#endif // WIDGET_H
widget.cpp调用
connect(ui->btn_open, &QPushButton::clicked, [=](){
getExcelContent(exc_res);//exc_res中数据即为excel内容
});
对excel数据画图
首先在界面拖入wiget控件,右键控件提升,将类名称写为QChartView
此时运行程序,运行程序会报错,点击问题提示会进入ui mainwindow.h中
添加下面两句到ui widget.h中
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
点击按钮画图到指定控件
connect(ui->btn_show, &QPushButton::clicked, [=](){
mChart = new QChart();
//qDebug()<<"intCols----"<<intCols;
//qDebug()<<"intRows----"<<intRows;
for(int j=0; j<intCols; ++j)
{
QSplineSeries *splineSeries = new QSplineSeries();
for(int i=0; i<intRows; ++i)
{
//qDebug()<<"j="<<j<<" , i="<<i;
splineSeries->append(i,exc_res[i][j].toDouble());
}
mChart->addSeries(splineSeries);
}
mChart->setTheme(QChart::ChartThemeLight);
mChart->legend()->hide();
mChart->setTitle("曲线图图表标题"); // 设置标题
mChart->createDefaultAxes(); // 基于已添加到图表中的series为图表创建轴。以前添加到图表中的任何轴都将被删除。
//mChart->axes(Qt::Vertical).first()->setRange(0, 1); // 设置Y轴的范围
ui->widget->setRenderHint(QPainter::Antialiasing); // 设置抗锯齿
ui->widget->setChart(mChart);//拖入界面的控件名为widget
});