Q4.7.0T操作EXCEL

 這兩天楊老師讓我處理一下學校EXCEL課表的數據,需要篩選整理出用到的信息,於是我就用編了一個QT程序處理了一下,兩天的時間總算完成了任務。源程序代碼如下:

qtandexcel.pro

#-------------------------------------------------

#
# Project created by QtCreator 2011-08-19T19:46:26
#
#-------------------------------------------------

QT       += core gui

TARGET = qtandexcel
TEMPLATE = app


SOURCES += main.cpp

HEADERS  +=
CONFIG  += console    //qDebug打印信息的需要
CONFIG  +=qaxcontainer //QT操作EXCEL的相關控件

 

main.cpp

#include <QtGui/QApplication>
#include <QAxObject>
#include <QAxWidget>
#include <QAxSelect>
#include <qaxselect.h>
#include <QDebug>
#include <QFile>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QAxObject* excel = new QAxObject("Excel.Application");
    excel->setProperty("Visible",true);
    QAxObject* workbooks=excel->querySubObject("WorkBooks");
    workbooks->dynamicCall("Open(const QString&)",QString("E:/2011-2012biao.xls"));//表格名字不能有中文
    /*獲取活動工作薄*/
    QAxObject* workbook=excel->querySubObject("ActiveWorkBook");
    /*獲取工作表總數*/
    QAxObject* worksheets=workbook->querySubObject("WorkSheets");
    int intCount=worksheets->property("Count").toInt();
    qDebug()<<"the num of worksheets : "<<intCount<<endl;//1
    /*獲取第一張工作表的起始行,總行數,起始列,總列數*/
    QAxObject* worksheet=workbook->querySubObject("Worksheets(int)",1);
    QAxObject* usedrange=worksheet->querySubObject("UsedRange");
    QAxObject* rows=usedrange->querySubObject("Rows");
    QAxObject* columns=usedrange->querySubObject("Columns");
    int intRowStart=usedrange->property("Row").toInt();
    int intColStart=usedrange->property("Column").toInt();
    int intCols=columns->property("Count").toInt();
    int intRows=rows->property("Count").toInt();
    qDebug()<<"RowStart,Rows,ColStart,Cols : "<<intRowStart<<intRows<<intColStart<<intCols<<endl;

    /*從excel中讀取數據,並寫入rawbiao.txt*/
    QFile data("rawbiao.txt");
    if(data.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream out(&data);
        for(int i=intRowStart;i<intRowStart+intRows;i++)
        {
          for(int j=intColStart;j<intColStart+intCols;j++)
          {
            QAxObject* range=worksheet->querySubObject("Cells(int,int)",i,j);
            out<<range->property("Value").toString()<<";";//用分號做隔列符
          }
          out<<"\r\n";
        }
    }
    data.close();
    qDebug()<<"read excel data to rawbiao.txt complete!!"<<endl;

    /*篩選出北區課表,刪除東區*/
    QString oneline,xiaoqu,qizhiweek,danshuangzhou,week,jieci,classname,teachername,teachernumber,newline;
    int i=0;
    int j=0;
    int totalline=0;

    QFile bqbiao("bqkebiao.txt");//存放篩選出的北區課表
    if(bqbiao.open(QFile::WriteOnly | QFile::Truncate))
    {
        QTextStream out(&bqbiao);

        if(data.open(QIODevice::ReadOnly))
        {
            QTextStream in(&data);
            /*讀取第一行標題各列並直接寫入txt*/
            oneline=in.readLine();
            qizhiweek=oneline.section(";",9,9);//起始周
            qizhiweek.append(";");
            danshuangzhou=oneline.section(";",8,8);//單雙週
            danshuangzhou.append(";");
            week=oneline.section(";",6,6);//星期
            week.append(";");
            jieci=oneline.section(";",7,7);//節次
            jieci.append(";");
            classname=oneline.section(";",2,2);//課程名稱
            classname.append(";");
            teachername=oneline.section(";",4,4);//教師姓名
            teachername.append(";");
            teachernumber=oneline.section(";",3,3);//教師工號
            newline=qizhiweek.append(danshuangzhou);
            newline.append(week);
            newline.append(jieci);
            newline.append(classname);
            newline.append(teachername);
            newline.append(teachernumber);
            out<<newline<<"\r\n";

            while(!in.atEnd())
            {
               oneline=in.readLine();
               totalline++;
               xiaoqu=oneline.section(";",11,11);
               if(xiaoqu.contains("2"))//是否是北區的課表信息
               {
                   i++;
                   qizhiweek=oneline.section(";",9,9);//起始周
                   qizhiweek.append(";");
                   danshuangzhou=oneline.section(";",8,8);//單雙週
                   danshuangzhou.append(";");
                   week=oneline.section(";",6,6);//星期
                   week.append(";");
                   jieci=oneline.section(";",7,7);//節次
                   jieci.append(";");
                   classname=oneline.section(";",2,2);//課程名稱
                   classname.append(";");
                   teachername=oneline.section(";",4,4);//教師姓名
                   teachername.append(";");
                   teachernumber=oneline.section(";",3,3);//教師工號
                   newline=qizhiweek.append(danshuangzhou);
                   newline.append(week);
                   newline.append(jieci);
                   newline.append(classname);
                   newline.append(teachername);
                   newline.append(teachernumber);
                   out<<newline<<"\r\n";
               }
               else if(xiaoqu.contains("1"))
               {
                   j++;
               }
               else
               {
                   qDebug()<<totalline<<xiaoqu<<endl;
               }
           }
        }
    }
    data.close();
    bqbiao.close();
    qDebug()<<totalline<<i<<j<<endl;
    qDebug()<<"beiqu kebiao is complete!!";

    excel->setProperty("DisplayAlerts",0);
    workbook->dynamicCall("Save(void)");
    workbook->dynamicCall("Close(Boolean)",false);
    excel->dynamicCall("Quit(void)");
    excel->setProperty("DisplayAlerts",1);
    delete excel;
    return a.exec();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章