首页 > 代码库 > Qt-读取文本导出word

Qt-读取文本导出word

代码:

技术分享
  1 #include "FileOpeartion.h"  2   3   4 FileOpeartion::FileOpeartion (void)  5 {  6 }  7   8   9 QVector<simRecord> FileOpeartion::GetTheSimulationRecord() 10 { 11     QVector<simRecord> simTempList ; 12     return simTempList; 13 } 14  15 // lyy : 2016/8/26 16:40:11 说明:读取文本 16 bool FileOpeartion:: GetTheTextContent (const QString strPath,  QStringList &strContent) 17 { 18     QFile file (strPath); 19      20     if (file.open (QIODevice::ReadOnly | QIODevice::Text)) 21     { 22         QTextStream in (&file); 23         //in.setCodec (QTextCodec::codecForName ("GB2312")); 24         QString line; 25          26         while (!in.atEnd()) 27         { 28             line = in.readLine(); 29              30             if (!line.isNull() && !line.isEmpty()) 31             { 32                 strContent << line; 33             } 34         } 35          36         file.close(); 37     } 38      39     return true; 40 } 41 QString FileOpeartion::GetTheTextHtml (const QString strPath) 42 { 43     QString strContent; 44     strContent.append ("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns=\"http://www.w3.org/TR/REC-html40\">"); 45     strContent.append ("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); 46     strContent.append ("<head><meta http-equiv=Content-Type  content=\"text/html; charset=gb2312\" ></head>"); 47     strContent.append ("<body lang=ZH-CN>"); 48     strContent.append ("<table width=\"100%\" border=\"0\" height=\"90%\" >"); 49     strContent.append (" <tr>"); 50     strContent.append (" <td><strong>值班员:</strong><u><strong>"); 51     strContent.append ("");//replace one 52     strContent.append ("</strong></u></td>"); 53     strContent.append (" <td align=\"right\" style=\"text-align: right\"><strong>日期 :</strong><u><strong>"); 54     strContent.append ("");//replace two 55     strContent.append ("</strong></u></td>"); 56     strContent.append (" </tr>"); 57     strContent.append (" <tr>"); 58     strContent.append ("  <td colspan=\"2\" height=\"60\" ><div align=\"center\" style=\"font-size:25px;letter-spacing: 10px;;\"><strong>空 间 环 境 预 报 和 警 报</strong></div></td>"); 59     strContent.append ("</tr>"); 60     strContent.append (" <tr>"); 61     strContent.append ("<td colspan=\"2\" ><table width=\"100%\"  border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse:collapse\">"); 62     strContent.append (" <tr  align=\"center\">"); 63     strContent.append ("  <td colspan=\"2\" bgcolor=\"#999999\"><strong>项目</strong></td>"); 64     strContent.append (" <td width=\"20%\" bgcolor=\"#999999\">"); 65     strContent.append ("");//repalce three 66     strContent.append ("</td>"); 67     strContent.append (" <td width=\"20%\" bgcolor=\"#999999\">"); 68     strContent.append ("");//repalce three 69     strContent.append ("</td>"); 70     strContent.append (" <td width=\"20%\" bgcolor=\"#999999\">"); 71     strContent.append ("");//repalce three 72     strContent.append ("</td>"); 73     strContent.append (" </tr>"); 74     strContent.append ("  <tr>"); 75     strContent.append (" <td width=\"5%\" rowspan=\"8\" align=\"center\" ><strong>空<br>间<br>环<br>境<br>参<br> 数</strong><br></td>"); 76     strContent.append (" <td width=\"35%\" height=\"30\" style=\"text-align:left;\"><strong>太阳10.7cm流量</strong></td>"); 77     strContent.append (" <td align=\"center\">"); 78     strContent.append (""); 79     strContent.append ("</td>"); 80     strContent.append (" <td align=\"center\">"); 81     strContent.append (""); 82     strContent.append ("</td>"); 83     strContent.append (" <td align=\"center\">"); 84     strContent.append (""); 85     strContent.append ("</td>"); 86     strContent.append (" </tr>"); 87     strContent.append (" <tr>"); 88     strContent.append ("<td height=\"30\"><strong>太阳M级X耀斑概率</strong></td>"); 89     strContent.append (" <td align=\"center\">"); 90     strContent.append (""); 91     strContent.append ("</td>"); 92     strContent.append (" <td align=\"center\">"); 93     strContent.append (""); 94     strContent.append ("</td>"); 95     strContent.append (" <td align=\"center\">"); 96     strContent.append (""); 97     strContent.append ("</td>"); 98     strContent.append (" </tr>"); 99     strContent.append (" <tr>");100     strContent.append (" <td height=\"30\"><strong>太阳X级X耀斑概率</strong></td>");101     strContent.append (" <td align=\"center\">");102     strContent.append ("");103     strContent.append ("</td>");104     strContent.append (" <td align=\"center\">");105     strContent.append ("");106     strContent.append ("</td>");107     strContent.append (" <td align=\"center\">");108     strContent.append ("");109     strContent.append ("</td>");110     strContent.append (" </tr>");111     strContent.append (" <tr>");112     strContent.append (" <td height=\"30\"><strong>太阳质子事件发生概率</strong></td>");113     strContent.append (" <td align=\"center\">");114     strContent.append ("");115     strContent.append ("</td>");116     strContent.append (" <td align=\"center\">");117     strContent.append ("");118     strContent.append ("</td>");119     strContent.append (" <td align=\"center\">");120     strContent.append ("");121     strContent.append ("</td>");122     strContent.append (" </tr>");123     strContent.append (" <tr>");124     strContent.append (" <td height=\"30\"><strong>高能电子增强事件概率</strong></td>");125     strContent.append (" <td align=\"center\">");126     strContent.append ("");127     strContent.append ("</td>");128     strContent.append (" <td align=\"center\">");129     strContent.append ("");130     strContent.append ("</td>");131     strContent.append (" <td align=\"center\">");132     strContent.append ("");133     strContent.append ("</td>");134     strContent.append (" </tr>");135     strContent.append (" <tr>");136     strContent.append (" <td height=\"30\"><strong>地磁Ap指数</strong></td>");137     strContent.append (" <td align=\"center\">");138     strContent.append ("");139     strContent.append ("</td>");140     strContent.append (" <td align=\"center\">");141     strContent.append ("");142     strContent.append ("</td>");143     strContent.append (" <td align=\"center\">");144     strContent.append ("");145     strContent.append ("</td>");146     strContent.append (" </tr>");147     strContent.append (" <tr>");148     strContent.append (" <td height=\"30\"><strong>小地磁暴发生概率</strong></td>");149     strContent.append (" <td align=\"center\">");150     strContent.append ("");151     strContent.append ("</td>");152     strContent.append (" <td align=\"center\">");153     strContent.append ("");154     strContent.append ("</td>");155     strContent.append (" <td align=\"center\">");156     strContent.append ("");157     strContent.append ("</td>");158     strContent.append (" </tr>");159     strContent.append (" <tr>");160     strContent.append (" <td height=\"30\"><strong>大地磁暴发生概率</strong></td>");161     strContent.append (" <td align=\"center\">");162     strContent.append ("");163     strContent.append ("</td>");164     strContent.append (" <td align=\"center\">");165     strContent.append ("");166     strContent.append ("</td>");167     strContent.append (" <td align=\"center\">");168     strContent.append ("");169     strContent.append ("</td>");170     strContent.append (" </tr>");171     strContent.append (" <tr>");172     strContent.append (" <td colspan=\"2\" align=\"center\" height=\"110\" ><strong>空间环境综述:</strong></td>");173     strContent.append (" <td colspan=\"3\" class=\"padding10\">");174     strContent.append ("");175     strContent.append ("</td>");176     strContent.append (" </tr>");177     strContent.append (" <tr>");178     strContent.append (" <td colspan=\"2\" align=\"center\" height=\"110\" ><strong>空间环境形势:</strong></td>");179     strContent.append (" <td colspan=\"3\" class=\"padding10\">");180     strContent.append ("");181     strContent.append ("</td>");182     strContent.append (" </tr>");183     strContent.append (" <tr>");184     strContent.append (" <td colspan=\"2\" align=\"center\" height=\"110\" ><strong>空间环境警报:</strong></td>");185     strContent.append (" <td colspan=\"3\" class=\"padding10\">");186     strContent.append ("");187     strContent.append ("</td>");188     strContent.append (" </tr>");189     strContent.append (" <tr>");190     strContent.append ("  <td colspan=\"2\" align=\"center\" height=\"40\" ><strong>备注:</strong></td>");191     strContent.append (" <td colspan=\"3\">");192     strContent.append ("</td>");193     strContent.append (" </tr>");194     strContent.append (" </table></td>");195     strContent.append ("</tr>");196     strContent.append ("<tr>  <td colspan=\"2\" height=\"50\">&nbsp</td> </tr>");197     strContent.append (" <tr>");198     strContent.append (" <td colspan=\"2\" height=\"30\"><div align=\"right\"><strong>中国科学院空间环境研究预报中心</strong></div></td>");199     strContent.append (" </tr>");200     strContent.append ("<tr>");201     strContent.append ("<td colspan=\"2\" height=\"30\"><div align=\"right\"><strong>中国科学院国家空间科学中心</strong></div></td>");202     strContent.append ("</tr>");203     strContent.append ("</table>");204     strContent.append ("</body>");205     strContent.append ("</html>");206     strContent.append ("");207     strContent.append ("");208     strContent.append ("");209     strContent.append ("");210     strContent.append ("");211     strContent.append ("");212     strContent.append ("");213     strContent.append ("");214     strContent.append ("");215     strContent.append ("");216     strContent.append ("");217     strContent.append ("");218     strContent.append ("");219     strContent.append ("");220     strContent.append ("");221     strContent.append ("");222     strContent.append ("");223     strContent.append ("");224     strContent.append ("");225     strContent.append ("");226     strContent.append ("");227     strContent.append ("");228     strContent.append ("");229     strContent.append ("");230     strContent.append ("");231     strContent.append ("");232     strContent.append ("");233     strContent.append ("");234     strContent.append ("");235     strContent.append ("");236     strContent.append ("");237     strContent.append ("");238     strContent.append ("");239     strContent.append ("");240     strContent.append ("");241     strContent.append ("");242     strContent.append ("");243     strContent.append ("");244     strContent.append ("");245     strContent.append ("");246     strContent.append ("");247     strContent.append ("");248     strContent.append ("");249     strContent.append ("");250     strContent.append ("");251     strContent.append ("");252     strContent.append ("");253     strContent.append ("");254     strContent.append ("");255     // QFile file (strPath);256     //if (file.open (QIODevice::ReadOnly | QIODevice::Text))257     //{258     //    QTextStream in (&file);259     //    //in.setCodec (QTextCodec::codecForName ("GB2312"));260     //    QString line;261     //262     //    while (!in.atEnd())263     //    {264     //        line = in.readLine();265     //266     //        if (!line.isNull() && !line.isEmpty())267     //        {268     //            strContent.append (line);269     //        }270     //    }271     //272     //    file.close();273     //}274     return strContent;275 }276 277 QStringList FileOpeartion::GetThePicList (const QString strPath, QDateTime dt)278 {279     QStringList strTempList;280     QFile dataFile (strPath);281     282     if (dataFile.open (QIODevice::ReadOnly | QIODevice::Text))283     {284         QTextStream in (&dataFile);285         QString line;286         //  bool isNull = true;287         QString tmp = dt.toString ("yyyyMMdd hh:mm:ss");288         289         while (!in.atEnd())290         {291             line = in.readLine();292             /*QRegExp rx("^[# | :].*");*/293             QRegExp rx1 ("^#");294             QRegExp rx2 ("^:");295             296             if (line.indexOf (rx1) >= 0 || line.indexOf (rx2) >= 0)297             {298                 continue;299             }300             301             else302             {303                 QStringList pics = line.split ("/");304                 305                 if (pics.count() < 4)306                 {307                     continue;308                 }309                 310                 const QString picTime = pics.at (0).trimmed();311                 QString dtTime = dt.toString ("yyyyMMdd");312                 313                 if (picTime.startsWith (dtTime))314                 {315                     //  picfile = pics[4];316                     strTempList.append (pics[4]);317                     // isNull = false;318                 }319             }320         }321         322         dataFile.close();323     }324     325     return strTempList;326 }327 328 QStringList FileOpeartion::PathCombine (const QString strPath, QStringList strListTemp)329 {330     QStringList strReturn;331     332     foreach (const QString &str, strListTemp)333     {334         if (!str.isEmpty())335         {336             strReturn << strPath.trimmed() + str.trimmed();337         }338     }339     340     return strReturn;341 }342 343 344 QPixmap FileOpeartion::GetPicture (QString strPath, int mWidth, int mHeight)345 {346     QPixmap pictureimg;347     //"D:dataFolder/2015/03/15/AIA094/20150315_001402_1024_0094.jpg"348     349     if (pictureimg.load (strPath))350     {351         pictureimg = pictureimg.scaled (mWidth, mHeight, Qt::KeepAspectRatio);352     }353     354     return pictureimg;355 }356 357 FileOpeartion::~FileOpeartion (void)358 {359 }
View Code
技术分享
 1 #pragma once 2  3 #include <QString> 4 #include <QFile> 5 #include <QTextStream> 6 #include <QTextCodec> 7 #include <QStringList> 8 #include <QDateTime> 9 #include <QPixmap>10 #include "DataSource.h"11 12 class FileOpeartion13 {14     public:15     16         FileOpeartion (void);17         18         static bool GetTheTextContent (const QString strPath, QStringList &strContent);19         20         static QString GetTheTextHtml (const QString strPath);21         22         static QStringList GetThePicList (const QString strPath, QDateTime dt);23         24         static QStringList PathCombine (const QString strPath, QStringList strListTemp);25         26         static QPixmap  GetPicture (QString strPath, int mWidth, int mHeight);27         28         // lyy : 2016/9/20 10:31:01 说明:先定义一个签名,没有实现,邹说要统一更改现原有的配置文件29         static QVector<simRecord> GetTheSimulationRecord();30         31         ~FileOpeartion (void);32         33         34         35 };
View Code
void DailyPaper::outPrintDoc(){    QString html;    html = FileOpeartion::GetTheTextHtml ("C:/html.txt");    QFile outFile ("save.doc");    outFile.open (QIODevice::WriteOnly/* | QIODevice::Append*/);    QTextStream ts (&outFile);    ts << html << endl;    outFile.close();    //打开文件    QDesktopServices::openUrl (QUrl::fromLocalFile (QFileInfo ("save.doc").absoluteFilePath()));    //  QMessageBox::information (this, "test", "请在根目录下查看");}

技术分享

设计自己的网页样式就可以了

Qt-读取文本导出word