Browse Source

Add basic QDateTime support

master
Debao Zhang 11 years ago
parent
commit
1e4f34942b
  1. 4
      examples/style/main.cpp
  2. 27
      src/xlsxworksheet.cpp
  3. 2
      src/xlsxworksheet.h

4
examples/style/main.cpp

@ -45,6 +45,10 @@ int main()
sheet->write(row, col, row+col); sheet->write(row, col, row+col);
sheet->setColumn(8, 15, 5.0, format4); sheet->setColumn(8, 15, 5.0, format4);
QXlsx::Format *format5 = workbook.addFormat();
format5->setNumberFormat(22);
sheet->write("A5", QDate(2013, 8, 29), format5);
workbook.save(DATA_PATH"TestStyle.xlsx"); workbook.save(DATA_PATH"TestStyle.xlsx");
return 0; return 0;
} }

27
src/xlsxworksheet.cpp

@ -47,7 +47,8 @@ struct XlsxCellData
Number, Number,
Formula, Formula,
ArrayFormula, ArrayFormula,
Boolean Boolean,
DateTime
}; };
XlsxCellData(const QVariant &data=QVariant(), CellDataType type=Blank, Format *format=0) : XlsxCellData(const QVariant &data=QVariant(), CellDataType type=Blank, Format *format=0) :
value(data), dataType(type), format(format) value(data), dataType(type), format(format)
@ -200,7 +201,7 @@ int Worksheet::write(int row, int column, const QVariant &value, Format *format)
} else if (value.type() == QMetaType::Bool) { //Bool } else if (value.type() == QMetaType::Bool) { //Bool
ret = writeBool(row,column, value.toBool(), format); ret = writeBool(row,column, value.toBool(), format);
} else if (value.toDateTime().isValid()) { //DateTime } else if (value.toDateTime().isValid()) { //DateTime
ret = writeDateTime(row, column, value.toDateTime(), format);
} else if (value.toDouble(&ok), ok) { //Number } else if (value.toDouble(&ok), ok) { //Number
if (!m_workbook->isStringsToNumbersEnabled() && value.type() == QMetaType::QString) { if (!m_workbook->isStringsToNumbersEnabled() && value.type() == QMetaType::QString) {
//Don't convert string to number if the flag not enabled. //Don't convert string to number if the flag not enabled.
@ -301,6 +302,15 @@ int Worksheet::writeBool(int row, int column, bool value, Format *format)
return 0; return 0;
} }
int Worksheet::writeDateTime(int row, int column, const QDateTime &dt, Format *format)
{
if (checkDimensions(row, column))
return -1;
m_cellTable[row][column] = new XlsxCellData(dt, XlsxCellData::DateTime, format);
return 0;
}
/* /*
Check that row and col are valid and store the max and min Check that row and col are valid and store the max and min
values for use in other methods/elements. The ignore_row / values for use in other methods/elements. The ignore_row /
@ -499,7 +509,8 @@ void Worksheet::writeCellData(XmlStreamWriter &writer, int row, int col, XlsxCel
writer.writeAttribute("t", "s"); writer.writeAttribute("t", "s");
writer.writeTextElement("v", cell->value.toString()); writer.writeTextElement("v", cell->value.toString());
} else if (cell->dataType == XlsxCellData::Number){ } else if (cell->dataType == XlsxCellData::Number){
writer.writeTextElement("v", cell->value.toString()); double value = cell->value.toDouble();
writer.writeTextElement("v", QString::number(value, 'g', 15));
} else if (cell->dataType == XlsxCellData::Formula) { } else if (cell->dataType == XlsxCellData::Formula) {
bool ok = true; bool ok = true;
cell->formula.toDouble(&ok); cell->formula.toDouble(&ok);
@ -514,6 +525,16 @@ void Worksheet::writeCellData(XmlStreamWriter &writer, int row, int col, XlsxCel
writer.writeTextElement("v", cell->value.toBool() ? "1" : "0"); writer.writeTextElement("v", cell->value.toBool() ? "1" : "0");
} else if (cell->dataType == XlsxCellData::Blank) { } else if (cell->dataType == XlsxCellData::Blank) {
//Ok, empty here. //Ok, empty here.
} else if (cell->dataType == XlsxCellData::DateTime) {
QDateTime epoch(QDate(1899, 12, 31));
if (m_workbook->isDate1904())
epoch = QDateTime(QDate(1904, 1, 1));
qint64 delta = epoch.msecsTo(cell->value.toDateTime());
double excel_time = delta / (1000*60*60*24);
//Account for Excel erroneously treating 1900 as a leap year.
if (!m_workbook->isDate1904() && excel_time > 59)
excel_time += 1;
writer.writeTextElement("v", QString::number(excel_time, 'g', 15));
} }
writer.writeEndElement(); //c writer.writeEndElement(); //c
} }

2
src/xlsxworksheet.h

@ -30,6 +30,7 @@
#include <QMap> #include <QMap>
#include <QVariant> #include <QVariant>
class QIODevice; class QIODevice;
class QDateTime;
namespace QXlsx { namespace QXlsx {
class Package; class Package;
@ -52,6 +53,7 @@ public:
int writeFormula(int row, int column, const QString &formula, Format *format=0, double result=0); int writeFormula(int row, int column, const QString &formula, Format *format=0, double result=0);
int writeBlank(int row, int column, Format *format=0); int writeBlank(int row, int column, Format *format=0);
int writeBool(int row, int column, bool value, Format *format=0); int writeBool(int row, int column, bool value, Format *format=0);
int writeDateTime(int row, int column, const QDateTime& dt, Format *format=0);
bool setRow(int row, double height, Format* format=0, bool hidden=false); bool setRow(int row, double height, Format* format=0, bool hidden=false);
bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false); bool setColumn(int colFirst, int colLast, double width, Format* format=0, bool hidden=false);

Loading…
Cancel
Save