当前位置:   article > 正文

qt5 windows Excel QAxObject导入导出_qt多表格输出qaxobject

qt多表格输出qaxobject

1、点击导入按钮,弹出excel表格选择框,选择后读取内容:

  1. //在头文件中定义
  2. QScopedPointer<ExcelBase> m_xls;
  3. //下面是点击导入excel代码
  4. QString xlsFile = QFileDialog::getOpenFileName(this,QString(),QString(),"excel(*.xls*.xlsx)");
  5. qDebug()<<xlsFile;
  6. if(xlsFile.isEmpty()){
  7. return;
  8. }
  9. QList< QList<QVariant> > m_datas;
  10. if(m_xls.isNull())
  11. m_xls.reset(new ExcelBase);
  12. m_xls->open(xlsFile);
  13. m_xls->setCurrentSheet(1);
  14. m_xls->readAll(m_datas);
  15. for (int j = 0; j < m_datas[i].size(); ++j) {
  16. for (int j = 0; j < m_datas[i].size(); ++j) {
  17. QString imei = m_datas[i][j].toString();
  18. //这个imei就是第i行第j列读取出来的数据
  19. }
  20. }
  21. //这mxls中操作创建文件比较耗时,建议新建线程使用。

2、点击到处按钮,弹出选择保存excel的路径,然后存入数据。

  1. //头文件定义
  2. QScopedPointer<ExcelBase> m_xls;
  3. //点击导出
  4. //生成导出数据
  5. QList< QList<QVariant> > m_datas;
  6. for(int i = 0;i < 1000;i++){
  7. QList<QVariant> rows;
  8. for(int j = 0; j < 8;j++){
  9. rows<<"这里写入值";
  10. }
  11. m_datas<<rows;
  12. }
  13. //保存文件名
  14. QString xlsFile = QFileDialog::getExistingDirectory(this);
  15. if(xlsFile.isEmpty()){
  16. return;
  17. }
  18. QDateTime current_time = QDateTime::currentDateTime();
  19. //显示时间,格式为:年-月-日 时:分:秒 周几
  20. QString StrCurrentTime = current_time.toString("yyyy年MM月dd日_hh时mm分ss秒");
  21. xlsFile += "/xds_";
  22. xlsFile.append(StrCurrentTime).append(".xlsx");
  23. if(m_xls.isNull())
  24. m_xls.reset(new ExcelBase);
  25. m_xls->create(xlsFile);
  26. m_xls->setCurrentSheet(1);
  27. bool result = m_xls->writeCurrentSheet(m_datas);
  28. m_xls->save();
  29. m_xls.reset();
  30. if(result){
  31. //导出成功
  32. }else{
  33. //导出失败
  34. }
  35. //xls创建相关操作比较耗时,建议放在新线程中

3、别忘记pro文件中添加

QT += axcontainer

4、最后是一个别人写的工具类ExcelBase

h文件如下:

  1. #ifndef EXCELREADER_H
  2. #define EXCELREADER_H
  3. #include <QObject>
  4. #include <QAxObject>
  5. #include <QString>
  6. #include <QStringList>
  7. #include <QVariant>
  8. class ExcelBasePrivate;
  9. ///
  10. /// \brief win下excel快速读取类
  11. /// 参考天池项目源码,天池源码地址:https://github.com/qtcn/tianchi/blob/v0.0.2-build20130701/include/tianchi/file/tcmsexcel.h
  12. ///
  13. class ExcelBase : public QObject
  14. {
  15. public:
  16. ExcelBase(QObject* par=NULL);
  17. ~ExcelBase();
  18. private:
  19. Q_DISABLE_COPY(ExcelBase)
  20. Q_DECLARE_PRIVATE(ExcelBase)
  21. ExcelBasePrivate* const d_ptr;
  22. public:
  23. /// @brief 设置方向的常数
  24. enum Alignment
  25. {
  26. xlTop = -4160, ///< 靠上
  27. xlLeft = -4131, ///< 靠左
  28. xlRight = -4152, ///< 靠右
  29. xlCenter = -4108, ///< 居中
  30. xlBottom = -4107 ///< 靠下
  31. };
  32. /// @brief 创建一个Microsoft Excel文件
  33. bool create(const QString& filename = QString());
  34. /// @brief 打开一个Microsoft Excel文件
  35. bool open(const QString& filename = QString());
  36. /// @brief 另存Microsoft Excel文件
  37. void saveAs(const QString& filename);
  38. void save();
  39. /// @brief 关闭Microsoft Excel文件
  40. void close();
  41. /// @brief 踢出当前打开的 Microsoft Excel<br>
  42. /// @brief 放弃此对象对该 Excel 的控制权<br>
  43. /// @brief Excel 文件仍保持打开,但丧失了控制权
  44. void kick();
  45. /// @brief 设置当前打开的 Excel 是否可见
  46. void setVisible(bool value);
  47. /// @brief 设置 Excel 文档的标题
  48. void setCaption(const QString& value);
  49. /// @brief 新建一本 Excel 文档
  50. bool addBook();
  51. /// @brief 返回当前 Excel 的 Sheet 数量
  52. int sheetCount();
  53. /// @brief 返回当前打开的 Excel 全部 Sheet 名
  54. QStringList sheetNames();
  55. /// @brief 返回当前 Sheet.
  56. bool currentSheet();
  57. /// @brief 设置并指定当前 Sheet.
  58. /// @param [in] 当前 Sheet 索引,从 1 开始
  59. bool setCurrentSheet(int index);
  60. /// @brief 当前打开的 Excel 的 Sheet 名
  61. QString currentSheetName();
  62. /// @brief 读取单元格 Sheet 的内容
  63. /// @param [in] row 行号,从 1 开始
  64. /// @param [in] col 列号,从 1 开始
  65. /// @return 返回指定单元格的内容
  66. QVariant read(int row, int col);
  67. /// @brief 读取单元格 Sheet 的内容
  68. /// @param [in] row 行号,从 1 开始
  69. /// @param [in] col 列号,从 1 开始
  70. /// @return 返回指定单元格的内容
  71. inline QVariant cell(int row, int col) { return read(row, col); }
  72. /// @brief 写入单元格 Sheet 的内容
  73. /// @param [in] row 行号,从 1 开始
  74. /// @param [in] col 列号,从 1 开始
  75. /// @param [in] value 准备写入的内容
  76. void write(int row, int col, const QVariant& value);
  77. void cellFormat(int row, int col, const QString& format);
  78. void cellAlign(int row, int col, Alignment hAlign, Alignment vAlign);
  79. /// @brief 获取有效区域信息
  80. /// @see rowStart() const
  81. /// @see rowEnd() const
  82. /// @see colStart() const
  83. /// @see colEnd() const
  84. bool usedRange(int& rowStart, int& colStart, int &rowEnd, int &colEnd);
  85. QVariant readAll();
  86. void readAll(QList<QList<QVariant> > &cells);
  87. bool writeCurrentSheet(const QList<QList<QVariant> > &cells);
  88. static void convertToColName(int data, QString &res);
  89. static QString to26AlphabetString(int data);
  90. static void castListListVariant2Variant(const QList<QList<QVariant> > &cells, QVariant &res);
  91. static void castVariant2ListListVariant(const QVariant& var,QList<QList<QVariant> > &res);
  92. };
  93. #endif // EXCELREADER_H

cpp文件如下:

  1. #include "ExcelBase.h"
  2. #include <QFile>
  3. #include <QList>
  4. #include <QDebug>
  5. #if defined(Q_OS_WIN)
  6. #include <ActiveQt/QAxObject>
  7. #endif // Q_OS_WIN
  8. #define TC_FREE(x) {delete x; x=NULL;}
  9. class ExcelBasePrivate
  10. {
  11. Q_DECLARE_PUBLIC(ExcelBase)
  12. public:
  13. explicit ExcelBasePrivate(ExcelBase* qptr);
  14. ~ExcelBasePrivate();
  15. void construct();
  16. void destory();
  17. ExcelBase* const q_ptr;
  18. #if defined(Q_OS_WIN)
  19. QAxObject* excel;
  20. QAxObject* books;
  21. QAxObject* book;
  22. QAxObject* sheets;
  23. QAxObject* sheet;
  24. #endif
  25. QString filename;
  26. QString sheetName;
  27. };
  28. ExcelBasePrivate::ExcelBasePrivate(ExcelBase *qptr)
  29. : q_ptr(qptr)
  30. #if defined(Q_OS_WIN)
  31. , excel(NULL)
  32. , books(NULL)
  33. , book(NULL)
  34. , sheets(NULL)
  35. , sheet(NULL)
  36. #endif // Q_OS_WIN
  37. {
  38. }
  39. ExcelBasePrivate::~ExcelBasePrivate()
  40. {
  41. #if defined(Q_OS_WIN)
  42. if(excel)
  43. {
  44. if (!excel->isNull())
  45. {
  46. excel->dynamicCall("Quit()");
  47. }
  48. }
  49. TC_FREE(sheet );
  50. TC_FREE(sheets);
  51. TC_FREE(book );
  52. TC_FREE(books );
  53. TC_FREE(excel );
  54. #endif // Q_OS_WIN
  55. }
  56. void ExcelBasePrivate::construct()
  57. {
  58. #if defined(Q_OS_WIN)
  59. destory();
  60. excel = new QAxObject(q_ptr);
  61. excel->setControl("Excel.Application");
  62. excel->setProperty("Visible",false);
  63. if (excel->isNull())
  64. {
  65. excel->setControl("ET.Application");
  66. }
  67. if (!excel->isNull())
  68. {
  69. books = excel->querySubObject("Workbooks");
  70. }
  71. #endif // Q_OS_WIN
  72. }
  73. void ExcelBasePrivate::destory()
  74. {
  75. #if defined(Q_OS_WIN)
  76. TC_FREE(sheet );
  77. TC_FREE(sheets);
  78. if (book != NULL && ! book->isNull())
  79. {
  80. book->dynamicCall("Close(Boolean)", false);
  81. }
  82. TC_FREE(book );
  83. TC_FREE(books);
  84. if (excel != NULL && !excel->isNull())
  85. {
  86. excel->dynamicCall("Quit()");
  87. }
  88. TC_FREE(excel);
  89. filename = "";
  90. sheetName = "";
  91. #endif // Q_OS_WIN
  92. }
  93. ExcelBase::ExcelBase(QObject* par):QObject(par)
  94. ,d_ptr(new ExcelBasePrivate(this))
  95. {
  96. }
  97. ExcelBase::~ExcelBase()
  98. {
  99. close();
  100. delete d_ptr;
  101. }
  102. bool ExcelBase::create(const QString& filename)
  103. {
  104. bool ret = false;
  105. #if defined(Q_OS_WIN)
  106. Q_D(ExcelBase);
  107. d->construct();
  108. if (d->books != NULL && ! d->books->isNull())
  109. {
  110. d->books->dynamicCall("Add");
  111. d->book = d->excel->querySubObject("ActiveWorkBook");
  112. d->sheets = d->book ->querySubObject("WorkSheets" );
  113. currentSheet();
  114. d->filename = filename;
  115. ret = true;
  116. }
  117. #else
  118. Q_UNUSED(filename)
  119. #endif // Q_OS_WIN
  120. return ret;
  121. }
  122. bool ExcelBase::open(const QString& filename)
  123. {
  124. bool ret = false;
  125. #if defined(Q_OS_WIN)
  126. Q_D(ExcelBase);
  127. d->construct();
  128. if ( d->books != NULL && ! d->books->isNull() )
  129. {
  130. d->book = d->books->querySubObject("Open(QString, QVariant)", filename, 0);
  131. ret = d->book != NULL && ! d->book->isNull();
  132. if ( ret )
  133. {
  134. d->sheets = d->book->querySubObject("WorkSheets");
  135. d->filename = filename;
  136. currentSheet();
  137. }
  138. }
  139. #else
  140. Q_UNUSED(filename)
  141. #endif // Q_OS_WIN
  142. return ret;
  143. }
  144. void ExcelBase::saveAs(const QString& filename)
  145. {
  146. #if defined(Q_OS_WIN)
  147. Q_D(ExcelBase);
  148. if ( d->book != NULL && ! d->book->isNull() )
  149. {
  150. d->filename = filename;
  151. QString strPath = d->filename;
  152. strPath = strPath.replace('/','\\');
  153. qDebug()<<strPath;
  154. d->book->dynamicCall("SaveAs(const QString&,int,const QString&,const QString&,bool,bool)", strPath
  155. ,56,QString(""),QString(""),false,false);
  156. }
  157. #else
  158. Q_UNUSED(filename)
  159. #endif // Q_OS_WIN
  160. }
  161. ///
  162. /// \brief 保存excel
  163. ///
  164. void ExcelBase::save()
  165. {
  166. #if defined(Q_OS_WIN)
  167. Q_D(ExcelBase);
  168. if(d->filename.isEmpty())
  169. return;
  170. saveAs(d->filename);
  171. #else
  172. Q_UNUSED(filename)
  173. #endif // Q_OS_WIN
  174. }
  175. void ExcelBase::close()
  176. {
  177. #if defined(Q_OS_WIN)
  178. Q_D(ExcelBase);
  179. d->destory();
  180. #endif // Q_OS_WIN
  181. }
  182. void ExcelBase::kick()
  183. {
  184. #if defined(Q_OS_WIN)
  185. Q_D(ExcelBase);
  186. if (d->excel != NULL && !d->excel->isNull())
  187. {
  188. d->excel->setProperty("Visible", true);
  189. }
  190. TC_FREE(d->sheet );
  191. TC_FREE(d->sheets);
  192. TC_FREE(d->book );
  193. TC_FREE(d->books );
  194. TC_FREE(d->excel );
  195. d->destory();
  196. #endif // Q_OS_WIN
  197. }
  198. QStringList ExcelBase::sheetNames()
  199. {
  200. QStringList ret;
  201. #if defined(Q_OS_WIN)
  202. Q_D(ExcelBase);
  203. if (d->sheets != NULL && !d->sheets->isNull())
  204. {
  205. int sheetCount = d->sheets->property("Count").toInt();
  206. for (int i = 1; i <= sheetCount; i++)
  207. {
  208. QAxObject* sheet = d->sheets->querySubObject("Item(int)", i);
  209. if(NULL == sheet || sheet->isNull())
  210. continue;
  211. ret.append(sheet->property("Name").toString());
  212. delete sheet;
  213. }
  214. }
  215. #endif // Q_OS_WIN
  216. return ret;
  217. }
  218. QString ExcelBase::currentSheetName()
  219. {
  220. Q_D(ExcelBase);
  221. return d->sheetName;
  222. }
  223. void ExcelBase::setVisible(bool value)
  224. {
  225. #if defined(Q_OS_WIN)
  226. Q_D(ExcelBase);
  227. if (d->excel != NULL && !d->excel->isNull())
  228. {
  229. d->excel->setProperty("Visible", value);
  230. }
  231. #else
  232. Q_UNUSED(value)
  233. #endif // Q_OS_WIN
  234. }
  235. void ExcelBase::setCaption(const QString& value)
  236. {
  237. #if defined(Q_OS_WIN)
  238. Q_D(ExcelBase);
  239. if (d->excel != NULL && !d->excel->isNull())
  240. {
  241. d->excel->setProperty("Caption", value);
  242. }
  243. #else
  244. Q_UNUSED(value)
  245. #endif // Q_OS_WIN
  246. }
  247. bool ExcelBase::addBook()
  248. {
  249. bool ret = false;
  250. #if defined(Q_OS_WIN)
  251. Q_D(ExcelBase);
  252. if (d->excel != NULL && !d->excel->isNull())
  253. {
  254. TC_FREE(d->sheet );
  255. TC_FREE(d->sheets);
  256. TC_FREE(d->book );
  257. TC_FREE(d->books );
  258. d->books = d->excel->querySubObject("WorkBooks");
  259. ret = d->books != NULL && !d->books->isNull();
  260. }
  261. #endif // Q_OS_WIN
  262. return ret;
  263. }
  264. bool ExcelBase::currentSheet()
  265. {
  266. bool ret = false;
  267. #if defined(Q_OS_WIN)
  268. Q_D(ExcelBase);
  269. TC_FREE(d->sheet);
  270. if (d->excel != NULL && !d->excel->isNull())
  271. {
  272. TC_FREE(d->sheet);
  273. d->sheet = d->excel->querySubObject("ActiveWorkBook");
  274. ret = d->sheet != NULL && !d->sheet->isNull();
  275. d->sheetName = ret ? d->sheet->property("Name").toString() : "";
  276. }
  277. #endif // Q_OS_WIN
  278. return ret;
  279. }
  280. bool ExcelBase::setCurrentSheet(int index)
  281. {
  282. bool ret = false;
  283. #if defined(Q_OS_WIN)
  284. Q_D(ExcelBase);
  285. if (d->sheets != NULL && !d->sheets->isNull())
  286. {
  287. TC_FREE(d->sheet);
  288. d->sheet = d->sheets->querySubObject("Item(int)", index);
  289. ret = d->sheet != NULL && ! d->sheet->isNull();
  290. if(ret)
  291. {
  292. d->sheet->dynamicCall("Activate(void)");
  293. }
  294. d->sheetName = ret ? d->sheet->property("Name").toString() : "";
  295. }
  296. #else
  297. Q_UNUSED(index)
  298. #endif // Q_OS_WIN
  299. return ret;
  300. }
  301. int ExcelBase::sheetCount()
  302. {
  303. int ret = 0;
  304. #if defined(Q_OS_WIN)
  305. Q_D(ExcelBase);
  306. if (d->sheets != NULL && ! d->sheets->isNull())
  307. {
  308. ret = d->sheets->property("Count").toInt();
  309. }
  310. #endif // Q_OS_WIN
  311. return ret;
  312. }
  313. void ExcelBase::cellFormat(int row, int col, const QString& format)
  314. {
  315. #if defined(Q_OS_WIN)
  316. Q_D(ExcelBase);
  317. if (d->sheet != NULL && ! d->sheet->isNull())
  318. {
  319. QAxObject* range = d->sheet->querySubObject("Cells(int, int)", row, col);
  320. range->setProperty("NumberFormatLocal", format);
  321. delete range;
  322. }
  323. #else
  324. Q_UNUSED(row)
  325. Q_UNUSED(col)
  326. Q_UNUSED(format)
  327. #endif // Q_OS_WIN
  328. }
  329. void ExcelBase::cellAlign(int row, int col, Alignment hAlign, Alignment vAlign)
  330. {
  331. #if defined(Q_OS_WIN)
  332. Q_D(ExcelBase);
  333. if (d->sheet != NULL && !d->sheet->isNull())
  334. {
  335. QAxObject* range = d->sheet->querySubObject("Cells(int, int)", row, col);
  336. range->setProperty("HorizontalAlignment", hAlign);
  337. range->setProperty("VerticalAlignment", vAlign);
  338. delete range;
  339. }
  340. #else
  341. Q_UNUSED(row)
  342. Q_UNUSED(col)
  343. Q_UNUSED(hAlign)
  344. Q_UNUSED(vAlign)
  345. #endif // Q_OS_WIN
  346. }
  347. QVariant ExcelBase::read(int row, int col)
  348. {
  349. QVariant ret;
  350. #if defined(Q_OS_WIN)
  351. Q_D(ExcelBase);
  352. if (d->sheet != NULL && ! d->sheet->isNull())
  353. {
  354. QAxObject* range = d->sheet->querySubObject("Cells(int, int)", row, col);
  355. //ret = range->property("Value");
  356. ret = range->dynamicCall("Value()");
  357. delete range;
  358. }
  359. #else
  360. Q_UNUSED(row)
  361. Q_UNUSED(col)
  362. #endif // Q_OS_WIN
  363. return ret;
  364. }
  365. void ExcelBase::write(int row, int col, const QVariant& value)
  366. {
  367. #if defined(Q_OS_WIN)
  368. Q_D(ExcelBase);
  369. if (d->sheet != NULL && ! d->sheet->isNull())
  370. {
  371. QAxObject* range = d->sheet->querySubObject("Cells(int, int)", row, col);
  372. // range->setProperty("Value", value);
  373. range->setProperty("Value2", value);//这里不能用Value他只能在office中写入,不能写入wps。 Value2两个都可以写入
  374. delete range;
  375. }
  376. #else
  377. Q_UNUSED(row)
  378. Q_UNUSED(col)
  379. Q_UNUSED(value)
  380. #endif // Q_OS_WIN
  381. }
  382. bool ExcelBase::usedRange(int& rowStart, int& colStart, int& rowEnd, int& colEnd)
  383. {
  384. bool ret = false;
  385. #if defined(Q_OS_WIN)
  386. Q_D(ExcelBase);
  387. if (d->sheet != NULL && ! d->sheet->isNull())
  388. {
  389. QAxObject* urange = d->sheet->querySubObject("UsedRange");
  390. rowStart = urange->property("Row" ).toInt();
  391. colStart = urange->property("Column").toInt();
  392. rowEnd = urange->querySubObject("Rows" )->property("Count").toInt();
  393. colEnd = urange->querySubObject("Columns")->property("Count").toInt();
  394. delete urange;
  395. ret = true;
  396. }
  397. #else
  398. Q_UNUSED(rowStart)
  399. Q_UNUSED(colStart)
  400. Q_UNUSED(rowEnd)
  401. Q_UNUSED(colEnd)
  402. #endif // Q_OS_WIN
  403. return ret;
  404. }
  405. ///
  406. /// \brief 读取整个sheet
  407. /// \return
  408. ///
  409. QVariant ExcelBase::readAll()
  410. {
  411. QVariant var;
  412. #if defined(Q_OS_WIN)
  413. Q_D(ExcelBase);
  414. if (d->sheet != NULL && ! d->sheet->isNull())
  415. {
  416. QAxObject *usedRange = d->sheet->querySubObject("UsedRange");
  417. if(NULL == usedRange || usedRange->isNull())
  418. {
  419. return var;
  420. }
  421. var = usedRange->dynamicCall("Value");
  422. delete usedRange;
  423. }
  424. #endif
  425. return var;
  426. }
  427. ///
  428. /// \brief 读取整个sheet的数据,并放置到cells中
  429. /// \param cells
  430. ///
  431. void ExcelBase::readAll(QList<QList<QVariant> > &cells)
  432. {
  433. #if defined(Q_OS_WIN)
  434. castVariant2ListListVariant(readAll(),cells);
  435. #else
  436. Q_UNUSED(cells);
  437. #endif
  438. }
  439. ///
  440. /// \brief 写入一个表格内容
  441. /// \param cells
  442. /// \return 成功写入返回true
  443. /// \see readAllSheet
  444. ///
  445. bool ExcelBase::writeCurrentSheet(const QList<QList<QVariant> > &cells)
  446. {
  447. #if defined(Q_OS_WIN)
  448. Q_D(ExcelBase);
  449. if(cells.size() <= 0)
  450. return false;
  451. if(NULL == d->sheet || d->sheet->isNull())
  452. return false;
  453. int row = cells.size();
  454. int col = cells.at(0).size();
  455. QString rangStr;
  456. convertToColName(col,rangStr);
  457. rangStr += QString::number(row);
  458. rangStr = "A1:" + rangStr;
  459. qDebug()<<rangStr;
  460. // QAxObject *range = d->sheet->querySubObject("Range(const QString&)",rangStr);
  461. QVariantList params;
  462. params<<"A1"<<rangStr;
  463. QAxObject *range = d->sheet->querySubObject("Range(QVariant,QVariant)",params);
  464. range->setProperty("ColumnWidth",25);
  465. if(NULL == range || range->isNull())
  466. {
  467. return false;
  468. }
  469. bool succ = false;
  470. QVariant var;
  471. castListListVariant2Variant(cells,var);
  472. succ = range->setProperty("Value2", var);//这里不能用Value他只能在office中写入,不能写入wps。 Value2两个都可以写入
  473. delete range;
  474. return succ;
  475. #else
  476. return true;
  477. #endif
  478. }
  479. ///
  480. /// \brief 把列数转换为excel的字母列号
  481. /// \param data 大于0的数
  482. /// \return 字母列号,如1->A 26->Z 27 AA
  483. ///
  484. void ExcelBase::convertToColName(int data, QString &res)
  485. {
  486. Q_ASSERT(data>0 && data<65535);
  487. int tempData = data / 26;
  488. if(tempData > 0)
  489. {
  490. int mode = data % 26;
  491. convertToColName(mode,res);
  492. convertToColName(tempData,res);
  493. }
  494. else
  495. {
  496. res=(to26AlphabetString(data)+res);
  497. }
  498. }
  499. ///
  500. /// \brief 数字转换为26字母
  501. ///
  502. /// 1->A 26->Z
  503. /// \param data
  504. /// \return
  505. ///
  506. QString ExcelBase::to26AlphabetString(int data)
  507. {
  508. QChar ch = data + 0x40;//A对应0x41
  509. return QString(ch);
  510. }
  511. ///
  512. /// \brief QList<QList<QVariant> >转换为QVariant
  513. /// \param cells
  514. /// \return
  515. ///
  516. void ExcelBase::castListListVariant2Variant(const QList<QList<QVariant> > &cells, QVariant &res)
  517. {
  518. QVariantList vars;
  519. const int rows = cells.size();
  520. for(int i=0;i<rows;++i)
  521. {
  522. vars.append(QVariant(cells[i]));
  523. }
  524. res = QVariant(vars);
  525. }
  526. ///
  527. /// \brief 把QVariant转为QList<QList<QVariant> >
  528. /// \param var
  529. /// \param res
  530. ///
  531. void ExcelBase::castVariant2ListListVariant(const QVariant &var, QList<QList<QVariant> > &res)
  532. {
  533. QVariantList varRows = var.toList();
  534. if(varRows.isEmpty())
  535. {
  536. return;
  537. }
  538. const int rowCount = varRows.size();
  539. QVariantList rowData;
  540. for(int i=0;i<rowCount;++i)
  541. {
  542. rowData = varRows[i].toList();
  543. res.push_back(rowData);
  544. }
  545. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/170591?site
推荐阅读
相关标签
  

闽ICP备14008679号