Browse Source

Use save() function to save the package instead of close

master
Debao Zhang 11 years ago
parent
commit
98611399fd
  1. 13
      examples/hello/main.cpp
  2. 3
      src/xlsxpackage.cpp
  3. 2
      src/xlsxpackage_p.h
  4. 25
      src/xlsxworkbook.cpp
  5. 9
      src/xlsxworkbook.h

13
examples/hello/main.cpp

@ -2,15 +2,18 @@
#include "xlsxworkbook.h" #include "xlsxworkbook.h"
#include "xlsxworksheet.h" #include "xlsxworksheet.h"
int main(int argc, char* argv[])
{
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QXlsx::Workbook workbook("../../../Test.xlsx"); # define DATA_PATH "../../../"
#else #else
QXlsx::Workbook workbook("Test.xlsx"); # define DATA_PATH "./"
#endif #endif
int main()
{
QXlsx::Workbook workbook;
QXlsx::Worksheet *sheet = workbook.addWorksheet(); QXlsx::Worksheet *sheet = workbook.addWorksheet();
sheet->write("A1", "Hello Qt!"); sheet->write("A1", "Hello Qt!");
workbook.close(); workbook.save(DATA_PATH"Test.xlsx");
workbook.save(DATA_PATH"Test.zip");
return 0; return 0;
} }

3
src/xlsxpackage.cpp

@ -87,8 +87,7 @@ Package::Package(Workbook *workbook) :
bool Package::createPackage(const QString &packageName) bool Package::createPackage(const QString &packageName)
{ {
QString fileName = packageName.isEmpty() ? m_workbook->fileName() : packageName; ZipWriter zipWriter(packageName);
ZipWriter zipWriter(fileName);
writeWorksheetFiles(zipWriter); writeWorksheetFiles(zipWriter);
// writeChartsheetFiles(zipWriter); // writeChartsheetFiles(zipWriter);

2
src/xlsxpackage_p.h

@ -37,7 +37,7 @@ class Package
public: public:
Package(Workbook *workbook); Package(Workbook *workbook);
bool createPackage(const QString &packageName=QString()); bool createPackage(const QString &packageName);
private: private:
void writeWorksheetFiles(ZipWriter &zipWriter); void writeWorksheetFiles(ZipWriter &zipWriter);

25
src/xlsxworkbook.cpp

@ -32,8 +32,8 @@
namespace QXlsx { namespace QXlsx {
Workbook::Workbook(const QString &name, QObject *parent) : Workbook::Workbook(QObject *parent) :
QObject(parent), m_fileName(name) QObject(parent)
{ {
m_sharedStrings = new SharedStrings(this); m_sharedStrings = new SharedStrings(this);
m_styles = new Styles(this); m_styles = new Styles(this);
@ -49,24 +49,13 @@ Workbook::Workbook(const QString &name, QObject *parent) :
m_activesheet = 0; m_activesheet = 0;
m_firstsheet = 0; m_firstsheet = 0;
m_table_count = 0; m_table_count = 0;
m_closed = false;
} }
Workbook::~Workbook() Workbook::~Workbook()
{ {
close();
} }
void Workbook::close() void Workbook::save(const QString &name)
{
if (m_closed)
return;
m_closed = true;
saveWorkbook();
}
void Workbook::saveWorkbook()
{ {
//Add a default worksheet if non have been added. //Add a default worksheet if non have been added.
if (m_worksheets.size() == 0) if (m_worksheets.size() == 0)
@ -84,15 +73,9 @@ void Workbook::saveWorkbook()
sheet->setActived(true); sheet->setActived(true);
} }
//Create the package based on current workbook //Create the package based on current workbook
Package package(this); Package package(this);
package.createPackage(); package.createPackage(name);
}
QString Workbook::fileName() const
{
return m_fileName;
} }
/*! /*!

9
src/xlsxworkbook.h

@ -41,7 +41,7 @@ class Workbook : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Workbook(const QString &name, QObject *parent=0); Workbook(QObject *parent=0);
~Workbook(); ~Workbook();
Worksheet *addWorksheet(const QString &name = QString()); Worksheet *addWorksheet(const QString &name = QString());
@ -51,7 +51,7 @@ public:
void setOptimizationEnabled(bool enable=true); void setOptimizationEnabled(bool enable=true);
void setStringsToNumbersEnabled(bool enable=true); void setStringsToNumbersEnabled(bool enable=true);
void close(); void save(const QString &name);
private: private:
friend class Package; friend class Package;
@ -60,14 +60,9 @@ private:
QList<Worksheet *> worksheets() const; QList<Worksheet *> worksheets() const;
SharedStrings *sharedStrings(); SharedStrings *sharedStrings();
Styles *styles(); Styles *styles();
QString fileName() const;
bool isOptimizationEnabled() const; bool isOptimizationEnabled() const;
bool isStringsToNumbersEnabled() const; bool isStringsToNumbersEnabled() const;
void saveToXmlFile(QIODevice *device); void saveToXmlFile(QIODevice *device);
void saveWorkbook();
QString m_fileName;
bool m_closed;
SharedStrings *m_sharedStrings; SharedStrings *m_sharedStrings;
QList<Worksheet *> m_worksheets; QList<Worksheet *> m_worksheets;

Loading…
Cancel
Save