OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLDocument.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLDOCUMENT_HPP
2#define OPENXLSX_XLDOCUMENT_HPP
3
4#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif // _MSC_VER
9
10// ===== External Includes ===== //
11#include <algorithm> // std::find_if
12#include <gsl/gsl>
13#include <list>
14#include <map>
15#include <memory> // std::unique_ptr
16#include <shared_mutex> // std::shared_mutex
17#include <string>
18#include <string_view>
19#include <unordered_map> // O(1) shared string lookup
20
21// ===== OpenXLSX Includes ===== //
22#include "IZipArchive.hpp"
23#include "OpenXLSX-Exports.hpp"
24#include "XLChart.hpp"
25#include "XLCommandQuery.hpp"
26#include "XLComments.hpp"
27#include "XLContentTypes.hpp"
28#include "XLDrawing.hpp"
29#include "XLProperties.hpp"
30#include "XLRelationships.hpp"
31#include "XLSharedStrings.hpp"
32#include "XLStringArena.hpp"
33#include "XLStyles.hpp"
34#include "XLTables.hpp"
36#include "XLWorkbook.hpp"
37#include "XLXmlData.hpp"
38#include "XLZipArchive.hpp"
39#include "XLInternalAccess.hpp"
40
41namespace OpenXLSX
42{
43 constexpr const unsigned int pugi_parse_settings = pugi::parse_default | pugi::parse_ws_pcdata;
44
45 constexpr const bool XLForceOverwrite = true; // readability constant for 2nd parameter of XLDocument::saveAs
46 constexpr const bool XLDoNotOverwrite = false; // "
47
74
81 class OPENXLSX_EXPORT XLDocument final
82 {
83 public:
85 {
86 std::string formula;
87 uint32_t baseRow;
88 uint16_t baseCol;
89 };
90
91 // =========================================================================
92 // 受限的内部特权 API (Restricted Internal API)
93 // 只有持有 XLInternalAccess 通行证的类才能调用,普通用户代码无法编译。
94 // =========================================================================
95
96 [[nodiscard]] XLXmlData* getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow = false) {
97 return getXmlData(path, doNotThrow);
98 }
99
100 [[nodiscard]] const XLXmlData* getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow = false) const {
101 return getXmlData(path, doNotThrow);
102 }
103
104 XLXmlData* addXmlData(XLInternalAccess, const std::string& path, const std::string& id, XLContentType type) {
105 return &m_data.emplace_back(this, path, id, type);
106 }
107
108 std::map<void*, std::unordered_map<uint32_t, SharedFormula>>& sharedFormulas(XLInternalAccess) const {
109 return m_sharedFormulas;
110 }
111
112 //---------- Public Member Functions
113 public:
117 explicit XLDocument(const IZipArchive& zipArchive = XLZipArchive());
118
123 explicit XLDocument(std::string_view docPath, const IZipArchive& zipArchive = XLZipArchive());
124
129 explicit XLDocument(const std::string& docPath, const IZipArchive& zipArchive = XLZipArchive());
130
131 XLDocument(const XLDocument& other) = delete;
132 XLDocument(XLDocument&& other) noexcept = default;
133 ~XLDocument();
134 XLDocument& operator=(const XLDocument& other) = delete;
135 XLDocument& operator=(XLDocument&& other) noexcept = default;
136
137 void showWarnings();
138 void suppressWarnings();
139
144 void open(std::string_view fileName);
145
151 void open(std::string_view fileName, const std::string& password);
152
158 void create(std::string_view fileName, bool forceOverwrite = XLForceOverwrite);
159
164 [[deprecated]] void create(const std::string& fileName);
165
169 void close();
170
174 void save();
175 void addStreamedFile(std::string_view pathInZip, std::string_view tempFilePath);
176
182 void saveAs(std::string_view fileName, bool forceOverwrite = XLForceOverwrite);
183
190 void saveAs(std::string_view fileName, const std::string& password, bool forceOverwrite = XLForceOverwrite);
191
196 [[deprecated]] void saveAs(const std::string& fileName);
197
202 [[nodiscard]] std::string name() const;
203
208 [[nodiscard]] const std::string& path() const;
209
213 [[nodiscard]] XLContentTypes& contentTypes();
214
218 [[nodiscard]]
223 bool hasMacro() const;
224
229 void deleteMacro();
230
231 XLCustomProperties& customProperties();
232
237 [[nodiscard]] uint32_t nextTableId() const;
238
243 [[nodiscard]] XLWorkbook workbook() const;
244
248 [[nodiscard]] std::string property(XLProperty prop) const;
249
255 void setProperty(XLProperty prop, std::string_view value);
256
260 void deleteProperty(XLProperty theProperty);
261
265 [[nodiscard]] explicit operator bool() const;
266
270 [[nodiscard]] bool isOpen() const;
271
276 void setCompressionLevel(int level);
277
281 int compressionLevel() const;
282
286 void setDefaultAuthor(const std::string& author);
287
291 std::string defaultAuthor() const;
292
296 [[nodiscard]] XLStyles& styles();
297
301 [[nodiscard]] bool hasSheetRelationships(uint16_t sheetXmlNo, bool isChartsheet = false) const;
302 [[nodiscard]] bool hasSheetVmlDrawing(uint16_t sheetXmlNo) const;
303 [[nodiscard]] bool hasSheetComments(uint16_t sheetXmlNo) const;
304 [[nodiscard]] bool hasSheetThreadedComments(uint16_t sheetXmlNo) const;
305 [[nodiscard]] bool hasSheetDrawing(uint16_t sheetXmlNo) const;
306 [[nodiscard]] bool hasSheetTables(uint16_t sheetXmlNo) const;
307
308 XLRelationships sheetRelationships(uint16_t sheetXmlNo, bool isChartsheet = false);
309 XLDrawing sheetDrawing(uint16_t sheetXmlNo);
310 XLDrawing createDrawing();
311 XLDrawing drawing(std::string_view path);
312 XLVmlDrawing sheetVmlDrawing(uint16_t sheetXmlNo);
313 XLComments sheetComments(uint16_t sheetXmlNo);
314 XLThreadedComments sheetThreadedComments(uint16_t sheetXmlNo);
315 XLTables sheetTables(uint16_t sheetXmlNo);
316
317 class XLChart createChart(XLChartType type = XLChartType::Bar);
318
319 class XLPivotTable createPivotTable();
320 class XLPivotCacheDefinition createPivotCacheDefinition();
321 class XLPivotCacheRecords createPivotCacheRecords(std::string_view cacheDefPath);
322
323 std::string createTableSlicerCache(uint32_t tableId, uint32_t tableColumnId, std::string_view name, std::string_view sourceName);
324
325 std::string createPivotSlicerCache(uint32_t pivotCacheId,
326 uint32_t sheetId,
327 std::string_view pivotTableName,
328 std::string_view name,
329 std::string_view sourceName);
330
331 std::string createSlicer(std::string_view name, std::string_view cacheName, std::string_view caption);
332
339 std::string addImage(std::string_view name, std::string_view data);
340
344 [[nodiscard]] std::string getImage(std::string_view path) const;
345
346 XLRelationships drawingRelationships(std::string_view drawingPath);
350 XLRelationships xmlRelationships(std::string_view xmlPath);
354 XLRelationships& workbookRelationships() { return m_wbkRelationships; }
355
360 [[nodiscard]] IZipArchive& archive() { return m_archive; }
361 [[nodiscard]] const IZipArchive& archive() const { return m_archive; }
362
367 [[nodiscard]] XLAppProperties& appProperties() { return m_appProperties; }
368 [[nodiscard]] const XLAppProperties& appProperties() const { return m_appProperties; }
369
374 [[nodiscard]] XLProperties& coreProperties() { return m_coreProperties; }
375 [[nodiscard]] const XLProperties& coreProperties() const { return m_coreProperties; }
376
380 bool validateSheetName(std::string_view sheetName, bool throwOnInvalid = false);
381
382 bool execCommand(const XLCommand& command);
383
388 [[nodiscard]] XLQuery execQuery(const XLQuery& query) const;
389 XLQuery execQuery(const XLQuery& query);
390
391 void setSavingDeclaration(XLXmlSavingDeclaration const& savingDeclaration);
392
397 [[nodiscard]] const XLSharedStrings& sharedStrings() const { return m_sharedStrings; }
398
402 bool hasPersons() const;
403
407 XLPersons& persons();
408
413 void cleanupSharedStrings();
414
418 void setFormulaNeedsRecalculation(bool status = true) { m_formulaNeedsRecalculation = status; }
419
420 public:
425 [[nodiscard]] std::string extractXmlFromArchive(std::string_view path);
426
427 protected:
432 [[nodiscard]] XLXmlData* getXmlData(std::string_view path, bool doNotThrow = false);
433 [[nodiscard]] const XLXmlData* getXmlData(std::string_view path, bool doNotThrow = false) const;
434 [[nodiscard]] bool hasXmlData(std::string_view path) const;
435
436 public:
440 std::shared_mutex& mutex() const { return *m_docMutex; }
441
442 private:
443 bool m_suppressWarnings{true};
444 std::string m_filePath{};
445 std::string m_defaultAuthor{"System Admin"};
446
447 XLXmlSavingDeclaration m_xmlSavingDeclaration;
448
449 mutable std::list<XLXmlData> m_data{};
450 mutable XLSharedStringsState m_sharedStringsState{};
451 mutable std::unique_ptr<std::shared_mutex> m_docMutex{std::make_unique<std::shared_mutex>()};
452 mutable XLSharedStrings m_sharedStrings{};
453 mutable std::map<void*, std::unordered_map<uint32_t, SharedFormula>> m_sharedFormulas{};
454 std::map<std::string, std::string> m_unhandledEntries{};
455
456 bool m_formulaNeedsRecalculation{false};
457 bool m_isEncryptedSession{false};
458 std::string m_encryptionPassword{""};
459 std::string m_tempDecryptedPath{""};
460
461 XLRelationships m_docRelationships{};
462 XLRelationships m_wbkRelationships{};
463 XLContentTypes m_contentTypes{};
464 XLAppProperties m_appProperties{};
465 XLProperties m_coreProperties{};
466 XLCustomProperties m_customProperties{};
467 XLStyles m_styles{};
468 XLWorkbook m_workbook{};
469 XLPersons m_persons{};
470 IZipArchive m_archive{};
471 };
472
473 [[nodiscard]] OPENXLSX_EXPORT std::string BinaryAsHexString(gsl::span<const std::byte> data);
474 [[nodiscard]] OPENXLSX_EXPORT uint16_t ExcelPasswordHash(std::string_view password);
475 [[nodiscard]] OPENXLSX_EXPORT std::string ExcelPasswordHashAsString(std::string_view password);
476
477 [[nodiscard]] std::string getPathARelativeToPathB(std::string_view pathA, std::string_view pathB);
478 [[nodiscard]] std::string eliminateDotAndDotDotFromPath(std::string_view path);
479
480} // namespace OpenXLSX
481
482#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
483# pragma warning(pop)
484#endif // _MSC_VER
485
486#endif // OPENXLSX_XLDOCUMENT_HPP
return XLRelationships(xmlData, relsFilename)
This class functions as a wrapper around any class that provides the necessary functionality for a zi...
Definition IZipArchive.hpp:26
The XLAppProperties class represents the extended/application properties (docProps/app....
Definition XLProperties.hpp:146
Definition XLChart.hpp:223
Definition XLCommandQuery.hpp:43
The XLComments class is the base class for worksheet comments.
Definition XLComments.hpp:88
The purpose of this class is to load, store add and save item in the [Content_Types]....
Definition XLContentTypes.hpp:102
The XLCustomProperties class represents custom user-defined properties (docProps/custom....
Definition XLProperties.hpp:293
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:82
std::map< void *, std::unordered_map< uint32_t, SharedFormula > > & sharedFormulas(XLInternalAccess) const
Definition XLDocument.hpp:108
const XLSharedStrings & sharedStrings() const
Access the workbook's central Shared String Table (SST). The SST reduces file size by deduplicating t...
Definition XLDocument.hpp:397
const XLXmlData * getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow=false) const
Definition XLDocument.hpp:100
const XLProperties & coreProperties() const
Definition XLDocument.hpp:375
XLXmlData * getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow=false)
Definition XLDocument.hpp:96
const XLAppProperties & appProperties() const
Definition XLDocument.hpp:368
std::shared_mutex & mutex() const
Get the document-level mutex for thread-safe operations.
Definition XLDocument.hpp:440
XLDocument & operator=(XLDocument &&other) noexcept=default
const IZipArchive & archive() const
Definition XLDocument.hpp:361
XLProperties & coreProperties()
Access core document metadata properties. [[nodiscard]] is used to prevent state-querying errors.
Definition XLDocument.hpp:374
XLAppProperties & appProperties()
Access application-specific properties (metadata). [[nodiscard]] is used to prevent state-querying er...
Definition XLDocument.hpp:367
XLXmlData * addXmlData(XLInternalAccess, const std::string &path, const std::string &id, XLContentType type)
Definition XLDocument.hpp:104
XLDocument(const XLDocument &other)=delete
XLDocument & operator=(const XLDocument &other)=delete
void setFormulaNeedsRecalculation(bool status=true)
Marks the document to require formula recalculation on load.
Definition XLDocument.hpp:418
IZipArchive & archive()
Access the low-level ZIP archive for advanced package manipulation. [[nodiscard]] is used to prevent ...
Definition XLDocument.hpp:360
XLDocument(XLDocument &&other) noexcept=default
The XLDrawing class is the base class for worksheet drawings (images, charts, etc....
Definition XLDrawing.hpp:383
Passkey Idiom for internal access control. Only the specified friend classes can instantiate this pas...
Definition XLInternalAccess.hpp:24
A class encapsulating modern Excel persons metadata (persons.xml)
Definition XLThreadedComments.hpp:119
Definition XLPivotTable.hpp:173
Definition XLPivotTable.hpp:198
Definition XLPivotTable.hpp:126
The XLProperties class represents the core properties of the document (docProps/core....
Definition XLProperties.hpp:26
Definition XLCommandQuery.hpp:106
Definition XLRelationships.hpp:140
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:67
An encapsulation of the styles file (xl/styles.xml) in an Excel document package.
Definition XLStyles.hpp:2261
The XLTableCollection class manages multiple tables within a worksheet.
Definition XLTables.hpp:40
A class encapsulating modern Excel threaded comments (ThreadedComments.xml)
Definition XLThreadedComments.hpp:56
The XLVmlDrawing class is the base class for worksheet comments.
Definition XLDrawing.hpp:508
This class encapsulates the concept of a Workbook. It provides access to the individual sheets (works...
Definition XLWorkbook.hpp:87
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:29
The XLXmlSavingDeclaration class encapsulates the properties of an XML saving declaration,...
Definition XLXmlSavingDeclaration.hpp:19
Definition XLZipArchive.hpp:22
Definition IZipArchive.hpp:18
std::string getPathARelativeToPathB(std::string_view pathA, std::string_view pathB)
Definition XLUtilities.cpp:105
OPENXLSX_EXPORT uint16_t ExcelPasswordHash(std::string_view password)
Definition XLUtilities.cpp:39
XLContentType
Definition XLContentTypes.hpp:28
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:45
std::string eliminateDotAndDotDotFromPath(std::string_view path)
Definition XLUtilities.cpp:126
XLChartType
Definition XLChart.hpp:14
XLProperty
The XLDocumentProperties class is an enumeration of the possible properties (metadata) that can be se...
Definition XLDocument.hpp:52
constexpr const bool XLDoNotOverwrite
Definition XLDocument.hpp:46
constexpr const unsigned int pugi_parse_settings
Definition XLDocument.hpp:43
OPENXLSX_EXPORT std::string ExcelPasswordHashAsString(std::string_view password)
Definition XLUtilities.cpp:56
OPENXLSX_EXPORT std::string BinaryAsHexString(gsl::span< const std::byte > data)
Definition XLUtilities.cpp:24
Definition XLCellIterator.hpp:121
Definition XLDocument.hpp:85
uint16_t baseCol
Definition XLDocument.hpp:88
std::string formula
Definition XLDocument.hpp:86
uint32_t baseRow
Definition XLDocument.hpp:87