Qt 之 excel格式文件

1. 导出excel格式文件

核心代码

1.1 run.pri
//CONFIG 配置
QT += axcontainer
1.2 CustomExportExcel.h
//引入头文件
#include <QObject>
#include <QAxObject>

class CustomExportExcel : public QAxObject
{
	Q_OBJECT
public:
	explicit CustomExportExcel (QObject *parent = 0);
	~CustomExportExcel ();
public:
	// 向excel单元格中写入数据
	void setCellValue(unit nRow, unit nCol, const QString &value);
	// 设置边框颜色
	void setCellcRectSize(unit nRow, unit nCol, unit nWidth, unit nHeight);
	// 设置无边框颜色
	void setCellNocRectSize(unit nRow, unit nCol, unit nWidth, unit nHeight);
	// 设置单元格对齐方式
	void setCellAlign(unit nRow, unit nCol, unit nRowAline, unit nColAline);
	// 设置单元格合并
	void setCellMerage(unit nRowStart, unit nColStart, unit nRowEnd, unit nColEnd);
	// 创建Excel表格并导出Excel
	void createExcelTable();
private:
	QAxObject *m_pApplication;
	QAxObject *m_pWorkBooks;
	QAxObject *m_pWorkBook;
	QAxObject *m_pWorkSheets;
	QAxObject *m_pWorkSheet;

	QString m_fileName;
	QAxObject *m_pRange = NULL;
};
1.3 CustomExportExcel.cpp
CustomExportExcel::CustomExportExcel (QObject *parent = 0)
	: QAxObject(parent)
	, m_pApplication(NULL)
	, m_pWorkBooks(NULL)
	, m_pWorkBook(NULL)
	, m_pWorkSheets(NULL)
	, m_pWorkSheet(NULL)
{

}
CustomExportExcel::~CustomExportExcel ()
{
	delete m_pApplication;
	m_pApplication = NULL;
}

void CustomExportExcel::setCellValue(unit nRow, unit nCol, const QString &value)
{
	m_pRange = m_pWorkSheet->querySubObject("Cells(int, int)", nRow, nCol);
	m_pRange->dynamicCall("Value", value);
}
void CustomExportExcel::setCellcRectSize(unit nRow, unit nCol, unit nWidth, unit nHeight)
{
	QAxObject *cell = m_pWorkSheet->querySubObject("Cells(int, int)", nRow, nCol);
	cell->setProperty("ColumnWidth", nWidth);// 设置单元格宽
	cell->setProperty("RowHeight", nHeight);// 设置单元格高
	// 设置边框
	QAxObject *borderColor = cell->querySubObject("Borders");
	borderColor->setProperty("Color", QColor(0,0,0));
}

void CustomExportExcel::setCellNocRectSize(unit nRow, unit nCol, unit nWidth, unit nHeight)
{
	QAxObject *cell = m_pWorkSheet->querySubObject("Cells(int, int)", nRow, nCol);
	cell->setProperty("ColumnWidth", nWidth);// 设置单元格宽
	cell->setProperty("RowHeight", nHeight);// 设置单元格高
}

void CustomExportExcel::setCellAlign(unit nRow, unit nCol, unit nRowAline, unit nColAline)
{
	QAxObject *cellAlign = m_pWorkSheet->querySubObject("Cells(int, int)", nRow, nCol);
	// 左对齐(xlLeft):-4131 居中(xlCenter):-4108 右对齐(xlRight):-4152
	cellAlign->setProperty("HorizontalAlignment", nRowAline);
	// 上对齐(xlTop):-4160 居中(xlCenter):-4108 下对齐(xlBottom):-4107
	cellAlign->setProperty("VerticalAlignment", nColAline);
}

void CustomExportExcel::setCellMerage(unit nRowStart, unit nColStart, unit nRowEnd, unit nColEnd)
{
	QString merageCell;
	merageCell.append(QString::number(nRowStart));//初始行
	merageCell.append(QChar(nColStart - 1 + 'A'));//初始列
	merageCell.append(":");
	merageCell.append(QString::number(nRowEnd));//终止行
	merageCell.append(QChar(nColEnd- 1 + 'A'));//终止列
	QAxObject *merageRange = m_pWorkSheet->querySubObject("Range(const QString&)",merageCell);
	merageRange->setProperty("HorizontalAlignment","xlCenter");
	merageRange->setProperty("VerticalAlignment","xlCenter");
	merageRange->setProperty("WrapText", true);// 文字换行
	merageRange->setProperty("merageCells", true);// 合并单元格
}

void CustomExportExcel::createExcelTable()
{
	// 创建要导出的文件格式
	QString fileName = QDialog::getSaveFileName(NULL, QObject::tr("Save File"),
	QStandardPaths::writeableLocation(QStandardPaths::DocumentsLocation),
	QObject::tr("Excel WorkSheet(*.xlsx *.xls)"));

	if (fileName  != NULL | m_fileName != "")
	{
		m_pApplication = new QAxObject;
		if (m_pApplication->setProperty("Excel.Application"))
		{
			m_pApplication->dynamicCall("SetVisible(bool)", "false");
			m_pApplication->setProperty("DisplayAlerts", false);
			m_pWorkBooks = m_pApplication->querySubObject("WorkBooks");
			m_pWorkBooks->dynamicCall("Add");
			m_pWorkBook = m_pApplication->querySubObject("ActiveWorkBook");
			m_pWorkSheets = m_pWorkBook ->querySubObject("Sheets");

			m_pWorkSheet = m_pWorkSheets->quertySubObject("Item(int)", 1);

//			QAxObject *userRange = m_pWorkSheet->querySubObject("UserdRange");
//			QAxObject *nRows = userRange->querySubObject("Rows");
//			QAxObject *nColumns = userRange->querySubObject("Columns");

			// 获取相应数据并将数据插入到表格中
			forint row = 0; row < 10; row++{
				forint col = 0; col < 10; col++{
					//setCellValue(row, col, data);
				}
			}
		}
	}
}

1.4 方法使用
// 在别的地方进行调用
#include "CustomExportExcel.h"

CustomExportExcel *pExcel = new CustomExportExcel;
pExcel->createExcel();

猜你喜欢

转载自blog.csdn.net/QIJINGBO123/article/details/87361933