ExcelWriter =========== .. cpp:class:: pandas::ExcelWriter pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use ExcelWriter ExcelWriter obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``ExcelWriter(const ExcelWriter&) = delete`` - pd_excel.h:86 - * - ``ExcelWriter(ExcelWriter&& other) noexcept`` - pd_excel.h:90 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``SheetData& get_or_create_sheet(const std::string& sheet_name)`` - SheetData& - pd_excel.h:121 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_open() const`` - bool - pd_excel.h:105 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static bool check_extension(const std::string& ext)`` - static bool - pd_excel.h:104 - :ref:`View ` * - ``void close()`` - void - pd_excel.h:103 - :ref:`View ` * - ``static std::string col_to_letter(size_t col)`` - static std::string - pd_excel.h:142 - * - ``static uint32_t crc32(const std::vector& data)`` - static uint32_t - pd_excel.h:143 - * - ``std::string date_format() const`` - std::string - pd_excel.h:96 - :ref:`View ` * - ``std::string datetime_format() const`` - std::string - pd_excel.h:97 - :ref:`View ` * - ``std::string engine() const`` - std::string - pd_excel.h:95 - :ref:`View ` * - ``static std::string escape_xml(const std::string& str)`` - static std::string - pd_excel.h:141 - * - ``bool has_sheet(const std::string& sheet_name) const`` - bool - pd_excel.h:124 - * - ``std::string if_sheet_exists() const`` - std::string - pd_excel.h:98 - :ref:`View ` * - ``std::string path() const`` - std::string - pd_excel.h:94 - :ref:`View ` * - ``std::vector sheet_names() const`` - std::vector - pd_excel.h:99 - :ref:`View ` * - ``static std::vector supported_extensions()`` - static std::vector - pd_excel.h:100 - :ref:`View ` * - ``void write_xlsx()`` - void - pd_excel.h:140 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-excelwriter-is_open-0: .. dropdown:: is_open (pd_test_2_all.cpp:4092) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4082 :emphasize-lines: 11 } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); } if (writer.if_sheet_exists() != "replace") { std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl; throw std::runtime_error("if_sheet_exists property incorrect"); } if (!writer.is_open()) { std::cout << " [FAIL] : is_open should be true" << std::endl; throw std::runtime_error("is_open should be true"); } writer.close(); if (writer.is_open()) { std::cout << " [FAIL] : is_open should be false after close" << std::endl; throw std::runtime_error("is_open should be false after close"); } .. _example-excelwriter-check_extension-1: .. dropdown:: check_extension (pd_test_2_all.cpp:4112) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4102 :emphasize-lines: 11 } std::remove("temp/test_writer_props.xlsx"); std::cout << " -> tests passed" << std::endl; } void pd_test_excel_writer_check_extension() { std::cout << "========= ExcelWriter check_extension =============="; if (!pandas::ExcelWriter::check_extension(".xlsx")) { std::cout << " [FAIL] : .xlsx should be supported" << std::endl; throw std::runtime_error(".xlsx should be supported"); } if (!pandas::ExcelWriter::check_extension(".XLSX")) { std::cout << " [FAIL] : .XLSX should be supported (case insensitive)" << std::endl; throw std::runtime_error(".XLSX should be supported"); } if (!pandas::ExcelWriter::check_extension(".xlsm")) { std::cout << " [FAIL] : .xlsm should be supported" << std::endl; throw std::runtime_error(".xlsm should be supported"); .. _example-excelwriter-close-2: .. dropdown:: close (pd_test_2_all.cpp:3486) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3476 :emphasize-lines: 11 // Check ZIP signature (PK..) file.seekg(0, std::ios::beg); char sig[4]; file.read(sig, 4); if (sig[0] != 'P' || sig[1] != 'K' || sig[2] != 0x03 || sig[3] != 0x04) { std::cout << " [FAIL] : in pd_test_excel_basic() : Invalid ZIP signature" << std::endl; throw std::runtime_error("pd_test_excel_basic failed: invalid ZIP signature"); } file.close(); std::cout << " -> tests passed" << std::endl; } // Test Excel export with custom sheet name void pd_test_excel_sheet_name() { std::cout << "========= Excel export with sheet name ============="; std::map> data = { {"X", {1.1, 2.2, 3.3}}, {"Y", {4.4, 5.5, 6.6}} .. _example-excelwriter-date_format-3: .. dropdown:: date_format (pd_test_2_all.cpp:4079) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4069 :emphasize-lines: 11 "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace"); if (writer.path() != "temp/test_writer_props.xlsx") { std::cout << " [FAIL] : path property incorrect" << std::endl; throw std::runtime_error("path property incorrect"); } if (writer.engine() != "xlsxwriter") { std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl; throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); } if (writer.if_sheet_exists() != "replace") { std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl; throw std::runtime_error("if_sheet_exists property incorrect"); .. _example-excelwriter-datetime_format-4: .. dropdown:: datetime_format (pd_test_2_all.cpp:4083) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4073 :emphasize-lines: 11 throw std::runtime_error("path property incorrect"); } if (writer.engine() != "xlsxwriter") { std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl; throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); } if (writer.if_sheet_exists() != "replace") { std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl; throw std::runtime_error("if_sheet_exists property incorrect"); } if (!writer.is_open()) { std::cout << " [FAIL] : is_open should be true" << std::endl; .. _example-excelwriter-engine-5: .. dropdown:: engine (pd_test_2_all.cpp:4075) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4065 :emphasize-lines: 11 void pd_test_excel_writer_properties() { std::cout << "========= ExcelWriter properties ==================="; pandas::ExcelWriter writer("temp/test_writer_props.xlsx", "xlsxwriter", "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace"); if (writer.path() != "temp/test_writer_props.xlsx") { std::cout << " [FAIL] : path property incorrect" << std::endl; throw std::runtime_error("path property incorrect"); } if (writer.engine() != "xlsxwriter") { std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl; throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); .. _example-excelwriter-if_sheet_exists-6: .. dropdown:: if_sheet_exists (pd_test_2_all.cpp:4087) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4077 :emphasize-lines: 11 throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); } if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") { std::cout << " [FAIL] : datetime_format property incorrect" << std::endl; throw std::runtime_error("datetime_format property incorrect"); } if (writer.if_sheet_exists() != "replace") { std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl; throw std::runtime_error("if_sheet_exists property incorrect"); } if (!writer.is_open()) { std::cout << " [FAIL] : is_open should be true" << std::endl; throw std::runtime_error("is_open should be true"); } writer.close(); .. _example-excelwriter-path-7: .. dropdown:: path (pd_test_2_all.cpp:4071) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4061 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_excel_writer_properties() { std::cout << "========= ExcelWriter properties ==================="; pandas::ExcelWriter writer("temp/test_writer_props.xlsx", "xlsxwriter", "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace"); if (writer.path() != "temp/test_writer_props.xlsx") { std::cout << " [FAIL] : path property incorrect" << std::endl; throw std::runtime_error("path property incorrect"); } if (writer.engine() != "xlsxwriter") { std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl; throw std::runtime_error("engine property incorrect"); } if (writer.date_format() != "YYYY/MM/DD") { std::cout << " [FAIL] : date_format property incorrect" << std::endl; throw std::runtime_error("date_format property incorrect"); .. _example-excelwriter-sheet_names-8: .. dropdown:: sheet_names (pd_test_2_all.cpp:4049) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4039 :emphasize-lines: 11 pandas::ExcelWriter writer("temp/test_writer_names.xlsx"); std::map> data = {{"A", {1}}}; pandas::DataFrame df(data); df.to_excel(writer, "First"); df.to_excel(writer, "Second"); df.to_excel(writer, "Third"); auto names = writer.sheet_names(); if (names.size() != 3) { std::cout << " [FAIL] : sheet_names: expected 3, got " << names.size() << std::endl; throw std::runtime_error("sheet_names: wrong count"); } if (names[0] != "First" || names[1] != "Second" || names[2] != "Third") { std::cout << " [FAIL] : sheet_names: wrong order or names" << std::endl; throw std::runtime_error("sheet_names: wrong order"); } writer.close(); .. _example-excelwriter-supported_extensions-9: .. dropdown:: supported_extensions (pd_test_2_all.cpp:4139) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4129 :emphasize-lines: 11 std::cout << " [FAIL] : .xls should not be supported" << std::endl; throw std::runtime_error(".xls should not be supported"); } std::cout << " -> tests passed" << std::endl; } void pd_test_excel_writer_supported_extensions() { std::cout << "========= ExcelWriter supported_extensions ========="; auto exts = pandas::ExcelWriter::supported_extensions(); if (exts.size() < 2) { std::cout << " [FAIL] : expected at least 2 extensions" << std::endl; throw std::runtime_error("expected at least 2 extensions"); } bool found_xlsx = false; for (const auto& ext : exts) { if (ext == ".xlsx") { found_xlsx = true; break; .. _example-excelwriter-write_xlsx-10: .. dropdown:: write_xlsx (pd_test_5_all.cpp:83986) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 83976 :emphasize-lines: 11 put_u16(out, 0); // start disk put_u16(out, static_cast(entries.size())); // disk entries put_u16(out, static_cast(entries.size())); // total entries put_u32(out, cd_size); put_u32(out, cd_start); put_u16(out, 0); // comment len return out; } static std::string write_xlsx(const std::string& path, const std::vector& sheets) { // Collect shared strings (dedup). std::vector shared; auto sidx = [&](const std::string& s) -> size_t { for (size_t i = 0; i < shared.size(); ++i) if (shared[i] == s) return i; shared.push_back(s); return shared.size() - 1; };