一 效果图
二 代码
2.1 头文件 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();//增加行数
void on_pushButton_2_clicked();//导出
void on_pushButton_3_clicked();//导入
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
2.2 核心代码 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QDebug>
#include <QTextEdit>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(8);//只设置列数,行数动态中增加
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"城市"<<"总云量"<<"降水量"<<"能见度"<<"温度"<<"湿度"<<"风向"<<"风速");
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);//设置表格选中单个单元格的方式
//ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止修改
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//单个选中和多个选中的设置
ui->tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
ui->tableWidget->horizontalHeader()->setDefaultSectionSize(70); //设置列宽
QFont font = ui->tableWidget->horizontalHeader()->font(); //先获取字体
font.setBold(true); //字体设置为粗体
ui->tableWidget->horizontalHeader()->setFont(font); //设置每一列的标题字体为粗体
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
int RowCont;
RowCont=ui->tableWidget->rowCount();
ui->tableWidget->insertRow(RowCont);//增加一行
}
void MainWindow::on_pushButton_2_clicked()
{
//QString filepath = QFileDialog::getSaveFileName(this, tr("保存文件"),QString(), tr("EXCEL files (*.xls);;HTML-Files (*.txt);;"));
QString filepath = "F:/1.xls";
if (!filepath.isEmpty())
{
int row = ui->tableWidget->rowCount();
int col = ui->tableWidget->columnCount();
QList<QString> list;
//添加列标题
QString HeaderRow;
for (int i = 0; i < col; i++)
{
HeaderRow.append(ui->tableWidget->horizontalHeaderItem(i)->text() + "\t");
}
list.push_back(HeaderRow);
for (int i = 0; i < row; i++)
{
QString rowStr = "";
for (int j = 0; j < col; j++){
if(ui->tableWidget->item(i,j)!=0)
{
rowStr += ui->tableWidget->item(i, j)->text() + "\t";
}
else
{
QMessageBox::information(0,tr("提示"),tr("增加的当前行数据为空!"));
break;
}
}
list.push_back(rowStr);
}
QTextEdit textEdit;
for (int i = 0; i < list.size(); i++)
{
textEdit.append(list.at(i));
}
QFile file(filepath);
if (file.open(QFile::WriteOnly | QIODevice::Text))
{
QTextStream ts(&file);
ts.setCodec("GB2312");//这个地方大家自己判断是否用“utf-8”
ts << textEdit.document()->toPlainText();
file.close();
}
//导出后将表格情况,这一步大家自己选择要不要
//ui->tableWidget->clearContents();
//ui->tableWidget->setRowCount(0);
}
}
void MainWindow::on_pushButton_3_clicked()
{
//QString fileName = QFileDialog::getOpenFileName(0, tr("导入文件"),QString(), tr("EXCEL files (*.xls);;HTML-Files (*.txt);;"));
QString fileName = "F:/1.xls";
if(!fileName.isEmpty())
{
QFile file;
file.setFileName(fileName);
//打开文件
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox msg;
msg.setText("打开文件失败!");
msg.exec();
}
else
{
if(!file.size())
{
QMessageBox::warning(0,tr("警告"),tr("文件大小为空!"),QMessageBox::Ok);
}
else
{
//将文件数据导入表格
int r_count = 0; //统计文件的行数
QStringList textList; //记录文件中每一行的数据
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
textList.append(line); //保存文件的数据
r_count++; //记录文件的行数 前两行为表头
}
file.close();
if(!textList.isEmpty() && r_count>1)
{
QStringList listColHeader = textList.at(1).split("\t");
QStringList listRowHeader = textList.at(0).split("\t");
int c_count = listRowHeader.count();
ui->tableWidget->clear();
ui->tableWidget->setRowCount(r_count-1); //前1行是行表头
ui->tableWidget->setColumnCount(c_count-1); //最后一行是“\t"
ui->tableWidget->setHorizontalHeaderLabels(listRowHeader);
ui->tableWidget->setVerticalHeaderLabels(listColHeader);
qDebug()<<"start importTable row count:::"<<r_count<<"col count:::"<<c_count;
for(int row = 0; row < r_count; row++)
{
QStringList tmpList;
tmpList = textList.at(row).split("\t");
for(int col = 0; col < c_count; col++)
{
QTableWidgetItem *item = new QTableWidgetItem(tmpList.at(col));
ui->tableWidget->setItem(row-1, col, item);
}
}
QMessageBox::information(0,tr("提示"),tr("导入成功!"));
}
else
{
QMessageBox::information(0,tr("提示"),tr("文件数据为空!"));
}
}
}
}
else
{
QMessageBox::warning(0,tr("警告"),tr("文件名未指定!"),QMessageBox::Ok);
}
}
参考文章:https://blog.csdn.net/sinat_30440627/article/details/53978580
https://blog.csdn.net/graceland525/article/details/45150489
https://blog.csdn.net/cwj066/article/details/83344705
https://blog.csdn.net/polokang/article/details/6696982