Qt操作Excel类

基于某文章(原文找不到了), 进行小修改调整

.h

#ifndef QEXCEL_H  
#define QEXCEL_H  

#include   
#include   

class QAxObject;

class ExcelEngine final : public QObject
{
public:
	ExcelEngine(QString strPath, QObject *parent = 0);
	~ExcelEngine();

public:

	/*****************************************************/
	/* 工作表                                                                 */
	/****************************************************/
	bool selectSheet(const QString& sheetName);
	bool selectSheet(int sheetIndex);

	void deleteSheet(const QString& sheetName);
	void deleteSheet(int sheetIndex);

	// 插入工作表
	bool insertSheet(QString sheetName, bool bAtLast = true);

	// 向前/向后移动表
	bool moveSheetForward(int nSheetIndex);
	bool moveSheetBack(int nSheetIndex);

	//获取工作表数目
	int getSheetsCount();

	//获取工作表名称 (在 selectSheet() 之后才可调用)
	QString getSheetName();
	QString getSheetName(int sheetIndex);

	/*****************************************************/
	/* 单元格                                                                 */
	/****************************************************/

	// 设置单元格内容
	void setCell(int row, int column, const QString& value);
	void setCell(const QString& cell, const QString& value);

	void setCell(int row, int column, const int& value);
	void setCell(const QString& cell, const int& value);
	
	void setCell(int row, int column, const float& value);
	void setCell(const QString& cell, const float& value);

	//根据行列值获取单元格值, 如: 3行,5列
	QString getCell(int row, int column);
	//根据行列编号获取单元格值, 如: "F6"
	QString getCell(const QString& strCell);

	QVariant getCellValue(int row, int column);
	QVariant getCellValue(const QString& strCell);

	//合并单元格. range 例如 "A5:C7"
	void mergeCells(const QString& range);
	void mergeCells(int topLeftRow, int topLeftColumn, int bottomRightRow, int bottomRightColumn);

	//清空单元格内容
	void clearCell(int row, int column);
	void clearCell(const QString& cell);

	
	//获取行列数
	int getRowsCount();
	int getColumnsCount();

	// 在指定行后插入一空行
	bool insertEmptyRow(int nInsertRow = 1);
	bool insertEmptyColumn(int nInsertCol = 1);

	// 删除指定行
	void deleteRow(int nRowNum);
	bool deleteColumn(int nColumnNum);

	// 复制行/列并插入
	bool copyRowThenInsert(int nCopyRow, int nInsertRow);
	bool copyColumnThenInsert(int nCopyCol, int nInsertCol);

	// 复制行/列并粘贴
	bool copyRowThenPaste(int nCopyRow, int nInsertRow);
	bool copyColumnThenPaste(int nCopyCol, int nInsertCol);

	// 区域复制插入 如: copyBlockThenPaste("A1:D6", "A8");
	bool copyBlockThenInsert(const QString& strCellCopy, const QString& strCellPast);
	bool copyBlockThenPaste(const QString& strCellCopy, const QString& strCellPast);

	bool multiSelectCopy(const QString &strCellCopy, const QString &strCellPaste);

	// 单元格求和, 将要累加的单元格进行累加后,存入 strCellResult
	bool  sumFunc(const QString& strCell, const QString& strCellResult);
	bool  sumFunc(const QStringList& strCells, const QString& strCellResult);

	// 设置单元格执行的公式
	bool setCellFormula(const QString& strCell, const QString& strFormula);

	/*****************************************************/
	/* 布局格式                                                                 */
	/****************************************************/
	void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);
	void setColumnWidth(int column, int width);
	void setRowHeight(int row, int height);
	void setCellTextCenter(int row, int column);
	void setCellTextCenter(const QString& cell);
	void setCellTextWrap(int row, int column, bool isWrap);
	void setCellTextWrap(const QString& cell, bool isWrap);
	void setAutoFitRow(int row);
	void mergeSerialSameCellsInAColumn(int column, int topRow);
	int getUsedRowsCount();

	void setCellFontBold(int row, int column, bool isBold);
	void setCellFontBold(const QString& cell, bool isBold);

	void setCellFontSize(int row, int column, int size);
	void setCellFontSize(const QString& cell, int size);

	/*****************************************************/
	/* 文件                                                                      */
	/*****************************************************/
	void save();
	void saveAs(const QString& strPath);
	void close();

private:
	QAxObject * getWorkBooks();
	QAxObject * getWorkBook();
	QAxObject * getWorkSheets();
	QAxObject * getWorkSheet();

private:
	QAxObject * m_pExcel = nullptr;

	QAxObject * m_pWorkBooks = nullptr;
	QAxObject * m_pWorkBook = nullptr;

	QAxObject * m_pWorkSheets = nullptr;
	QAxObject * m_pWorkSheet = nullptr;

	QAxObject* m_pAxObj = nullptr;
};

#endif  

.cpp

#include "ExcelEngine.h"


#include   
#include   
//#include 
#include   
#include   

// 清理指针
#define DEL_OBJ(X) { if (X) { delete X; X=nullptr;} }

#include 
ExcelEngine::ExcelEngine(QString strPath, QObject *parent)
{
	m_pExcel = new QAxObject("Excel.Application", parent);
	m_pExcel->setProperty("Visible", true);	// 是否可见
	m_pWorkBooks = m_pExcel->querySubObject("Workbooks");

	QFile file(strPath);
	if (file.exists())
	{
		m_pWorkBooks->dynamicCall("Open(const QString&)", strPath);
		m_pWorkBook = m_pExcel->querySubObject("ActiveWorkBook");
		m_pWorkSheets = m_pWorkBook->querySubObject("WorkSheets");
	}
	else
	{
		//	m_pWorkBooks->dynamicCall("Add");
		//	m_pWorkBook = m_pExcel->querySubObject("ActiveWorkBook");
		//	m_pWorkBook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(strPath));
		//	m_pWorkSheets = m_pWorkBook->querySubObject("WorkSheets");
		return;
	}
	selectSheet(1);
}

ExcelEngine::~ExcelEngine()
{
	close();
}

void ExcelEngine::close()
{
	if (m_pExcel)
	{
		m_pWorkBook->dynamicCall("Close()");
		m_pExcel->dynamicCall("Quit()");//关闭excel
	}

	DEL_OBJ(m_pWorkSheet);
	DEL_OBJ(m_pWorkSheets);
	DEL_OBJ(m_pWorkBook);
	DEL_OBJ(m_pWorkBooks);

	DEL_OBJ(m_pExcel);
}

QAxObject *ExcelEngine::getWorkBooks()
{
	return m_pWorkBooks;
}

QAxObject *ExcelEngine::getWorkBook()
{
	return m_pWorkBook;
}

QAxObject *ExcelEngine::getWorkSheets()
{
	return m_pWorkSheets;
}

QAxObject *ExcelEngine::getWorkSheet()
{
	if (!m_pWorkSheet)
		return nullptr;

	return m_pWorkSheet;
}


/*****************************************************/
/* 工作表                                                                 */
/****************************************************/

bool ExcelEngine::selectSheet(const QString& sheetName)
{
	if (!m_pWorkSheets)
		return false;

	m_pWorkSheet = m_pWorkSheets->querySubObject("Item(const QString&)", sheetName);
	return true;
}


bool ExcelEngine::selectSheet(int sheetIndex)
{
	if (!m_pWorkSheets || sheetIndex < 1 || sheetIndex > getSheetsCount())
		return false;

	m_pWorkSheet = m_pWorkSheets->querySubObject("Item(int)", sheetIndex);
	return true;
}

void ExcelEngine::deleteSheet(const QString& sheetName)
{
	if (!m_pWorkSheets)
		return;

	m_pAxObj = m_pWorkSheets->querySubObject("Item(const QString&)", sheetName);
	if(m_pAxObj)
		m_pAxObj->dynamicCall("delete");
}

void ExcelEngine::deleteSheet(int sheetIndex)
{
	if (!m_pWorkSheets)
		return;

	m_pAxObj = m_pWorkSheets->querySubObject("Item(int)", sheetIndex);
	if (m_pAxObj)
		m_pAxObj->dynamicCall("delete");
}


bool ExcelEngine::insertSheet(QString sheetName, bool bAtLast /*= true*/)
{
	if (!m_pWorkSheets)
		return false;

	if (!bAtLast)
	{
		int nSheetCount = getSheetsCount();
		QAxObject* pLastSheet = m_pWorkSheets->querySubObject("Item(int)", nSheetCount);
		m_pWorkSheets->querySubObject("Add(QVariant)", pLastSheet->asVariant()); //新加表 处于倒数第二的位置;
		m_pWorkSheet = m_pWorkSheets->querySubObject("Item(int)", nSheetCount);
		pLastSheet->dynamicCall("Move(QVariant)", m_pWorkSheet->asVariant());

		nSheetCount = getSheetsCount();
		pLastSheet = m_pWorkSheets->querySubObject("Item(int)", nSheetCount);
		pLastSheet->setProperty("Name", sheetName);  //设置工作表名称
	}
	else
	{
		m_pWorkSheets->querySubObject("Add()"); //新建一个工作簿
		m_pAxObj = m_pWorkSheets->querySubObject("Item(int)", 1);  //获取工作表集合的工作表1
		if (m_pAxObj)
			m_pAxObj->setProperty("Name", sheetName);
	}
	return true;
}


bool ExcelEngine::moveSheetForward(int nSheetIndex)
{
	if (!m_pWorkBooks)
		return false;

	if (nSheetIndex < 2) return true; // 己在最前面

	if (selectSheet(nSheetIndex))
	{
		m_pAxObj = m_pWorkSheets->querySubObject("Item(int)", nSheetIndex - 1);
		m_pWorkSheet->dynamicCall("Move(QVariant)", m_pAxObj->asVariant());
	}

	return true;
}

bool ExcelEngine::moveSheetBack(int nSheetIndex)
{
	int nCount = getSheetsCount();
	if (nSheetIndex > nCount)
		return  true;

	moveSheetForward(nSheetIndex + 1);
	return true;
}


int ExcelEngine::getSheetsCount()
{
	if (!m_pWorkSheets)
		return 0;
	return m_pWorkSheets->property("Count").toInt();
}

QString ExcelEngine::getSheetName()
{
	if (!m_pWorkSheet)
		return " ";

	return m_pWorkSheet->property("Name").toString();
}

QString ExcelEngine::getSheetName(int sheetIndex)
{
	if (!m_pWorkSheets)
		return "";

	m_pAxObj = m_pWorkSheets->querySubObject("Item(int)", sheetIndex);
	return m_pAxObj->property("Name").toString();
}



/*****************************************************/
/* 单元格                                                                 */
/****************************************************/

void ExcelEngine::setCell(int row, int column, const QString& value)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
	m_pAxObj->dynamicCall("SetValue(const QString&)", value);
}

void ExcelEngine::setCell(const QString& cell, const QString& value)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->dynamicCall("SetValue(const QString&)", value);
}


void ExcelEngine::setCell(int row, int column, const int& value)
{
	if (!m_pWorkSheet) 
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
	m_pAxObj->dynamicCall("SetValue(const int&)", value);
}

void ExcelEngine::setCell(const QString& cell, const int& value)
{
	if (!m_pWorkSheet) 
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&, const QString&,)", cell);
	m_pAxObj->dynamicCall("SetValue(const int&)", value);
}


void ExcelEngine::setCell(int row, int column, const float& value)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
	m_pAxObj->dynamicCall("SetValue(const float&)", value);
}

void ExcelEngine::setCell(const QString& cell, const float& value)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&, const QString&,)", cell);
	m_pAxObj->dynamicCall("SetValue(const float&)", value);
}

void ExcelEngine::mergeCells(const QString& cell)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("VerticalAlignment", -4108);//xlCenter  
	m_pAxObj->setProperty("WrapText", true);
	m_pAxObj->setProperty("MergeCells", true);
}

void ExcelEngine::mergeCells(int topLeftRow, int topLeftColumn, int bottomRightRow, int bottomRightColumn)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(topLeftColumn - 1 + 'A'));
	cell.append(QString::number(topLeftRow));
	cell.append(":");
	cell.append(QChar(bottomRightColumn - 1 + 'A'));
	cell.append(QString::number(bottomRightRow));

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("VerticalAlignment", -4108);//xlCenter  
	m_pAxObj->setProperty("WrapText", true);
	m_pAxObj->setProperty("MergeCells", true);
}


void ExcelEngine::clearCell(int row, int column)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(column - 1 + 'A'));
	cell.append(QString::number(row));

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->dynamicCall("ClearContents()");
}

void ExcelEngine::clearCell(const QString& cell)
{
	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->dynamicCall("ClearContents()");
}


int ExcelEngine::getRowsCount()
{
	if (!m_pWorkSheet)
		return 0;

	int rows = 0;
	try {
		QAxObject * pUsedrange = m_pWorkSheet->querySubObject("UsedRange");
		QAxObject* pRows = pUsedrange->querySubObject("Rows");
		rows = pRows->property("Count").toInt();
	}
	catch (...) {
		qCritical() << "获取行数失败...";
	}
	return rows;
}


int ExcelEngine::getColumnsCount()
{
	if (!m_pWorkSheet)
		return 0;

	int columns = 0;
	try {
		QAxObject * pUsedrange = m_pWorkSheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
		QAxObject* pColumns = pUsedrange->querySubObject("Columns");
		columns = pColumns->property("Count").toInt();
	}
	catch (...) {
		qCritical() << "获取列数失败...";
	}
	return columns;
}

bool ExcelEngine::insertEmptyRow(int nInsertRow /*= 1*/)
{
	if (m_pWorkSheet || nInsertRow < 1) return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nInsertRow);
	m_pAxObj->dynamicCall("Insert()");
	return true;
}

bool ExcelEngine::insertEmptyColumn(int nInsertCol /*= 1*/)
{
	if (m_pWorkSheet || nInsertCol < 1)
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nInsertCol);
	m_pAxObj->dynamicCall("Insert()");
	return true;
}


void ExcelEngine::deleteRow(int nRowNum)
{
	if (!m_pWorkSheet || nRowNum < 1)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nRowNum);//获取选定的行
	if (m_pAxObj)
		m_pAxObj->dynamicCall("Delete()"); //修改所选行
}

bool ExcelEngine::deleteColumn(int nColumnNum)
{
	if (!m_pWorkSheet || nColumnNum < 1)
		return false;

	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nColumnNum);
	if (m_pAxObj)
		m_pAxObj->dynamicCall("Delete()");
	return true;
}
bool ExcelEngine::copyRowThenInsert(int nCopyRow, int nInsertRow)
{
	if (!m_pWorkSheet || nCopyRow < 1 || nInsertRow < 1)
		return false;

	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nCopyRow);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");
	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nInsertRow);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Insert()");
	return true;
}

bool ExcelEngine::copyColumnThenInsert(int nCopyCol, int nInsertCol)
{
	if (!m_pWorkSheet || nCopyCol < 1 || nInsertCol < 1)
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nCopyCol);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nInsertCol);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Insert()");

	return true;
}

bool ExcelEngine::copyRowThenPaste(int nCopyRow, int nInsertRow)
{
	if (!m_pWorkSheet || nCopyRow < 1 || nInsertRow < 1) 
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nCopyRow);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Rows(int)", nInsertRow);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("PasteSpecial()");
	return true;
}

bool ExcelEngine::copyColumnThenPaste(int nCopyCol, int nInsertCol)
{
	if (!m_pWorkSheet || nCopyCol < 1 || nInsertCol < 1) 
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nCopyCol);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Columns(int)", nInsertCol);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("PasteSpecial()");

	return true;
}

bool ExcelEngine::copyBlockThenInsert(const QString& strCellCopy, const QString& strCellPast)
{
	if (!m_pWorkSheet) 
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellCopy);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellPast);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Insert()");

	return true;
}

bool ExcelEngine::copyBlockThenPaste(const QString& strCellCopy, const QString& strCellPast)
{
	if (!m_pWorkSheet) 
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellCopy);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellPast);
	m_pAxObj->dynamicCall("Select()");
	m_pAxObj->dynamicCall("PasteSpecial()");

	return true;
}

bool ExcelEngine::multiSelectCopy(const QString &strCellCopy, const QString &strCellPaste)
{
	if (!m_pWorkSheet) 
		return false;
	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellCopy);
	m_pAxObj->dynamicCall("Select()");

	m_pAxObj->dynamicCall("Copy()");

	m_pAxObj = m_pWorkSheet->querySubObject("Range(QVariant&,QVariant&)", strCellPaste);//->dynamicCall("Select()");
	m_pAxObj->dynamicCall("Paste()");
	return true;
}

QString ExcelEngine::getCell(int row, int column)
{
	if (!m_pWorkSheet)
		return "";

	QString strValue = "";
	try {
		m_pAxObj = m_pWorkSheet->querySubObject("Cells(int, int)", row, column);
		strValue = m_pAxObj->property("Value").toString();
	}
	catch (...) {
		qCritical() << "获取单元格信息失败...";
	}

	return strValue;
}

QString ExcelEngine::getCell(const QString& strCell)
{
	if (!m_pWorkSheet)
		return "";

	QString strValue = "";
	try {
		m_pAxObj = m_pWorkSheet->querySubObject("Range(QString)", strCell);
		strValue = m_pAxObj->property("Value").toString();
	}
	catch (...) {
		qCritical() << "获取单元格信息失败...";
	}

	return strValue;
}


QVariant ExcelEngine::getCellValue(int row, int column)
{
	if (!m_pWorkSheet)
		return 0;

	m_pAxObj = m_pWorkSheet->querySubObject("Cells(int,int)", row, column);
	return m_pAxObj->property("Value");
}

QVariant ExcelEngine::getCellValue(const QString& strCell)
{
	if (!m_pWorkSheet)
		return false;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", strCell);
	return m_pAxObj->property("Value");
}

bool ExcelEngine::sumFunc(const QString& strCell, const QString& strCellResult)
{
	if (!m_pWorkSheet)
		return false;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", strCellResult);
	QString strSum = "=SUM(" + strCell + ")";
	m_pAxObj->dynamicCall("SetValue(const QString&)", strSum);
	return true;
}

bool ExcelEngine::sumFunc(const QStringList& strCells, const QString& strCellResult)
{
	if (!m_pWorkSheet)
		return false;

	QString strSum = "=SUM(";
	for (int i = 0; i < strCells.size(); i++)
	{
		strSum += strCells.at(i) + ",";
	}
	strSum += ")";

	//    m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", strCellResult);
	//    m_pAxObj->dynamicCall("SetValue(const QString&)",strSum);

	setCellFormula(strCellResult, strSum);
	return true;
}

bool ExcelEngine::setCellFormula(const QString& strCell, const QString& strFormula)
{
	if (!m_pWorkSheet)
		return false;

	m_pAxObj = m_pWorkSheet->querySubObject("Columns(const QString&)", strCell);
	m_pAxObj->setProperty("SetValue(const QString&)", strFormula);
	return true;
}

/*****************************************************/
/* 布局格式                                                               */
/*****************************************************/

void ExcelEngine::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)
{
	if (!m_pWorkSheet)
		return;

	QAxObject *usedRange = m_pWorkSheet->querySubObject("UsedRange");
	*topLeftRow = usedRange->property("Row").toInt();
	*topLeftColumn = usedRange->property("Column").toInt();

	QAxObject *rows = usedRange->querySubObject("Rows");
	*bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;

	QAxObject *columns = usedRange->querySubObject("Columns");
	*bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
}

void ExcelEngine::setColumnWidth(int column, int width)
{
	if (!m_pWorkSheet)
		return;

	QString columnName;
	columnName.append(QChar(column - 1 + 'A'));
	columnName.append(":");
	columnName.append(QChar(column - 1 + 'A'));

	m_pAxObj = m_pWorkSheet->querySubObject("Columns(const QString&)", columnName);
	m_pAxObj->setProperty("ColumnWidth", width);
}

void ExcelEngine::setRowHeight(int row, int height)
{
	if (!m_pWorkSheet)
		return;

	QString rowsName;
	rowsName.append(QString::number(row));
	rowsName.append(":");
	rowsName.append(QString::number(row));

	m_pAxObj = m_pWorkSheet->querySubObject("Rows(const QString &)", rowsName);
	m_pAxObj->setProperty("RowHeight", height);
}

void ExcelEngine::setCellTextCenter(int row, int column)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(column - 1 + 'A'));
	cell.append(QString::number(row));

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("HorizontalAlignment", -4108);//xlCenter  
}

void ExcelEngine::setCellTextCenter(const QString &cell)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("HorizontalAlignment", -4108);//xlCenter  
}

void ExcelEngine::setCellTextWrap(int row, int column, bool isWrap)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(column - 1 + 'A'));
	cell.append(QString::number(row));

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("WrapText", isWrap);
}

void ExcelEngine::setCellTextWrap(const QString &cell, bool isWrap)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj->setProperty("WrapText", isWrap);
}

void ExcelEngine::setAutoFitRow(int row)
{
	if (!m_pWorkSheet)
		return;

	QString rowsName;
	rowsName.append(QString::number(row));
	rowsName.append(":");
	rowsName.append(QString::number(row));

	QAxObject * rows = m_pWorkSheet->querySubObject("Rows(const QString &)", rowsName);
	rows->dynamicCall("AutoFit()");
}

void ExcelEngine::mergeSerialSameCellsInAColumn(int column, int topRow)
{
	int a, b, c, rowsCount;
	getUsedRange(&a, &b, &rowsCount, &c);

	int aMergeStart = topRow, aMergeEnd = topRow + 1;

	QString value;
	while (aMergeEnd <= rowsCount)
	{
		value = getCellValue(aMergeStart, column).toString();
		while (value == getCellValue(aMergeEnd, column).toString())
		{
			clearCell(aMergeEnd, column);
			aMergeEnd++;
		}
		aMergeEnd--;
		mergeCells(aMergeStart, column, aMergeEnd, column);

		aMergeStart = aMergeEnd + 1;
		aMergeEnd = aMergeStart + 1;
	}
}

int ExcelEngine::getUsedRowsCount()
{
	if (!m_pWorkSheet)
		return 0;

	QAxObject *usedRange = m_pWorkSheet->querySubObject("UsedRange");
	int topRow = usedRange->property("Row").toInt();
	QAxObject *rows = usedRange->querySubObject("Rows");
	int bottomRow = topRow + rows->property("Count").toInt() - 1;
	return bottomRow;
}

void ExcelEngine::setCellFontBold(int row, int column, bool isBold)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(column - 1 + 'A'));
	cell.append(QString::number(row));

	QAxObject *m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj = m_pAxObj->querySubObject("Font");
	m_pAxObj->setProperty("Bold", isBold);
}

void ExcelEngine::setCellFontBold(const QString &cell, bool isBold)
{
	if (!m_pWorkSheet)
		return;
	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj = m_pAxObj->querySubObject("Font");
	m_pAxObj->setProperty("Bold", isBold);
}

void ExcelEngine::setCellFontSize(int row, int column, int size)
{
	if (!m_pWorkSheet)
		return;

	QString cell;
	cell.append(QChar(column - 1 + 'A'));
	cell.append(QString::number(row));

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj = m_pAxObj->querySubObject("Font");
	m_pAxObj->setProperty("Size", size);
}

void ExcelEngine::setCellFontSize(const QString &cell, int size)
{
	if (!m_pWorkSheet)
		return;

	m_pAxObj = m_pWorkSheet->querySubObject("Range(const QString&)", cell);
	m_pAxObj = m_pAxObj->querySubObject("Font");
	m_pAxObj->setProperty("Size", size);
}

/*****************************************************/
/* 文件                                                                      */
/*****************************************************/
void ExcelEngine::save()
{
	if (m_pWorkBook)
		m_pWorkBook->dynamicCall("Save()");
}

void ExcelEngine::saveAs(const QString& strPath)
{
	//QString filepath=QFileDialog::getSaveFileName(nullptr, tr("Save orbit"),".", tr("Microsoft Office 2010 (*.xlsx)"));//获取保存路径
	// 保存至filepath,注意一定要用QDir::toNativeSeparators将路径中的"/"转换为"\",不然一定保存不了。
	m_pWorkBook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(strPath));
}

你可能感兴趣的:(Qt,C++)