QT 操作excel 類封裝(轉載)

QT 操作excel 類封裝(轉載)



pro file

[plain] view plaincopy
  1. CONFIG += qaxcontainer  
  2.   
  3. QT       += core  
  4.   
  5. QT       -= gui  
  6.   
  7. TARGET = QExcel  
  8. CONFIG   += console  
  9. CONFIG   -= app_bundle  
  10.   
  11. TEMPLATE = app  
  12.   
  13.   
  14. SOURCES += main.cpp \  
  15.     qexcel.cpp  
  16.   
  17. HEADERS += \  
  18.     qexcel.h  


 qexcel.h

  1. #ifndef QEXCEL_H  
  2. #define QEXCEL_H  
  3.   
  4. #include <QString>  
  5. #include <QVariant>  
  6.   
  7. class QAxObject;  
  8.   
  9. class QExcel : public QObject  
  10. {  
  11. public:  
  12.     QExcel(QString xlsFilePath, QObject *parent = 0);  
  13.     ~QExcel();  
  14.   
  15. public:  
  16.     QAxObject * getWorkBooks();  
  17.     QAxObject * getWorkBook();  
  18.     QAxObject * getWorkSheets();  
  19.     QAxObject * getWorkSheet();  
  20.   
  21. public:  
  22.     /**************************************************************************/  
  23.     /* 工作表                                                                 */  
  24.     /**************************************************************************/  
  25.     void selectSheet(const QString& sheetName);  
  26.     //sheetIndex 起始於 1  
  27.     void selectSheet(int sheetIndex);  
  28.     void deleteSheet(const QString& sheetName);  
  29.     void deleteSheet(int sheetIndex);  
  30.     void insertSheet(QString sheetName);  
  31.     int getSheetsCount();  
  32.     //在 selectSheet() 之後纔可調用  
  33.     QString getSheetName();  
  34.     QString getSheetName(int sheetIndex);  
  35.   
  36.     /**************************************************************************/  
  37.     /* 單元格                                                                 */  
  38.     /**************************************************************************/  
  39.     void setCellString(int row, int column, const QString& value);  
  40.     //cell 例如 "A7"  
  41.     void setCellString(const QString& cell, const QString& value);  
  42.     //range 例如 "A5:C7"  
  43.     void mergeCells(const QString& range);  
  44.     void mergeCells(int topLeftRow, int topLeftColumn, int bottomRightRow, int bottomRightColumn);  
  45.     QVariant getCellValue(int row, int column);  
  46.     void clearCell(int row, int column);  
  47.     void clearCell(const QString& cell);  
  48.   
  49.     /**************************************************************************/  
  50.     /* 佈局格式                                                               */  
  51.     /**************************************************************************/  
  52.     void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);  
  53.     void setColumnWidth(int column, int width);  
  54.     void setRowHeight(int row, int height);  
  55.     void setCellTextCenter(int row, int column);  
  56.     void setCellTextCenter(const QString& cell);  
  57.     void setCellTextWrap(int row, int column, bool isWrap);  
  58.     void setCellTextWrap(const QString& cell, bool isWrap);  
  59.     void setAutoFitRow(int row);  
  60.     void mergeSerialSameCellsInAColumn(int column, int topRow);  
  61.     int getUsedRowsCount();  
  62.     void setCellFontBold(int row, int column, bool isBold);  
  63.     void setCellFontBold(const QString& cell, bool isBold);  
  64.     void setCellFontSize(int row, int column, int size);  
  65.     void setCellFontSize(const QString& cell, int size);  
  66.   
  67.     /**************************************************************************/  
  68.     /* 文件                                                                   */  
  69.     /**************************************************************************/  
  70.     void save();  
  71.     void close();  
  72.   
  73. private:  
  74.     QAxObject * excel;  
  75.     QAxObject * workBooks;  
  76.     QAxObject * workBook;  
  77.     QAxObject * sheets;  
  78.     QAxObject * sheet;  
  79. };  
  80.   
  81. #endif  


 qexcel.cpp

  1. #include <QAxObject>  
  2. #include <QFile>  
  3. #include <QStringList>  
  4. #include <QDebug>  
  5.   
  6. #include "qexcel.h"  
  7.   
  8. QExcel::QExcel(QString xlsFilePath, QObject *parent)  
  9. {  
  10.     excel = 0;  
  11.     workBooks = 0;  
  12.     workBook = 0;  
  13.     sheets = 0;  
  14.     sheet = 0;  
  15.   
  16.     excel = new QAxObject("Excel.Application", parent);  
  17.     workBooks = excel->querySubObject("Workbooks");  
  18.     QFile file(xlsFilePath);  
  19.     if (file.exists())  
  20.     {  
  21.         workBooks->dynamicCall("Open(const QString&)", xlsFilePath);  
  22.         workBook = excel->querySubObject("ActiveWorkBook");  
  23.         sheets = workBook->querySubObject("WorkSheets");  
  24.     }  
  25. }  
  26.   
  27. QExcel::~QExcel()  
  28. {  
  29.     close();  
  30. }  
  31.   
  32. void QExcel::close()  
  33. {  
  34.     excel->dynamicCall("Quit()");  
  35.   
  36.     delete sheet;  
  37.     delete sheets;  
  38.     delete workBook;  
  39.     delete workBooks;  
  40.     delete excel;  
  41.   
  42.     excel = 0;  
  43.     workBooks = 0;  
  44.     workBook = 0;  
  45.     sheets = 0;  
  46.     sheet = 0;  
  47. }  
  48.   
  49. QAxObject *QExcel::getWorkBooks()  
  50. {  
  51.     return workBooks;  
  52. }  
  53.   
  54. QAxObject *QExcel::getWorkBook()  
  55. {  
  56.     return workBook;  
  57. }  
  58.   
  59. QAxObject *QExcel::getWorkSheets()  
  60. {  
  61.     return sheets;  
  62. }  
  63.   
  64. QAxObject *QExcel::getWorkSheet()  
  65. {  
  66.     return sheet;  
  67. }  
  68.   
  69. void QExcel::selectSheet(const QString& sheetName)  
  70. {  
  71.     sheet = sheets->querySubObject("Item(const QString&)", sheetName);  
  72. }  
  73.   
  74. void QExcel::deleteSheet(const QString& sheetName)  
  75. {  
  76.     QAxObject * a = sheets->querySubObject("Item(const QString&)", sheetName);  
  77.     a->dynamicCall("delete");  
  78. }  
  79.   
  80. void QExcel::deleteSheet(int sheetIndex)  
  81. {  
  82.     QAxObject * a = sheets->querySubObject("Item(int)", sheetIndex);  
  83.     a->dynamicCall("delete");  
  84. }  
  85.   
  86. void QExcel::selectSheet(int sheetIndex)  
  87. {  
  88.     sheet = sheets->querySubObject("Item(int)", sheetIndex);  
  89. }  
  90.   
  91. void QExcel::setCellString(int row, int column, const QString& value)  
  92. {  
  93.     QAxObject *range = sheet->querySubObject("Cells(int,int)", row, column);  
  94.     range->dynamicCall("SetValue(const QString&)", value);  
  95. }  
  96.   
  97. void QExcel::setCellFontBold(int row, int column, bool isBold)  
  98. {  
  99.     QString cell;  
  100.     cell.append(QChar(column - 1 + 'A'));  
  101.     cell.append(QString::number(row));  
  102.   
  103.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  104.     range = range->querySubObject("Font");  
  105.     range->setProperty("Bold", isBold);  
  106. }  
  107.   
  108. void QExcel::setCellFontSize(int row, int column, int size)  
  109. {  
  110.     QString cell;  
  111.     cell.append(QChar(column - 1 + 'A'));  
  112.     cell.append(QString::number(row));  
  113.   
  114.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  115.     range = range->querySubObject("Font");  
  116.     range->setProperty("Size", size);  
  117. }  
  118.   
  119. void QExcel::mergeCells(const QString& cell)  
  120. {  
  121.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  122.     range->setProperty("VerticalAlignment", -4108);//xlCenter  
  123.     range->setProperty("WrapText"true);  
  124.     range->setProperty("MergeCells"true);  
  125. }  
  126.   
  127. void QExcel::mergeCells(int topLeftRow, int topLeftColumn, int bottomRightRow, int bottomRightColumn)  
  128. {  
  129.     QString cell;  
  130.     cell.append(QChar(topLeftColumn - 1 + 'A'));  
  131.     cell.append(QString::number(topLeftRow));  
  132.     cell.append(":");  
  133.     cell.append(QChar(bottomRightColumn - 1 + 'A'));  
  134.     cell.append(QString::number(bottomRightRow));  
  135.   
  136.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  137.     range->setProperty("VerticalAlignment", -4108);//xlCenter  
  138.     range->setProperty("WrapText"true);  
  139.     range->setProperty("MergeCells"true);  
  140. }  
  141.   
  142. QVariant QExcel::getCellValue(int row, int column)  
  143. {  
  144.     QAxObject *range = sheet->querySubObject("Cells(int,int)", row, column);  
  145.     return range->property("Value");  
  146. }  
  147.   
  148. void QExcel::save()  
  149. {  
  150.     workBook->dynamicCall("Save()");  
  151. }  
  152.   
  153. int QExcel::getSheetsCount()  
  154. {  
  155.     return sheets->property("Count").toInt();  
  156. }  
  157.   
  158. QString QExcel::getSheetName()  
  159. {  
  160.     return sheet->property("Name").toString();  
  161. }  
  162.   
  163. QString QExcel::getSheetName(int sheetIndex)  
  164. {  
  165.     QAxObject * a = sheets->querySubObject("Item(int)", sheetIndex);  
  166.     return a->property("Name").toString();  
  167. }  
  168.   
  169. void QExcel::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)  
  170. {  
  171.     QAxObject *usedRange = sheet->querySubObject("UsedRange");  
  172.     *topLeftRow = usedRange->property("Row").toInt();  
  173.     *topLeftColumn = usedRange->property("Column").toInt();  
  174.   
  175.     QAxObject *rows = usedRange->querySubObject("Rows");  
  176.     *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;  
  177.   
  178.     QAxObject *columns = usedRange->querySubObject("Columns");  
  179.     *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;  
  180. }  
  181.   
  182. void QExcel::setColumnWidth(int column, int width)  
  183. {  
  184.     QString columnName;  
  185.     columnName.append(QChar(column - 1 + 'A'));  
  186.     columnName.append(":");  
  187.     columnName.append(QChar(column - 1 + 'A'));  
  188.   
  189.     QAxObject * col = sheet->querySubObject("Columns(const QString&)", columnName);  
  190.     col->setProperty("ColumnWidth", width);  
  191. }  
  192.   
  193. void QExcel::setCellTextCenter(int row, int column)  
  194. {  
  195.     QString cell;  
  196.     cell.append(QChar(column - 1 + 'A'));  
  197.     cell.append(QString::number(row));  
  198.   
  199.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  200.     range->setProperty("HorizontalAlignment", -4108);//xlCenter  
  201. }  
  202.   
  203. void QExcel::setCellTextWrap(int row, int column, bool isWrap)  
  204. {  
  205.     QString cell;  
  206.     cell.append(QChar(column - 1 + 'A'));  
  207.     cell.append(QString::number(row));  
  208.   
  209.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  210.     range->setProperty("WrapText", isWrap);  
  211. }  
  212.   
  213. void QExcel::setAutoFitRow(int row)  
  214. {  
  215.     QString rowsName;  
  216.     rowsName.append(QString::number(row));  
  217.     rowsName.append(":");  
  218.     rowsName.append(QString::number(row));  
  219.   
  220.     QAxObject * rows = sheet->querySubObject("Rows(const QString &)", rowsName);  
  221.     rows->dynamicCall("AutoFit()");  
  222. }  
  223.   
  224. void QExcel::insertSheet(QString sheetName)  
  225. {  
  226.     sheets->querySubObject("Add()");  
  227.     QAxObject * a = sheets->querySubObject("Item(int)", 1);  
  228.     a->setProperty("Name", sheetName);  
  229. }  
  230.   
  231. void QExcel::mergeSerialSameCellsInAColumn(int column, int topRow)  
  232. {  
  233.     int a,b,c,rowsCount;  
  234.     getUsedRange(&a, &b, &rowsCount, &c);  
  235.   
  236.     int aMergeStart = topRow, aMergeEnd = topRow + 1;  
  237.   
  238.     QString value;  
  239.     while(aMergeEnd <= rowsCount)  
  240.     {  
  241.         value = getCellValue(aMergeStart, column).toString();  
  242.         while(value == getCellValue(aMergeEnd, column).toString())  
  243.         {  
  244.             clearCell(aMergeEnd, column);  
  245.             aMergeEnd++;  
  246.         }  
  247.         aMergeEnd--;  
  248.         mergeCells(aMergeStart, column, aMergeEnd, column);  
  249.   
  250.         aMergeStart = aMergeEnd + 1;  
  251.         aMergeEnd = aMergeStart + 1;  
  252.     }  
  253. }  
  254.   
  255. void QExcel::clearCell(int row, int column)  
  256. {  
  257.     QString cell;  
  258.     cell.append(QChar(column - 1 + 'A'));  
  259.     cell.append(QString::number(row));  
  260.   
  261.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  262.     range->dynamicCall("ClearContents()");  
  263. }  
  264.   
  265. void QExcel::clearCell(const QString& cell)  
  266. {  
  267.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  268.     range->dynamicCall("ClearContents()");  
  269. }  
  270.   
  271. int QExcel::getUsedRowsCount()  
  272. {  
  273.     QAxObject *usedRange = sheet->querySubObject("UsedRange");  
  274.     int topRow = usedRange->property("Row").toInt();  
  275.     QAxObject *rows = usedRange->querySubObject("Rows");  
  276.     int bottomRow = topRow + rows->property("Count").toInt() - 1;  
  277.     return bottomRow;  
  278. }  
  279.   
  280. void QExcel::setCellString(const QString& cell, const QString& value)  
  281. {  
  282.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  283.     range->dynamicCall("SetValue(const QString&)", value);  
  284. }  
  285.   
  286. void QExcel::setCellFontSize(const QString &cell, int size)  
  287. {  
  288.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  289.     range = range->querySubObject("Font");  
  290.     range->setProperty("Size", size);  
  291. }  
  292.   
  293. void QExcel::setCellTextCenter(const QString &cell)  
  294. {  
  295.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  296.     range->setProperty("HorizontalAlignment", -4108);//xlCenter  
  297. }  
  298.   
  299. void QExcel::setCellFontBold(const QString &cell, bool isBold)  
  300. {  
  301.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  302.     range = range->querySubObject("Font");  
  303.     range->setProperty("Bold", isBold);  
  304. }  
  305.   
  306. void QExcel::setCellTextWrap(const QString &cell, bool isWrap)  
  307. {  
  308.     QAxObject *range = sheet->querySubObject("Range(const QString&)", cell);  
  309.     range->setProperty("WrapText", isWrap);  
  310. }  
  311.   
  312. void QExcel::setRowHeight(int row, int height)  
  313. {  
  314.     QString rowsName;  
  315.     rowsName.append(QString::number(row));  
  316.     rowsName.append(":");  
  317.     rowsName.append(QString::number(row));  
  318.   
  319.     QAxObject * r = sheet->querySubObject("Rows(const QString &)", rowsName);  
  320.     r->setProperty("RowHeight", height);  
  321. }  

main.cpp

  1. #include <QtGui/QApplication>  
  2. #include <QDebug>  
  3.   
  4. #include "qexcel.h"  
  5.   
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QApplication a(argc, argv);  
  9.   
  10.     //打開文件,取得工作簿  
  11.         QExcel j("D:/test.xls");  
  12.     //取得工作表數量  
  13.     //qDebug()<<"SheetCount"<<j.getSheetsCount();  
  14.     //激活一張工作表  
  15.     //j.selectSheet(1);  
  16.     //j.selectSheet("JSheet2");  
  17.     //取得工作表名稱  
  18.     //j.selectSheet(1);  
  19.     //j.getSheetName();  
  20.     //qDebug()<<"SheetName 1"<<j.getSheetName(1);  
  21.     //取得工作表已使用範圍  
  22.     //int topLeftRow, topLeftColumn, bottomRightRow, bottomRightColumn;  
  23.     //j.getUsedRange(&topLeftRow, &topLeftColumn, &bottomRightRow, &bottomRightColumn);  
  24.     //讀值  
  25.     //j.getCellValue(2, 2).toString();  
  26.     //刪除工作表  
  27.     //j.selectSheet("Sheet1");  
  28.     //j.selectSheet(1);  
  29.     //j.deleteSheet();  
  30.     //j.save();  
  31.     //插入數據  
  32.         j.selectSheet("Sheet1");  
  33.         j.setCellString(1, 7, "addString");  
  34.         j.setCellString("A3""abc");  
  35.         j.save();  
  36.     //合併單元格  
  37.     //j.selectSheet(2);  
  38.     //j.mergeCells("G1:H2");  
  39.     //j.mergeCells(4, 7, 5 ,8);  
  40.     //j.save();  
  41.     //設置列寬  
  42.     //j.selectSheet(1);  
  43.     //j.setColumnWidth(1, 20);  
  44.     //j.save();  
  45.     //設置粗體  
  46.     //j.selectSheet(1);  
  47.     //j.setCellFontBold(2, 2, true);  
  48.     //j.setCellFontBold("A2", true);  
  49.     //j.save();  
  50.     //設置文字大小  
  51.     //j.selectSheet(1);  
  52.     //j.setCellFontSize("B3", 20);  
  53.     //j.setCellFontSize(1, 2, 20);  
  54.     //j.save();  
  55.     //設置單元格文字居中  
  56.     //j.selectSheet(2);  
  57.     //j.setCellTextCenter(1, 2);  
  58.     //j.setCellTextCenter("A2");  
  59.     //j.save();  
  60.     //設置單元格文字自動折行  
  61.     //j.selectSheet(1);  
  62.     //j.setCellTextWrap(2,2,true);  
  63.     //j.setCellTextWrap("A2", true);  
  64.     //j.save();  
  65.     //設置一行自適應行高  
  66.     //j.selectSheet(1);  
  67.     //j.setAutoFitRow(2);  
  68.     //j.save();  
  69.     //新建工作表  
  70.     //j.insertSheet("abc");  
  71.     //j.save();  
  72.     //清除單元格內容  
  73.     //j.selectSheet(4);  
  74.     //j.clearCell(1,1);  
  75.     //j.clearCell("A2");  
  76.     //j.save();  
  77.     //合併一列中相同連續的單元格  
  78.     //j.selectSheet(1);  
  79.     //j.mergeSerialSameCellsInColumn(1, 2);  
  80.     //j.save();  
  81.     //獲取一張工作表已用行數  
  82.     //j.selectSheet(1);  
  83.     //qDebug()<<j.getUsedRowsCount();  
  84.     //設置行高  
  85.         //j.selectSheet(1);  
  86.         //j.setRowHeight(2, 30);  
  87.         //j.save();  
  88.   
  89.     j.close();  
  90.     qDebug()<<"App End";  
  91.     return a.exec();  
  92. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章