1. 导出excel格式文件
核心代码
1.1 run.pri
QT + = axcontainer
1.2 CustomExportExcel.h
#include <QObject>
#include <QAxObject>
class CustomExportExcel : public QAxObject
{
Q_OBJECT
public :
explicit CustomExportExcel ( QObject * parent = 0 ) ;
~ CustomExportExcel ( ) ;
public :
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) ;
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) ;
cellAlign- > setProperty ( "HorizontalAlignment" , nRowAline) ;
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 ) ;
for (int row = 0 ; row < 10 ; row++ )
{
for (int col = 0 ; col < 10 ; col++ )
{
}
}
}
}
}
1.4 方法使用
#include "CustomExportExcel.h"
CustomExportExcel * pExcel = new CustomExportExcel;
pExcel- > createExcel ( ) ;