xlnt/source/detail/excel_serializer.hpp

186 lines
5.3 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
// Copyright (c) 2010-2015 openpyxl
2014-06-06 04:19:31 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
#pragma once
2015-10-30 01:46:56 +08:00
#include <cstdint>
#include <ostream>
2015-10-21 11:30:10 +08:00
#include <string>
#include <vector>
2014-06-06 04:19:31 +08:00
#include <xlnt/xlnt_config.hpp>
#include <xlnt/packaging/zip_file.hpp>
2016-06-11 01:40:50 +08:00
#include <detail/stylesheet.hpp>
2015-10-30 01:46:56 +08:00
2015-10-21 11:30:10 +08:00
namespace xlnt {
2015-10-21 11:30:10 +08:00
class workbook;
2014-06-06 04:19:31 +08:00
2015-10-31 06:54:04 +08:00
/// <summary>
/// Handles reading and writing workbooks from an actual XLSX archive
/// using other serializers.
/// </summary>
class XLNT_CLASS excel_serializer
2015-10-24 02:42:36 +08:00
{
2016-01-18 14:23:31 +08:00
public:
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
static const std::string central_directory_signature();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
static std::string repair_central_directory(const std::string &original);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Construct an excel_serializer which operates on wb.
/// </summary>
2015-10-30 01:46:56 +08:00
excel_serializer(workbook &wb);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, load archive from filename, then populate workbook
/// with data from archive.
/// </summary>
bool load_workbook(const std::string &filename, bool guess_types = false, bool data_only = false);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, load archive from stream, then populate workbook
/// with data from archive.
/// </summary>
2015-10-30 01:46:56 +08:00
bool load_stream_workbook(std::istream &stream, bool guess_types = false, bool data_only = false);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, load archive from bytes, then populate workbook
/// with data from archive.
/// </summary>
2016-01-18 14:23:31 +08:00
bool load_virtual_workbook(
const std::vector<std::uint8_t> &bytes, bool guess_types = false, bool data_only = false);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then save archive
/// to filename.
/// </summary>
bool save_workbook(const std::string &filename, bool as_template = false);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then assign ZIP file
/// binary data to bytes.
/// </summary>
2015-10-30 07:37:07 +08:00
bool save_virtual_workbook(std::vector<std::uint8_t> &bytes, bool as_template = false);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Create a ZIP file in memory, save workbook to this archive, then copy ZIP file
/// binary data to stream.
/// </summary>
2015-10-30 07:37:07 +08:00
bool save_stream_workbook(std::ostream &stream, bool as_template = false);
2016-06-11 01:40:50 +08:00
xlnt::detail::stylesheet &get_stylesheet();
2016-01-18 14:23:31 +08:00
private:
2015-10-31 06:54:04 +08:00
/// <summary>
/// Reads all files in archive and populates workbook with associated data
/// using other appropriate serializers such as workbook_serializer.
/// </summary>
2015-10-30 01:46:56 +08:00
void read_data(bool guess_types, bool data_only);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Read xl/sharedStrings.xml from internal archive and add shared strings to workbook.
/// </summary>
2015-10-30 01:46:56 +08:00
void read_shared_strings();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void read_images();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void read_charts();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void read_chartsheets();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void read_worksheets();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void read_external_links();
2015-10-31 06:54:04 +08:00
/// <summary>
/// Write all files needed to create a valid XLSX file which represents all
/// data contained in workbook.
/// </summary>
2015-10-30 01:46:56 +08:00
void write_data(bool as_template);
2015-10-31 06:54:04 +08:00
/// <summary>
/// Write xl/sharedStrings.xml to internal archive based on shared strings in workbook.
/// </summary>
2015-10-30 01:46:56 +08:00
void write_shared_strings();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void write_images();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void write_charts();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void write_chartsheets();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void write_worksheets();
2015-10-31 06:54:04 +08:00
/// <summary>
///
/// </summary>
2015-10-30 01:46:56 +08:00
void write_external_links();
2015-10-24 02:42:36 +08:00
2015-10-31 06:54:04 +08:00
/// <summary>
/// A reference to the workbook which is the object of read/write operations.
/// </summary>
workbook &workbook_;
/// <summary>
/// The internal archive which holds files representing workbook_ during
/// read/write operations.
/// </summary>
2015-10-30 01:46:56 +08:00
zip_file archive_;
2015-10-24 02:42:36 +08:00
};
2014-06-06 04:19:31 +08:00
} // namespace xlnt