需求:
工作中遇到一个需求,有两张表格,一个xlsx表,一个csv表格,格式如下:
以csv表格中船台标识为基础,读取xlsx中的数据,如果存在该MMSI则把船名写道csv中对应船名的后面,不存在的话,则添加进csv中,合并两个表格。由于表格数据非常多,有十几万个,所以只能通过程序判断。
提前声明:该代码仅供参考,速度很慢。建议用插件QtXlsxWriter来读写xlsx文件
代码:
pro文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
CONFIG += qaxcontainer
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void readXls(const QString& file_path);
void read_csv(const std::string& file_path);
void write_csv(const std::string& file_path);
void judge();
private:
Ui::Widget *ui;
std::vector<std::string> path_point;
std::vector<std::string> path_xlsx;
// QStringList str;
std::vector<std::string> path_point_new;
std::vector<std::string> path_xlsx_new;
QStringList str_new;
};
#endif // WIDGET_H