Qt操作Excel类
基于某文章(原文找不到了), 进行小修改调整
.h
#ifndef QEXCEL_H
#define QEXCEL_H #include <QString>
#include <QVariant> 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);// 单元格求和, 将要累加的单元格进行累加后,存入 strCellResultbool 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 <QAxObject>
#include <QFile>
//#include <QFileDialog>
#include <QStringList>
#include <QDebug> // 清理指针
#define DEL_OBJ(X) { if (X) { delete X; X=nullptr;} }#include <QDir>
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); //获取工作表集合的工作表1if (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操作Excel类相关推荐
- QT 操作excel 类封装(转载)
QT 操作excel 类封装(转载) 原链接:http://blog.csdn.net/liliming1234/article/details/7054941 pro file [plain] v ...
- PHP操作excel类 PHPExcel
PHP操作excel类 PHPExcel http://www.cr173.com/soft/40741.html 我的微云:http://share.weiyun.com/2db79f1438f ...
- php excel 设置常规_php实现的操作excel类详解
本文实例讲述了php实现的操作excel类.分享给大家供大家参考,具体如下: class Excel { static $instance=null; private $excel=null; pri ...
- 【博主推荐】Python 基于Xlwings、Openpyxl自己重新封装Python操作Excel类
1.简介:Python操作Excel,常用Xlwings.Openpyxl类,由于其知识琐碎,使用起来不太方便,因此自己把常用用法重新封装一个操作类. 2.应用场景:使用Python操作Excel,读 ...
- c++ qt 操作Excel 实现冻结窗格的功能。
使用qt操作Excel的一些基本操作内容我就不介绍了,你们搜一下就可以了,如果不想麻烦可以去下面这个链接地址看一下. qt操作Excel基础使用技巧 本文主要讲一个知识点,就是使用qt操作Excel实 ...
- Qt Xlsx使用教程、Qt操作Excel、Qt生成Excel图表、跨平台不依赖Office
文章目录 1.Qt Xlsx库简介 2. 用法①:使用Xlsx作为Qt5的附加模块 2.1 构建模块 2.2 下载QtXlsx源代码 2.3 为编译器安装Xlsx模块 2.3.1 打开 MinGW 7 ...
- Qt操作EXCEL设置自动筛选
Qt可以通过使用QAxObject操作excel,具体的方法这里不做介绍.假设获取到sheet的指针 QAxObject* workSheet; 设置第一行添加自动筛选. QAxObject* ran ...
- Qt操作Excel表格
简单介绍: 文章内使用的是Windows系统的ActiveX对象(QAxObject)操作Excel. 相关参考: 微软vba开发指南:https://docs.microsoft.com/zh-cn ...
- c#操作Excel类
这是其中的一种方式,使用微软的Microsoft.Office.Interop.Excel库. 自己写了一个操作类: using System; using System.Collections.Ge ...
最新文章
- rdp协议打开 windows_RDPY - Twisted Python 实现的RDP协议(Windows 远程桌面)
- 不同于NLP,数据驱动方法与机器学习无法攻克NLU,原因有三点
- python和php-PHP和Python如何选择?或许可以考虑这三个问题
- Linux开发5款实用工具推荐
- Unity3D学习(五):实现一个简单的视觉感知
- 正反案例介绍SOLID原则
- ASP获取数据库表名,字段名以及对字段的一些操作
- xx闪购—主体选项卡
- Git 常用命令大全
- Linux 2.6内核配置说明
- excel自动排班表_Excel教程:3秒搞定排班表模板
- 全网最详细教程(上):教你如何从0-1制作出一张可视化大屏
- 开源协议及应用 (BSD/GPL/LGPL/Apache/MIT)
- 大学英语综合教程一 Unit 4 课文内容英译中 中英翻译
- DELPHI读取网页源文件和获取字符串
- 预报精准的天气查询APP开发原理是什么
- Android学习——APP内容共享
- pyqt5做一个工具箱
- 服务器与客户端的TCP连接
- 游戏设计艺术 第2版 第30章 读书笔记
热门文章
- 计算机对体育专业就业前景,体育教育就业方向及就业前景分析
- 外派linux运维,请好好善待你身边的Linux运维工程师,因为他们...
- 大数据Saprk----Spark基础-scala的隐式转换
- 打马赛克就安全了吗?AI消除马赛克,GitHub霸榜
- 美味冰皮月饼的做法 月饼的做法
- 【nacos】com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
- kotlin java 知乎_GitHub - luciferldy/ZhihuDailyKotlin: 这是是一个使用 Kotlin 开发的知乎日报客户端...
- Matlab Shift Arithmetic模块
- 理解ES6中的TDZ(暂时性死区)
- 解决ValueError: too many values to unpack