예제 #1
0
bool ExportaTabela::exporta()
{
    if (outputfilename.isEmpty())
        return false;

    QTextDocument *document = new QTextDocument;
    QTextDocumentWriter *writer = new QTextDocumentWriter(outputfilename);
    writer->setFormat("plaintext");
    QTextCodec *codec = QTextCodec::codecForName("ISO 8859-1");
    writer->setCodec(codec);

    QStringList csv;

    // Cabeçalho
    for (int i = 0; i < query->record().count(); ++i)
        csv << QString(query->record().fieldName(i)).append(";");

    // Conteúdo
    while(query->next())
    {
        csv << "\n";
        for (int i = 0; i < query->record().count(); ++i)
            csv << query->value(i).toString().append(";");
    }

    QTextCursor cursor(document);
    for (int i = 0; i < csv.count(); ++i)
        cursor.insertText(csv.at(i));

    if (!writer->write(document))
        return false;
    else
        return true;
}
예제 #2
0
//-----------------------------------------------------------------
void Exporter::exportDoc()
{

    QTextDocument *document = buildFinalDoc();

    if(ui->fileTypeComboBox->currentIndex() == 0)
        format = "html";
    if(ui->fileTypeComboBox->currentIndex() == 1)
        format = "odt";
    if(ui->fileTypeComboBox->currentIndex() == 2){
        format = "txt";
        QMessageBox::information(this, tr("Project Exporter"), tr("You have selected the .txt format. There is no formatting !"), QMessageBox::Ok);

    }

    QTextDocumentWriter writer;
    QByteArray array;
    QString fileName(ui->pathLineEdit->text() + "/" + ui->fileNameLineEdit->text() + "." + format);
    writer.setFormat(array.append(format));
    writer.setFileName(fileName);
    writer.setCodec(QTextCodec::codecForName("UTF-8"));
    writer.write(document);

    if(format=="html"){

        QFile *textFile = new QFile;
        textFile->setFileName(fileName);
        bool opened = textFile->open(QFile::ReadWrite | QFile::Text);
        //                qDebug() << opened ;
        QTextStream textFileStream( textFile );
        QString textString(textFileStream.readAll());
        if (textFile->resize(0))
        {
            QRegExp regExpCss("<style type=\x0022text/css\x0022>.*</style>");
            regExpCss.setMinimal(true);
            textString = textString.replace( regExpCss, "<style type=\x0022text/css\x0022>p, li { white-space: pre-wrap; } p{line-height: 2em; font-family:'Liberation Serif'; font-size:12pt;margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:72px;}</style>");
            //                qDebug() << textString;
            QByteArray array;
            array.append(textString);
            textFile->write(array);
        }

        textFile->close();

    }




    QApplication::restoreOverrideCursor();

    QMessageBox::information(this, tr("Project exported"), tr("This project was successfully exported !"), QMessageBox::Ok);
}