diff --git a/examples/hello/main.cpp b/examples/hello/main.cpp index f167a72..e621b70 100755 --- a/examples/hello/main.cpp +++ b/examples/hello/main.cpp @@ -2,15 +2,18 @@ #include "xlsxworkbook.h" #include "xlsxworksheet.h" -int main(int argc, char* argv[]) -{ #ifdef Q_OS_MAC - QXlsx::Workbook workbook("../../../Test.xlsx"); +# define DATA_PATH "../../../" #else - QXlsx::Workbook workbook("Test.xlsx"); +# define DATA_PATH "./" #endif + +int main() +{ + QXlsx::Workbook workbook; QXlsx::Worksheet *sheet = workbook.addWorksheet(); sheet->write("A1", "Hello Qt!"); - workbook.close(); + workbook.save(DATA_PATH"Test.xlsx"); + workbook.save(DATA_PATH"Test.zip"); return 0; } diff --git a/src/xlsxpackage.cpp b/src/xlsxpackage.cpp index 6998c22..ca31508 100644 --- a/src/xlsxpackage.cpp +++ b/src/xlsxpackage.cpp @@ -87,8 +87,7 @@ Package::Package(Workbook *workbook) : bool Package::createPackage(const QString &packageName) { - QString fileName = packageName.isEmpty() ? m_workbook->fileName() : packageName; - ZipWriter zipWriter(fileName); + ZipWriter zipWriter(packageName); writeWorksheetFiles(zipWriter); // writeChartsheetFiles(zipWriter); diff --git a/src/xlsxpackage_p.h b/src/xlsxpackage_p.h index 75e86f5..e7685e8 100644 --- a/src/xlsxpackage_p.h +++ b/src/xlsxpackage_p.h @@ -37,7 +37,7 @@ class Package public: Package(Workbook *workbook); - bool createPackage(const QString &packageName=QString()); + bool createPackage(const QString &packageName); private: void writeWorksheetFiles(ZipWriter &zipWriter); diff --git a/src/xlsxworkbook.cpp b/src/xlsxworkbook.cpp index 88fec55..753efe5 100755 --- a/src/xlsxworkbook.cpp +++ b/src/xlsxworkbook.cpp @@ -32,8 +32,8 @@ namespace QXlsx { -Workbook::Workbook(const QString &name, QObject *parent) : - QObject(parent), m_fileName(name) +Workbook::Workbook(QObject *parent) : + QObject(parent) { m_sharedStrings = new SharedStrings(this); m_styles = new Styles(this); @@ -49,24 +49,13 @@ Workbook::Workbook(const QString &name, QObject *parent) : m_activesheet = 0; m_firstsheet = 0; m_table_count = 0; - m_closed = false; } Workbook::~Workbook() { - close(); } -void Workbook::close() -{ - if (m_closed) - return; - - m_closed = true; - saveWorkbook(); -} - -void Workbook::saveWorkbook() +void Workbook::save(const QString &name) { //Add a default worksheet if non have been added. if (m_worksheets.size() == 0) @@ -84,15 +73,9 @@ void Workbook::saveWorkbook() sheet->setActived(true); } - //Create the package based on current workbook Package package(this); - package.createPackage(); -} - -QString Workbook::fileName() const -{ - return m_fileName; + package.createPackage(name); } /*! diff --git a/src/xlsxworkbook.h b/src/xlsxworkbook.h index eb89351..51bfb35 100755 --- a/src/xlsxworkbook.h +++ b/src/xlsxworkbook.h @@ -41,7 +41,7 @@ class Workbook : public QObject { Q_OBJECT public: - Workbook(const QString &name, QObject *parent=0); + Workbook(QObject *parent=0); ~Workbook(); Worksheet *addWorksheet(const QString &name = QString()); @@ -51,7 +51,7 @@ public: void setOptimizationEnabled(bool enable=true); void setStringsToNumbersEnabled(bool enable=true); - void close(); + void save(const QString &name); private: friend class Package; @@ -60,14 +60,9 @@ private: QList worksheets() const; SharedStrings *sharedStrings(); Styles *styles(); - QString fileName() const; bool isOptimizationEnabled() const; bool isStringsToNumbersEnabled() const; void saveToXmlFile(QIODevice *device); - void saveWorkbook(); - - QString m_fileName; - bool m_closed; SharedStrings *m_sharedStrings; QList m_worksheets;