Go to the documentation of this file.00001 #include "exporttablethread.h"
00002
00003 ExportTableThread::ExportTableThread(QObject *parent) :
00004 QThread(parent)
00005 {
00006 }
00007
00008 void ExportTableThread::run(QSqlDatabase db, QString dbName, QString tableName,
00009 QString fileName, QString fieldDeliminator, QString fieldEncloser,
00010 QString lineDeliminator, bool addColumnsOnTop, bool CSV, bool HTML,
00011 bool XML, bool Excel) {
00012 db.open();
00013 if (db.isOpen()) {
00014
00015 QString textToExport;
00016 QString lineTerminator = lineDeliminator;
00017 QSqlQuery query = QSqlQuery(db);
00018 query.exec(tr("use `%1`").arg(dbName));
00019 query.clear();
00020 if (query.exec(strQuery)) {
00021 emit rowCount(query.size());
00022 int fin = 0;
00023 if (CSV) {
00024 if (addColumnsOnTop) {
00025 QSqlQuery query1 = QSqlQuery(db);
00026 if (query1.exec(strQuery)) {
00027 QString rowText = "";
00028 query1.first();
00029 QSqlRecord headerRec = query1.record();
00030 for (int irec = 0; irec < headerRec.count(); irec++) {
00031 if (columnList.contains(headerRec.fieldName(irec))) {
00032 rowText.append(fieldEncloser);
00033 rowText.append(headerRec.fieldName(irec));
00034 rowText.append(fieldEncloser);
00035 rowText.append(fieldDeliminator);
00036 }
00037 }
00038 rowText = rowText.left(rowText.length() -1);
00039 textToExport.append(rowText);
00040 textToExport.append(lineTerminator);
00041 query1.clear();
00042 }
00043 }
00044 while (query.next()) {
00045 fin = fin + 1;
00046 QSqlRecord rec = query.record();
00047 QString rowText = "";
00048 for (int col = 0; col < rec.count(); col++) {
00049 if (columnList.contains(rec.fieldName(col))) {
00050 rowText.append(fieldEncloser);
00051 rowText.append(rec.value(col).toString());
00052 rowText.append(fieldEncloser);
00053 rowText.append(fieldDeliminator);
00054 }
00055 }
00056 rowText = rowText.left(rowText.length() -1);
00057 textToExport.append(rowText);
00058 textToExport.append(lineTerminator);
00059 emit finishedRows(fin);
00060 }
00061 } else if (HTML) {
00062 QString table;
00063 table = "<html>";
00064 table.append("\n<head>");
00065 table.append("\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
00066 table.append(tr("<title>Output for %1</title>").arg(tableName));
00067 table.append(tr("\n"
00068 "<style type=\"text/css\" <!--\n"
00069 ".normal { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #000000}\n"
00070 ".medium { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 15px; font-weight: bold; color #000000; text-decoration: none}\n"
00071 "--></style>"
00072 "\n"));
00073 table.append("\n</head>");
00074 table.append("\n<body>");
00075 table.append(tr("\n<h3>%1</h3><table border=1>\n<tr>").arg(tableName));
00076 textToExport = table;
00077 QSqlQuery query1 = QSqlQuery(db);
00078 if (query1.exec(strQuery)) {
00079 QString rowText = "";
00080 query1.first();
00081 QSqlRecord headerRec = query1.record();
00082 for (int irec = 0; irec < headerRec.count(); irec++) {
00083 if (columnList.contains(headerRec.fieldName(irec))) {
00084 rowText.append("<td bgcolor=silver class='medium'>");
00085 rowText.append(headerRec.fieldName(irec));
00086 rowText.append("</td>");
00087 rowText.append("\n");
00088 }
00089 }
00090 rowText.append("\n</tr>");
00091 textToExport.append(rowText);
00092 textToExport.append(lineTerminator);
00093 query1.clear();
00094 }
00095 while (query.next()) {
00096 fin = fin + 1;
00097 QSqlRecord rec = query.record();
00098 QString rowText = "\n<tr>";
00099 for (int col = 0; col < rec.count(); col++) {
00100 if (columnList.contains(rec.fieldName(col))) {
00101 rowText.append("<td class='normal' valign='top'>");
00102 rowText.append(rec.value(col).toString());
00103 rowText.append("</td>");
00104 rowText.append("\n");
00105 }
00106 }
00107 rowText.append("</tr>");
00108 textToExport.append(rowText);
00109 emit finishedRows(fin);
00110 }
00111 textToExport.append("\n</table></body></html>");
00112 } else if (XML) {
00113 QString header = "<data>";
00114 textToExport = header;
00115 while (query.next()) {
00116 fin = fin + 1;
00117 QSqlRecord rec = query.record();
00118 QString rowText = "\n<row>";
00119 for (int col = 0; col < rec.count(); col++) {
00120 if (columnList.contains(rec.fieldName(col))) {
00121 rowText.append(tr("<%1>").arg(rec.fieldName(col)));
00122 rowText.append(rec.value(col).toString());
00123 rowText.append(tr("</%1>").arg(rec.fieldName(col)));
00124 }
00125 }
00126 rowText.append("</row>");
00127 textToExport.append(rowText);
00128 emit finishedRows(fin);
00129 }
00130 textToExport.append("\n</data>");
00131
00132 } else if (Excel) {
00133
00134 QString header = "";
00135 header.append(tr(""
00136 "<?xml version\"1.0\"?>"
00137 "\n<?mso-application progid=\"Excel.Sheet\"?>"
00138 "\n<Workbook"
00139 "\n xmlns:x=\"urn:schemas-microsoft-com:office:excel\""
00140 "\n xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""
00141 "\n xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">"
00142 "\n<styles>"
00143 "\n<style ss:ID=\"Default\" ss:\"Normal\">"
00144 "\n<Alignment ss:Vertical=\"Bottom\"/>"
00145 "\n<Borders/>"
00146 "\n<Font/>"
00147 "\n<Interior/>"
00148 "\n<NumberFormat/>"
00149 "\n<Protection/>"
00150 "\n</style>"
00151 "\n</styles>"
00152 "\n"));
00153 header.append("<Worksheet ss:Name=\"Sheet1\">\n<ss:Table>");
00154 textToExport = header;
00155 while (query.next()) {
00156 fin = fin + 1;
00157 QSqlRecord rec = query.record();
00158 QString rowText = "\n<ss:Row>\n";
00159 for (int col = 0; col < rec.count(); col++) {
00160 if (columnList.contains(rec.fieldName(col))) {
00161
00162 rowText.append(tr("<ss:Cell><Data ss:Type=\"String\">%1</Data></ss:Cell>\n").arg(rec.value(col).toString()));
00163
00164 }
00165 }
00166 rowText.append("</ss:Row>");
00167 textToExport.append(rowText);
00168 emit finishedRows(fin);
00169 }
00170 textToExport.append("\n</ss:Table>\n</Worksheet>\n</Workbook>");
00171 }
00172
00173 QFile file(fileName);
00174 if (!file.open(QFile::WriteOnly | QFile::Text)) {
00175 return;
00176 }
00177 QTextStream out(&file);
00178 out << textToExport;
00179 emit completed(true);
00180 } else {
00181 emit completed(false);
00182 }
00183 }
00184 }