ExcelWriter#
-
class pandas::ExcelWriter#
pandas C++ class.
Example#
#include <pandas/pandas.h>
using namespace pandas;
// Use ExcelWriter
ExcelWriter obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
pd_excel.h:86 |
|
|
pd_excel.h:90 |
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
SheetData& |
pd_excel.h:121 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
pd_excel.h:105 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
static bool |
pd_excel.h:104 |
|
|
void |
pd_excel.h:103 |
|
|
static std::string |
pd_excel.h:142 |
|
|
static uint32_t |
pd_excel.h:143 |
|
|
std::string |
pd_excel.h:96 |
|
|
std::string |
pd_excel.h:97 |
|
|
std::string |
pd_excel.h:95 |
|
|
static std::string |
pd_excel.h:141 |
|
|
bool |
pd_excel.h:124 |
|
|
std::string |
pd_excel.h:98 |
|
|
std::string |
pd_excel.h:94 |
|
|
std::vector<std::string> |
pd_excel.h:99 |
|
|
static std::vector<std::string> |
pd_excel.h:100 |
|
|
void |
pd_excel.h:140 |
Code Examples#
The following examples are extracted from the test suite.
is_open (pd_test_2_all.cpp:4092)
4082 }
4083 if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") {
4084 std::cout << " [FAIL] : datetime_format property incorrect" << std::endl;
4085 throw std::runtime_error("datetime_format property incorrect");
4086 }
4087 if (writer.if_sheet_exists() != "replace") {
4088 std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl;
4089 throw std::runtime_error("if_sheet_exists property incorrect");
4090 }
4091
4092 if (!writer.is_open()) {
4093 std::cout << " [FAIL] : is_open should be true" << std::endl;
4094 throw std::runtime_error("is_open should be true");
4095 }
4096
4097 writer.close();
4098
4099 if (writer.is_open()) {
4100 std::cout << " [FAIL] : is_open should be false after close" << std::endl;
4101 throw std::runtime_error("is_open should be false after close");
4102 }
check_extension (pd_test_2_all.cpp:4112)
4102 }
4103
4104 std::remove("temp/test_writer_props.xlsx");
4105
4106 std::cout << " -> tests passed" << std::endl;
4107 }
4108
4109 void pd_test_excel_writer_check_extension() {
4110 std::cout << "========= ExcelWriter check_extension ==============";
4111
4112 if (!pandas::ExcelWriter::check_extension(".xlsx")) {
4113 std::cout << " [FAIL] : .xlsx should be supported" << std::endl;
4114 throw std::runtime_error(".xlsx should be supported");
4115 }
4116 if (!pandas::ExcelWriter::check_extension(".XLSX")) {
4117 std::cout << " [FAIL] : .XLSX should be supported (case insensitive)" << std::endl;
4118 throw std::runtime_error(".XLSX should be supported");
4119 }
4120 if (!pandas::ExcelWriter::check_extension(".xlsm")) {
4121 std::cout << " [FAIL] : .xlsm should be supported" << std::endl;
4122 throw std::runtime_error(".xlsm should be supported");
close (pd_test_2_all.cpp:3486)
3476 // Check ZIP signature (PK..)
3477 file.seekg(0, std::ios::beg);
3478 char sig[4];
3479 file.read(sig, 4);
3480 if (sig[0] != 'P' || sig[1] != 'K' || sig[2] != 0x03 || sig[3] != 0x04) {
3481 std::cout << " [FAIL] : in pd_test_excel_basic() : Invalid ZIP signature" << std::endl;
3482 throw std::runtime_error("pd_test_excel_basic failed: invalid ZIP signature");
3483 }
3484
3485 file.close();
3486 std::cout << " -> tests passed" << std::endl;
3487 }
3488
3489 // Test Excel export with custom sheet name
3490 void pd_test_excel_sheet_name() {
3491 std::cout << "========= Excel export with sheet name =============";
3492
3493 std::map<std::string, std::vector<double>> data = {
3494 {"X", {1.1, 2.2, 3.3}},
3495 {"Y", {4.4, 5.5, 6.6}}
date_format (pd_test_2_all.cpp:4079)
4069 "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace");
4070
4071 if (writer.path() != "temp/test_writer_props.xlsx") {
4072 std::cout << " [FAIL] : path property incorrect" << std::endl;
4073 throw std::runtime_error("path property incorrect");
4074 }
4075 if (writer.engine() != "xlsxwriter") {
4076 std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl;
4077 throw std::runtime_error("engine property incorrect");
4078 }
4079 if (writer.date_format() != "YYYY/MM/DD") {
4080 std::cout << " [FAIL] : date_format property incorrect" << std::endl;
4081 throw std::runtime_error("date_format property incorrect");
4082 }
4083 if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") {
4084 std::cout << " [FAIL] : datetime_format property incorrect" << std::endl;
4085 throw std::runtime_error("datetime_format property incorrect");
4086 }
4087 if (writer.if_sheet_exists() != "replace") {
4088 std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl;
4089 throw std::runtime_error("if_sheet_exists property incorrect");
datetime_format (pd_test_2_all.cpp:4083)
4073 throw std::runtime_error("path property incorrect");
4074 }
4075 if (writer.engine() != "xlsxwriter") {
4076 std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl;
4077 throw std::runtime_error("engine property incorrect");
4078 }
4079 if (writer.date_format() != "YYYY/MM/DD") {
4080 std::cout << " [FAIL] : date_format property incorrect" << std::endl;
4081 throw std::runtime_error("date_format property incorrect");
4082 }
4083 if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") {
4084 std::cout << " [FAIL] : datetime_format property incorrect" << std::endl;
4085 throw std::runtime_error("datetime_format property incorrect");
4086 }
4087 if (writer.if_sheet_exists() != "replace") {
4088 std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl;
4089 throw std::runtime_error("if_sheet_exists property incorrect");
4090 }
4091
4092 if (!writer.is_open()) {
4093 std::cout << " [FAIL] : is_open should be true" << std::endl;
engine (pd_test_2_all.cpp:4075)
4065 void pd_test_excel_writer_properties() {
4066 std::cout << "========= ExcelWriter properties ===================";
4067
4068 pandas::ExcelWriter writer("temp/test_writer_props.xlsx", "xlsxwriter",
4069 "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace");
4070
4071 if (writer.path() != "temp/test_writer_props.xlsx") {
4072 std::cout << " [FAIL] : path property incorrect" << std::endl;
4073 throw std::runtime_error("path property incorrect");
4074 }
4075 if (writer.engine() != "xlsxwriter") {
4076 std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl;
4077 throw std::runtime_error("engine property incorrect");
4078 }
4079 if (writer.date_format() != "YYYY/MM/DD") {
4080 std::cout << " [FAIL] : date_format property incorrect" << std::endl;
4081 throw std::runtime_error("date_format property incorrect");
4082 }
4083 if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") {
4084 std::cout << " [FAIL] : datetime_format property incorrect" << std::endl;
4085 throw std::runtime_error("datetime_format property incorrect");
if_sheet_exists (pd_test_2_all.cpp:4087)
4077 throw std::runtime_error("engine property incorrect");
4078 }
4079 if (writer.date_format() != "YYYY/MM/DD") {
4080 std::cout << " [FAIL] : date_format property incorrect" << std::endl;
4081 throw std::runtime_error("date_format property incorrect");
4082 }
4083 if (writer.datetime_format() != "YYYY-MM-DD HH:MM:SS.fff") {
4084 std::cout << " [FAIL] : datetime_format property incorrect" << std::endl;
4085 throw std::runtime_error("datetime_format property incorrect");
4086 }
4087 if (writer.if_sheet_exists() != "replace") {
4088 std::cout << " [FAIL] : if_sheet_exists property incorrect" << std::endl;
4089 throw std::runtime_error("if_sheet_exists property incorrect");
4090 }
4091
4092 if (!writer.is_open()) {
4093 std::cout << " [FAIL] : is_open should be true" << std::endl;
4094 throw std::runtime_error("is_open should be true");
4095 }
4096
4097 writer.close();
path (pd_test_2_all.cpp:4071)
4061 std::cout << " -> tests passed" << std::endl;
4062 }
4063
4064 void pd_test_excel_writer_properties() {
4065 std::cout << "========= ExcelWriter properties ===================";
4066
4067 pandas::ExcelWriter writer("temp/test_writer_props.xlsx", "xlsxwriter",
4068 "YYYY/MM/DD", "YYYY-MM-DD HH:MM:SS.fff", "w", std::nullopt, "replace");
4069
4070 if (writer.path() != "temp/test_writer_props.xlsx") {
4071 std::cout << " [FAIL] : path property incorrect" << std::endl;
4072 throw std::runtime_error("path property incorrect");
4073 }
4074 if (writer.engine() != "xlsxwriter") {
4075 std::cout << " [FAIL] : engine property incorrect: " << writer.engine() << std::endl;
4076 throw std::runtime_error("engine property incorrect");
4077 }
4078 if (writer.date_format() != "YYYY/MM/DD") {
4079 std::cout << " [FAIL] : date_format property incorrect" << std::endl;
4080 throw std::runtime_error("date_format property incorrect");
sheet_names (pd_test_2_all.cpp:4049)
4039 pandas::ExcelWriter writer("temp/test_writer_names.xlsx");
4040
4041 std::map<std::string, std::vector<int>> data = {{"A", {1}}};
4042 pandas::DataFrame df(data);
4043
4044 df.to_excel(writer, "First");
4045 df.to_excel(writer, "Second");
4046 df.to_excel(writer, "Third");
4047
4048 auto names = writer.sheet_names();
4049 if (names.size() != 3) {
4050 std::cout << " [FAIL] : sheet_names: expected 3, got " << names.size() << std::endl;
4051 throw std::runtime_error("sheet_names: wrong count");
4052 }
4053 if (names[0] != "First" || names[1] != "Second" || names[2] != "Third") {
4054 std::cout << " [FAIL] : sheet_names: wrong order or names" << std::endl;
4055 throw std::runtime_error("sheet_names: wrong order");
4056 }
4057
4058 writer.close();
supported_extensions (pd_test_2_all.cpp:4139)
4129 std::cout << " [FAIL] : .xls should not be supported" << std::endl;
4130 throw std::runtime_error(".xls should not be supported");
4131 }
4132
4133 std::cout << " -> tests passed" << std::endl;
4134 }
4135
4136 void pd_test_excel_writer_supported_extensions() {
4137 std::cout << "========= ExcelWriter supported_extensions =========";
4138
4139 auto exts = pandas::ExcelWriter::supported_extensions();
4140 if (exts.size() < 2) {
4141 std::cout << " [FAIL] : expected at least 2 extensions" << std::endl;
4142 throw std::runtime_error("expected at least 2 extensions");
4143 }
4144
4145 bool found_xlsx = false;
4146 for (const auto& ext : exts) {
4147 if (ext == ".xlsx") {
4148 found_xlsx = true;
4149 break;
write_xlsx (pd_test_5_all.cpp:83986)
83976 put_u16(out, 0); // start disk
83977 put_u16(out, static_cast<uint16_t>(entries.size())); // disk entries
83978 put_u16(out, static_cast<uint16_t>(entries.size())); // total entries
83979 put_u32(out, cd_size);
83980 put_u32(out, cd_start);
83981 put_u16(out, 0); // comment len
83982
83983 return out;
83984}
83985
83986static std::string write_xlsx(const std::string& path,
83987 const std::vector<Sheet>& sheets) {
83988 // Collect shared strings (dedup).
83989 std::vector<std::string> shared;
83990 auto sidx = [&](const std::string& s) -> size_t {
83991 for (size_t i = 0; i < shared.size(); ++i)
83992 if (shared[i] == s) return i;
83993 shared.push_back(s);
83994 return shared.size() - 1;
83995 };