Qt 中读写Excel

开发时,需要将数据写入到excel中,做成一个报表,以便于查看,网上看了很多的例子,自己也仿照着写了个。

首先需要安装qtxlslwriter 这个库,下载地址:

https://github.com/leolin0518/QtXlsxWriter

环境的搭建这儿不细说,需要的可以网上找,有很多。

主要做的是读和写,方便查看。

实现的是写的功能,具体代码如下:

Qt版本为:Qt5.5.1MSVC2010

写入excel

void c_excel::Slot_Write_Excel_File()
{
	QXlsx::Document xlsx;
	QXlsx::Format format;   
	format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
	format.setVerticalAlignment(QXlsx::Format::AlignVCenter);
	xlsx.setColumnWidth(3,60);
	xlsx.write("A1",tr("Device"),format);
	xlsx.write("B1",tr("State"),format);
	xlsx.write("C1",tr("Question"),format);
	xlsx.write("D1",tr("Time"),format);

	int nCount = EXCEL_COUNT;
	static int gnCount = 0;
	for (int i = 0; i < nCount; i++)
	{
		char buff[100];
		memset(buff,0,sizeof(buff));
		sprintf(buff,"IPC%d",++ gnCount);
		xlsx.write(i+2,1,buff,format);
		memset(buff,0,sizeof(buff));
		sprintf(buff,"State: %d",gnCount);
		xlsx.write(i+2,2,buff,format);
		memset(buff,0,sizeof(buff));

		sprintf(buff,"1111111111111111111111111111Question: %d",gnCount);
		xlsx.write(i+2,3,buff,format);
		memset(buff,0,sizeof(buff));
		sprintf(buff,"111111111111111111Time: %d",gnCount);
		xlsx.write(i+2,4,buff,format);

	}
	// 添加Sheet页面
	xlsx.addSheet();
	xlsx.addSheet();
	xlsx.selectSheet("Sheet2"); // 选择操作页面
	for (int i = 0; i < nCount; i++)
	{
		char buff[100];
		memset(buff,0,sizeof(buff));
		sprintf(buff,"IPC%d",++ gnCount);
		xlsx.write(i+2,1,buff,format);
		memset(buff,0,sizeof(buff));
		sprintf(buff,"State: %d",gnCount);
		xlsx.write(i+2,2,buff,format);
		memset(buff,0,sizeof(buff));

		sprintf(buff,"1111111111111111111111111111Question: %d",gnCount);
		xlsx.write(i+2,3,buff,format);
		memset(buff,0,sizeof(buff));
		sprintf(buff,"111111111111111111Time: %d",gnCount);
		xlsx.write(i+2,4,buff,format);

	}
	bool bret = xlsx.saveAs(m_qstrfilename);
	QXlsx::Document xlsx2(m_qstrfilename);/*复制A到B*/
	xlsx2.saveAs("B.xlsx");
	if (bret)
	{
		emit signal_save_success();
	}
}

读取excel

void c_excel::Slot_Read_Excel_File()
{
	QXlsx::Document xlsx(m_qstrfilename);
	QXlsx::CellRange range = xlsx.dimension();
	int nrow = range.lastRow();
	//int ncol = range.lastColumn();

	for (int i = 0; i < nrow - 1; i++)
	{
		QString qstrip = xlsx.read(i+2,1).toString();
		QString qstrrtsp1 = xlsx.read(i+2,2).toString();
		QString qstrrtsp2 = xlsx.read(i+2,3).toString();
		QString qstruser = xlsx.read(i+2,4).toString();

		if (qstrip.isEmpty())
		{
			continue;
		}

	}
}


widget.cpp
#include "widget.h"

Widget::Widget(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_excel = new c_excel;
	m_excel->moveToThread(&m_thread);
	connect(ui.readBtn,SIGNAL(clicked()),m_excel,SLOT(Slot_Read_Excel_File()));
	//connect(ui.writeBtn,SIGNAL(clicked()),m_excel,SLOT(Slot_Write_Excel_File()));
	connect(ui.writeBtn,SIGNAL(clicked()),this,SLOT(Slot_Set_File_Name()));
	
	m_thread.start();
}

Widget::~Widget()
{
	m_thread.quit();
	m_thread.wait();
}

void Widget::Slot_Set_File_Name()
{
	QString fileName = QFileDialog::getSaveFileName(NULL, tr("Excel file"),".",tr("Excel Files (*.xlsx)"));
	m_excel->SetFileName(fileName);
}

代码下载地址:点击打开链接

编译好的QtXlsx库下载地址:点击打开链接

你可能感兴趣的:(Qt-C++,开发工具)