xlsxworkbook.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. // xlsxworkbook.cpp
  2. #include <QtGlobal>
  3. #include <QXmlStreamWriter>
  4. #include <QXmlStreamReader>
  5. #include <QFile>
  6. #include <QBuffer>
  7. #include <QDir>
  8. #include <QtDebug>
  9. #include "xlsxworkbook.h"
  10. #include "xlsxworkbook_p.h"
  11. #include "xlsxsharedstrings_p.h"
  12. #include "xlsxworksheet.h"
  13. #include "xlsxchartsheet.h"
  14. #include "xlsxstyles_p.h"
  15. #include "xlsxformat.h"
  16. #include "xlsxworksheet_p.h"
  17. #include "xlsxformat_p.h"
  18. #include "xlsxmediafile_p.h"
  19. #include "xlsxutility_p.h"
  20. #include "xlsxchart.h"
  21. QT_BEGIN_NAMESPACE_XLSX
  22. WorkbookPrivate::WorkbookPrivate(Workbook *q, Workbook::CreateFlag flag) :
  23. AbstractOOXmlFilePrivate(q, flag)
  24. {
  25. sharedStrings = QSharedPointer<SharedStrings> (new SharedStrings(flag));
  26. styles = QSharedPointer<Styles>(new Styles(flag));
  27. theme = QSharedPointer<Theme>(new Theme(flag));
  28. x_window = 240;
  29. y_window = 15;
  30. window_width = 16095;
  31. window_height = 9660;
  32. strings_to_numbers_enabled = false;
  33. strings_to_hyperlinks_enabled = true;
  34. html_to_richstring_enabled = false;
  35. date1904 = false;
  36. defaultDateFormat = QStringLiteral("yyyy-mm-dd");
  37. activesheetIndex = 0;
  38. firstsheet = 0;
  39. table_count = 0;
  40. last_worksheet_index = 0;
  41. last_chartsheet_index = 0;
  42. last_sheet_id = 0;
  43. }
  44. Workbook::Workbook(CreateFlag flag)
  45. : AbstractOOXmlFile(new WorkbookPrivate(this, flag))
  46. {
  47. }
  48. Workbook::~Workbook()
  49. {
  50. }
  51. bool Workbook::isDate1904() const
  52. {
  53. Q_D(const Workbook);
  54. return d->date1904;
  55. }
  56. /*!
  57. Excel for Windows uses a default epoch of 1900 and Excel
  58. for Mac uses an epoch of 1904. However, Excel on either
  59. platform will convert automatically between one system
  60. and the other. Qt Xlsx stores dates in the 1900 format
  61. by default.
  62. \note This function should be called before any date/time
  63. has been written.
  64. */
  65. void Workbook::setDate1904(bool date1904)
  66. {
  67. Q_D(Workbook);
  68. d->date1904 = date1904;
  69. }
  70. /*
  71. Enable the worksheet.write() method to convert strings
  72. to numbers, where possible, using float() in order to avoid
  73. an Excel warning about "Numbers Stored as Text".
  74. The default is false
  75. */
  76. void Workbook::setStringsToNumbersEnabled(bool enable)
  77. {
  78. Q_D(Workbook);
  79. d->strings_to_numbers_enabled = enable;
  80. }
  81. bool Workbook::isStringsToNumbersEnabled() const
  82. {
  83. Q_D(const Workbook);
  84. return d->strings_to_numbers_enabled;
  85. }
  86. void Workbook::setStringsToHyperlinksEnabled(bool enable)
  87. {
  88. Q_D(Workbook);
  89. d->strings_to_hyperlinks_enabled = enable;
  90. }
  91. bool Workbook::isStringsToHyperlinksEnabled() const
  92. {
  93. Q_D(const Workbook);
  94. return d->strings_to_hyperlinks_enabled;
  95. }
  96. void Workbook::setHtmlToRichStringEnabled(bool enable)
  97. {
  98. Q_D(Workbook);
  99. d->html_to_richstring_enabled = enable;
  100. }
  101. bool Workbook::isHtmlToRichStringEnabled() const
  102. {
  103. Q_D(const Workbook);
  104. return d->html_to_richstring_enabled;
  105. }
  106. QString Workbook::defaultDateFormat() const
  107. {
  108. Q_D(const Workbook);
  109. return d->defaultDateFormat;
  110. }
  111. void Workbook::setDefaultDateFormat(const QString &format)
  112. {
  113. Q_D(Workbook);
  114. d->defaultDateFormat = format;
  115. }
  116. /*!
  117. * \brief Create a defined name in the workbook.
  118. * \param name The defined name
  119. * \param formula The cell or range that the defined name refers to.
  120. * \param comment
  121. * \param scope The name of one worksheet, or empty which means golbal scope.
  122. * \return Return false if the name invalid.
  123. */
  124. bool Workbook::defineName(const QString &name, const QString &formula, const QString &comment, const QString &scope)
  125. {
  126. Q_D(Workbook);
  127. //Remove the = sign from the formula if it exists.
  128. QString formulaString = formula;
  129. if (formulaString.startsWith(QLatin1Char('=')))
  130. formulaString = formula.mid(1);
  131. int id=-1;
  132. if (!scope.isEmpty()) {
  133. for (int i=0; i<d->sheets.size(); ++i) {
  134. if (d->sheets[i]->sheetName() == scope) {
  135. id = d->sheets[i]->sheetId();
  136. break;
  137. }
  138. }
  139. }
  140. d->definedNamesList.append(XlsxDefineNameData(name, formulaString, comment, id));
  141. return true;
  142. }
  143. AbstractSheet *Workbook::addSheet(const QString &name, AbstractSheet::SheetType type)
  144. {
  145. Q_D(Workbook);
  146. return insertSheet(d->sheets.size(), name, type);
  147. }
  148. /*!
  149. * \internal
  150. */
  151. QStringList Workbook::worksheetNames() const
  152. {
  153. Q_D(const Workbook);
  154. return d->sheetNames;
  155. }
  156. /*!
  157. * \internal
  158. * Used only when load the xlsx file!!
  159. */
  160. AbstractSheet *Workbook::addSheet(const QString &name, int sheetId, AbstractSheet::SheetType type)
  161. {
  162. Q_D(Workbook);
  163. if (sheetId > d->last_sheet_id)
  164. d->last_sheet_id = sheetId;
  165. AbstractSheet *sheet = NULL;
  166. if (type == AbstractSheet::ST_WorkSheet)
  167. {
  168. // create work sheet (value sheet)
  169. sheet = new Worksheet(name, sheetId, this, F_LoadFromExists);
  170. }
  171. else if (type == AbstractSheet::ST_ChartSheet)
  172. {
  173. // create chart sheet
  174. sheet = new Chartsheet(name, sheetId, this, F_LoadFromExists);
  175. }
  176. else
  177. {
  178. qWarning("unsupported sheet type.");
  179. Q_ASSERT(false);
  180. }
  181. d->sheets.append(QSharedPointer<AbstractSheet>(sheet));
  182. d->sheetNames.append(name);
  183. return sheet;
  184. }
  185. AbstractSheet *Workbook::insertSheet(int index, const QString &name, AbstractSheet::SheetType type)
  186. {
  187. Q_D(Workbook);
  188. QString sheetName = createSafeSheetName(name);
  189. if(index > d->last_sheet_id){
  190. //User tries to insert, where no sheet has gone before.
  191. return 0;
  192. }
  193. if (!sheetName.isEmpty()) {
  194. //If user given an already in-used name, we should not continue any more!
  195. if (d->sheetNames.contains(sheetName))
  196. return 0;
  197. } else {
  198. if (type == AbstractSheet::ST_WorkSheet) {
  199. do {
  200. ++d->last_worksheet_index;
  201. sheetName = QStringLiteral("Sheet%1").arg(d->last_worksheet_index);
  202. } while (d->sheetNames.contains(sheetName));
  203. } else if (type == AbstractSheet::ST_ChartSheet) {
  204. do {
  205. ++d->last_chartsheet_index;
  206. sheetName = QStringLiteral("Chart%1").arg(d->last_chartsheet_index);
  207. } while (d->sheetNames.contains(sheetName));
  208. } else {
  209. qWarning("unsupported sheet type.");
  210. return 0;
  211. }
  212. }
  213. ++d->last_sheet_id;
  214. AbstractSheet *sheet = NULL;
  215. if ( type == AbstractSheet::ST_WorkSheet )
  216. {
  217. sheet = new Worksheet(sheetName, d->last_sheet_id, this, F_NewFromScratch);
  218. }
  219. else if ( type == AbstractSheet::ST_ChartSheet )
  220. {
  221. sheet = new Chartsheet(sheetName, d->last_sheet_id, this, F_NewFromScratch);
  222. }
  223. else
  224. {
  225. qWarning("unsupported sheet type.");
  226. Q_ASSERT(false);
  227. }
  228. d->sheets.insert(index, QSharedPointer<AbstractSheet>(sheet));
  229. d->sheetNames.insert(index, sheetName);
  230. d->activesheetIndex = index;
  231. return sheet;
  232. }
  233. /*!
  234. * Returns current active worksheet.
  235. */
  236. AbstractSheet *Workbook::activeSheet() const
  237. {
  238. Q_D(const Workbook);
  239. if (d->sheets.isEmpty())
  240. const_cast<Workbook*>(this)->addSheet();
  241. return d->sheets[d->activesheetIndex].data();
  242. }
  243. bool Workbook::setActiveSheet(int index)
  244. {
  245. Q_D(Workbook);
  246. if (index < 0 || index >= d->sheets.size()) {
  247. //warning
  248. return false;
  249. }
  250. d->activesheetIndex = index;
  251. return true;
  252. }
  253. /*!
  254. * Rename the worksheet at the \a index to \a newName.
  255. */
  256. bool Workbook::renameSheet(int index, const QString &newName)
  257. {
  258. Q_D(Workbook);
  259. QString name = createSafeSheetName(newName);
  260. if (index < 0 || index >= d->sheets.size())
  261. return false;
  262. //If user given an already in-used name, return false
  263. for (int i=0; i<d->sheets.size(); ++i) {
  264. if (d->sheets[i]->sheetName() == name)
  265. return false;
  266. }
  267. d->sheets[index]->setSheetName(name);
  268. d->sheetNames[index] = name;
  269. return true;
  270. }
  271. /*!
  272. * Remove the worksheet at pos \a index.
  273. */
  274. bool Workbook::deleteSheet(int index)
  275. {
  276. Q_D(Workbook);
  277. if (d->sheets.size() <= 1)
  278. return false;
  279. if (index < 0 || index >= d->sheets.size())
  280. return false;
  281. d->sheets.removeAt(index);
  282. d->sheetNames.removeAt(index);
  283. return true;
  284. }
  285. /*!
  286. * Moves the worksheet form \a srcIndex to \a distIndex.
  287. */
  288. bool Workbook::moveSheet(int srcIndex, int distIndex)
  289. {
  290. Q_D(Workbook);
  291. if (srcIndex == distIndex)
  292. return false;
  293. if (srcIndex < 0 || srcIndex >= d->sheets.size())
  294. return false;
  295. QSharedPointer<AbstractSheet> sheet = d->sheets.takeAt(srcIndex);
  296. d->sheetNames.takeAt(srcIndex);
  297. if (distIndex >= 0 || distIndex <= d->sheets.size()) {
  298. d->sheets.insert(distIndex, sheet);
  299. d->sheetNames.insert(distIndex, sheet->sheetName());
  300. } else {
  301. d->sheets.append(sheet);
  302. d->sheetNames.append(sheet->sheetName());
  303. }
  304. return true;
  305. }
  306. bool Workbook::copySheet(int index, const QString &newName)
  307. {
  308. Q_D(Workbook);
  309. if (index < 0 || index >= d->sheets.size())
  310. return false;
  311. QString worksheetName = createSafeSheetName(newName);
  312. if (!newName.isEmpty()) {
  313. //If user given an already in-used name, we should not continue any more!
  314. if (d->sheetNames.contains(newName))
  315. return false;
  316. } else {
  317. int copy_index = 1;
  318. do {
  319. ++copy_index;
  320. worksheetName = QStringLiteral("%1(%2)").arg(d->sheets[index]->sheetName()).arg(copy_index);
  321. } while (d->sheetNames.contains(worksheetName));
  322. }
  323. ++d->last_sheet_id;
  324. AbstractSheet *sheet = d->sheets[index]->copy(worksheetName, d->last_sheet_id);
  325. d->sheets.append(QSharedPointer<AbstractSheet> (sheet));
  326. d->sheetNames.append(sheet->sheetName());
  327. return true; // #162
  328. }
  329. /*!
  330. * Returns count of worksheets.
  331. */
  332. int Workbook::sheetCount() const
  333. {
  334. Q_D(const Workbook);
  335. return d->sheets.count();
  336. }
  337. /*!
  338. * Returns the sheet object at index \a sheetIndex.
  339. */
  340. AbstractSheet *Workbook::sheet(int index) const
  341. {
  342. Q_D(const Workbook);
  343. if (index < 0 || index >= d->sheets.size())
  344. return 0;
  345. return d->sheets.at(index).data();
  346. }
  347. SharedStrings *Workbook::sharedStrings() const
  348. {
  349. Q_D(const Workbook);
  350. return d->sharedStrings.data();
  351. }
  352. Styles *Workbook::styles()
  353. {
  354. Q_D(Workbook);
  355. return d->styles.data();
  356. }
  357. Theme *Workbook::theme()
  358. {
  359. Q_D(Workbook);
  360. return d->theme.data();
  361. }
  362. /*!
  363. * \internal
  364. *
  365. * Unlike media files, drawing file is a property of the sheet.
  366. */
  367. QList<Drawing *> Workbook::drawings()
  368. {
  369. Q_D(Workbook);
  370. QList<Drawing *> ds;
  371. for (int i=0; i<d->sheets.size(); ++i) {
  372. QSharedPointer<AbstractSheet> sheet = d->sheets[i];
  373. if (sheet->drawing())
  374. ds.append(sheet->drawing());
  375. }
  376. return ds;
  377. }
  378. /*!
  379. * \internal
  380. */
  381. QList<QSharedPointer<AbstractSheet> > Workbook::getSheetsByTypes(AbstractSheet::SheetType type) const
  382. {
  383. Q_D(const Workbook);
  384. QList<QSharedPointer<AbstractSheet> > list;
  385. for (int i=0; i<d->sheets.size(); ++i) {
  386. if (d->sheets[i]->sheetType() == type)
  387. list.append(d->sheets[i]);
  388. }
  389. return list;
  390. }
  391. void Workbook::saveToXmlFile(QIODevice *device) const
  392. {
  393. Q_D(const Workbook);
  394. d->relationships->clear();
  395. if (d->sheets.isEmpty())
  396. const_cast<Workbook *>(this)->addSheet();
  397. QXmlStreamWriter writer(device);
  398. writer.writeStartDocument(QStringLiteral("1.0"), true);
  399. writer.writeStartElement(QStringLiteral("workbook"));
  400. writer.writeAttribute(QStringLiteral("xmlns"), QStringLiteral("http://schemas.openxmlformats.org/spreadsheetml/2006/main"));
  401. writer.writeAttribute(QStringLiteral("xmlns:r"), QStringLiteral("http://schemas.openxmlformats.org/officeDocument/2006/relationships"));
  402. writer.writeEmptyElement(QStringLiteral("fileVersion"));
  403. writer.writeAttribute(QStringLiteral("appName"), QStringLiteral("xl"));
  404. writer.writeAttribute(QStringLiteral("lastEdited"), QStringLiteral("4"));
  405. writer.writeAttribute(QStringLiteral("lowestEdited"), QStringLiteral("4"));
  406. writer.writeAttribute(QStringLiteral("rupBuild"), QStringLiteral("4505"));
  407. // writer.writeAttribute(QStringLiteral("codeName"), QStringLiteral("{37E998C4-C9E5-D4B9-71C8-EB1FF731991C}"));
  408. writer.writeEmptyElement(QStringLiteral("workbookPr"));
  409. if (d->date1904)
  410. writer.writeAttribute(QStringLiteral("date1904"), QStringLiteral("1"));
  411. writer.writeAttribute(QStringLiteral("defaultThemeVersion"), QStringLiteral("124226"));
  412. writer.writeStartElement(QStringLiteral("bookViews"));
  413. writer.writeEmptyElement(QStringLiteral("workbookView"));
  414. writer.writeAttribute(QStringLiteral("xWindow"), QString::number(d->x_window));
  415. writer.writeAttribute(QStringLiteral("yWindow"), QString::number(d->y_window));
  416. writer.writeAttribute(QStringLiteral("windowWidth"), QString::number(d->window_width));
  417. writer.writeAttribute(QStringLiteral("windowHeight"), QString::number(d->window_height));
  418. //Store the firstSheet when it isn't the default
  419. //For example, when "the first sheet 0 is hidden", the first sheet will be 1
  420. if (d->firstsheet > 0)
  421. writer.writeAttribute(QStringLiteral("firstSheet"), QString::number(d->firstsheet + 1));
  422. //Store the activeTab when it isn't the first sheet
  423. if (d->activesheetIndex > 0)
  424. writer.writeAttribute(QStringLiteral("activeTab"), QString::number(d->activesheetIndex));
  425. writer.writeEndElement();//bookViews
  426. writer.writeStartElement(QStringLiteral("sheets"));
  427. int worksheetIndex = 0;
  428. int chartsheetIndex = 0;
  429. for (int i=0; i<d->sheets.size(); ++i) {
  430. QSharedPointer<AbstractSheet> sheet = d->sheets[i];
  431. writer.writeEmptyElement(QStringLiteral("sheet"));
  432. writer.writeAttribute(QStringLiteral("name"), sheet->sheetName());
  433. writer.writeAttribute(QStringLiteral("sheetId"), QString::number(sheet->sheetId()));
  434. if (sheet->sheetState() == AbstractSheet::SS_Hidden)
  435. writer.writeAttribute(QStringLiteral("state"), QStringLiteral("hidden"));
  436. else if (sheet->sheetState() == AbstractSheet::SS_VeryHidden)
  437. writer.writeAttribute(QStringLiteral("state"), QStringLiteral("veryHidden"));
  438. if (sheet->sheetType() == AbstractSheet::ST_WorkSheet)
  439. d->relationships->addDocumentRelationship(QStringLiteral("/worksheet"), QStringLiteral("worksheets/sheet%1.xml").arg(++worksheetIndex));
  440. else
  441. d->relationships->addDocumentRelationship(QStringLiteral("/chartsheet"), QStringLiteral("chartsheets/sheet%1.xml").arg(++chartsheetIndex));
  442. writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
  443. }
  444. writer.writeEndElement();//sheets
  445. if (d->externalLinks.size() > 0) {
  446. writer.writeStartElement(QStringLiteral("externalReferences"));
  447. for (int i=0; i<d->externalLinks.size(); ++i) {
  448. writer.writeEmptyElement(QStringLiteral("externalReference"));
  449. d->relationships->addDocumentRelationship(QStringLiteral("/externalLink"), QStringLiteral("externalLinks/externalLink%1.xml").arg(i+1));
  450. writer.writeAttribute(QStringLiteral("r:id"), QStringLiteral("rId%1").arg(d->relationships->count()));
  451. }
  452. writer.writeEndElement();//externalReferences
  453. }
  454. if (!d->definedNamesList.isEmpty()) {
  455. writer.writeStartElement(QStringLiteral("definedNames"));
  456. for (const XlsxDefineNameData &data : d->definedNamesList) {
  457. writer.writeStartElement(QStringLiteral("definedName"));
  458. writer.writeAttribute(QStringLiteral("name"), data.name);
  459. if (!data.comment.isEmpty())
  460. writer.writeAttribute(QStringLiteral("comment"), data.comment);
  461. if (data.sheetId != -1) {
  462. //find the local index of the sheet.
  463. for (int i=0; i<d->sheets.size(); ++i) {
  464. if (d->sheets[i]->sheetId() == data.sheetId) {
  465. writer.writeAttribute(QStringLiteral("localSheetId"), QString::number(i));
  466. break;
  467. }
  468. }
  469. }
  470. writer.writeCharacters(data.formula);
  471. writer.writeEndElement();//definedName
  472. }
  473. writer.writeEndElement();//definedNames
  474. }
  475. writer.writeStartElement(QStringLiteral("calcPr"));
  476. writer.writeAttribute(QStringLiteral("calcId"), QStringLiteral("124519"));
  477. writer.writeEndElement(); //calcPr
  478. writer.writeEndElement();//workbook
  479. writer.writeEndDocument();
  480. d->relationships->addDocumentRelationship(QStringLiteral("/theme"), QStringLiteral("theme/theme1.xml"));
  481. d->relationships->addDocumentRelationship(QStringLiteral("/styles"), QStringLiteral("styles.xml"));
  482. if (!sharedStrings()->isEmpty())
  483. d->relationships->addDocumentRelationship(QStringLiteral("/sharedStrings"), QStringLiteral("sharedStrings.xml"));
  484. }
  485. bool Workbook::loadFromXmlFile(QIODevice *device)
  486. {
  487. Q_D(Workbook);
  488. QXmlStreamReader reader(device);
  489. while (!reader.atEnd())
  490. {
  491. QXmlStreamReader::TokenType token = reader.readNext();
  492. if (token == QXmlStreamReader::StartElement)
  493. {
  494. if (reader.name() == QLatin1String("sheet"))
  495. {
  496. QXmlStreamAttributes attributes = reader.attributes();
  497. const auto& name = attributes.value(QLatin1String("name")).toString();
  498. int sheetId = attributes.value(QLatin1String("sheetId")).toString().toInt();
  499. const auto& rId = attributes.value(QLatin1String("r:id")).toString();
  500. const auto& stateString = attributes.value(QLatin1String("state"));
  501. AbstractSheet::SheetState state = AbstractSheet::SS_Visible;
  502. if (stateString == QLatin1String("hidden"))
  503. state = AbstractSheet::SS_Hidden;
  504. else if (stateString == QLatin1String("veryHidden"))
  505. state = AbstractSheet::SS_VeryHidden;
  506. XlsxRelationship relationship = d->relationships->getRelationshipById(rId);
  507. AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet;
  508. if (relationship.type.endsWith(QLatin1String("/worksheet")))
  509. {
  510. type = AbstractSheet::ST_WorkSheet;
  511. }
  512. else if (relationship.type.endsWith(QLatin1String("/chartsheet")))
  513. {
  514. type = AbstractSheet::ST_ChartSheet;
  515. }
  516. else if (relationship.type.endsWith(QLatin1String("/dialogsheet")))
  517. {
  518. type = AbstractSheet::ST_DialogSheet;
  519. }
  520. else if (relationship.type.endsWith(QLatin1String("/xlMacrosheet")))
  521. {
  522. type = AbstractSheet::ST_MacroSheet;
  523. }
  524. else
  525. {
  526. qWarning() << "unknown sheet type : " << relationship.type ;
  527. }
  528. AbstractSheet *sheet = addSheet(name, sheetId, type);
  529. sheet->setSheetState(state);
  530. QString strFilePath = filePath();
  531. // const QString fullPath = QDir::cleanPath(splitPath(strFilePath).constFirst() + QLatin1String("/") + relationship.target);
  532. QString str = *( splitPath(strFilePath).begin() );
  533. str = str + QLatin1String("/");
  534. str = str + relationship.target;
  535. const QString fullPath = QDir::cleanPath( str );
  536. sheet->setFilePath(fullPath);
  537. }
  538. else if (reader.name() == QLatin1String("workbookPr"))
  539. {
  540. QXmlStreamAttributes attrs = reader.attributes();
  541. if (attrs.hasAttribute(QLatin1String("date1904")))
  542. d->date1904 = true;
  543. }
  544. else if (reader.name() == QLatin1String("bookviews"))
  545. {
  546. while (!(reader.name() == QLatin1String("bookviews") &&
  547. reader.tokenType() == QXmlStreamReader::EndElement))
  548. {
  549. reader.readNextStartElement();
  550. if (reader.tokenType() == QXmlStreamReader::StartElement)
  551. {
  552. if (reader.name() == QLatin1String("workbookView"))
  553. {
  554. QXmlStreamAttributes attrs = reader.attributes();
  555. if (attrs.hasAttribute(QLatin1String("xWindow")))
  556. d->x_window = attrs.value(QLatin1String("xWindow")).toString().toInt();
  557. if (attrs.hasAttribute(QLatin1String("yWindow")))
  558. d->y_window = attrs.value(QLatin1String("yWindow")).toString().toInt();
  559. if (attrs.hasAttribute(QLatin1String("windowWidth")))
  560. d->window_width = attrs.value(QLatin1String("windowWidth")).toString().toInt();
  561. if (attrs.hasAttribute(QLatin1String("windowHeight")))
  562. d->window_height = attrs.value(QLatin1String("windowHeight")).toString().toInt();
  563. if (attrs.hasAttribute(QLatin1String("firstSheet")))
  564. d->firstsheet = attrs.value(QLatin1String("firstSheet")).toString().toInt();
  565. if (attrs.hasAttribute(QLatin1String("activeTab")))
  566. d->activesheetIndex = attrs.value(QLatin1String("activeTab")).toString().toInt();
  567. }
  568. }
  569. }
  570. }
  571. else if (reader.name() == QLatin1String("externalReference"))
  572. {
  573. QXmlStreamAttributes attributes = reader.attributes();
  574. const QString rId = attributes.value(QLatin1String("r:id")).toString();
  575. XlsxRelationship relationship = d->relationships->getRelationshipById(rId);
  576. QSharedPointer<SimpleOOXmlFile> link(new SimpleOOXmlFile(F_LoadFromExists));
  577. QString str = *( splitPath(filePath()).begin() );
  578. str = str + QLatin1String("/");
  579. str = str + relationship.target;
  580. const QString fullPath = QDir::cleanPath( str );
  581. link->setFilePath(fullPath);
  582. d->externalLinks.append(link);
  583. } else if (reader.name() == QLatin1String("definedName")) {
  584. QXmlStreamAttributes attrs = reader.attributes();
  585. XlsxDefineNameData data;
  586. data.name = attrs.value(QLatin1String("name")).toString();
  587. if (attrs.hasAttribute(QLatin1String("comment")))
  588. data.comment = attrs.value(QLatin1String("comment")).toString();
  589. if (attrs.hasAttribute(QLatin1String("localSheetId"))) {
  590. int localId = attrs.value(QLatin1String("localSheetId")).toString().toInt();
  591. int sheetId = d->sheets.at(localId)->sheetId();
  592. data.sheetId = sheetId;
  593. }
  594. data.formula = reader.readElementText();
  595. d->definedNamesList.append(data);
  596. }
  597. }
  598. }
  599. return true;
  600. }
  601. /*!
  602. * \internal
  603. */
  604. QList<QSharedPointer<MediaFile> > Workbook::mediaFiles() const
  605. {
  606. Q_D(const Workbook);
  607. return d->mediaFiles;
  608. }
  609. /*!
  610. * \internal
  611. */
  612. void Workbook::addMediaFile(QSharedPointer<MediaFile> media, bool force)
  613. {
  614. Q_D(Workbook);
  615. if (!force)
  616. {
  617. for (int i=0; i<d->mediaFiles.size(); ++i)
  618. {
  619. if (d->mediaFiles[i]->hashKey() == media->hashKey())
  620. {
  621. media->setIndex(i);
  622. return;
  623. }
  624. }
  625. }
  626. media->setIndex(d->mediaFiles.size());
  627. d->mediaFiles.append(media);
  628. }
  629. /*!
  630. * \internal
  631. */
  632. QList<QSharedPointer<Chart> > Workbook::chartFiles() const
  633. {
  634. Q_D(const Workbook);
  635. return d->chartFiles;
  636. }
  637. /*!
  638. * \internal
  639. */
  640. void Workbook::addChartFile(QSharedPointer<Chart> chart)
  641. {
  642. Q_D(Workbook);
  643. if (!d->chartFiles.contains(chart))
  644. d->chartFiles.append(chart);
  645. }
  646. QT_END_NAMESPACE_XLSX