• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            飛天

            快樂的生活......

             

            Bcb中的Excel操作類

                  最近做一個(gè)專案,要求能將Excel表中的數(shù)據(jù)轉(zhuǎn)入數(shù)據(jù)庫(kù),反之將數(shù)據(jù)庫(kù)的內(nèi)容導(dǎo)入到Excel表中。看了網(wǎng)上寫的關(guān)于操作Excel的方法,寫了一個(gè)簡(jiǎn)單的Excel操作類。功能還不是很完善,以后再慢慢修改。 
                
            ***********************************************************************
             
            * Module:  TExcel.cpp
             
            * Author:  cjz
             
            * Modified: 2007-07-29日 11:07:30
             
            * Purpose: Implementation of the class TExcel
             
            * Comment: Excel巨摸
             
            ***********************************************************************/

            #include 
            <vcl.h>
            #include 
            "TExcel.h"
            #include 
            "vcl\utilcls.h"

            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::TExcel()
            // Purpose:    Implementation of TExcel::TExcel()
            // Return:
            ////////////////////////////////////////////////////////////////////////

            TExcel::TExcel()
            {
                    Sheet
            =new TSheet();
                NewExcel();
            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::TExcel(AnsiString m_strFile)
            // Purpose:    Implementation of TExcel::TExcel()
            // Comment:    帶參數(shù)的構(gòu)造函數(shù)
            // Parameters:
            // - m_strFile
            // Return:
            ////////////////////////////////////////////////////////////////////////

            TExcel::TExcel(AnsiString m_strFile)
            {
               strFileName
            =m_strFile;
               Sheet
            =new TSheet();
               OpenExcel();
               
            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::~TExcel()
            // Purpose:    Implementation of TExcel::~TExcel()
            // Return:     
            ////////////////////////////////////////////////////////////////////////

            TExcel::
            ~TExcel()
            {
               
            //CloseExcel();
            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::OpenExcel()
            // Purpose:    Implementation of TExcel::OpenExcel()
            // Return:     bool
            ////////////////////////////////////////////////////////////////////////

            bool __fastcall TExcel::OpenExcel(void)
            {
               
            if(!FileExists(strFileName))
               
            {
                 MessageDlg(
            "ゅンぃ!",
                         mtWarning, TMsgDlgButtons() 
            << mbOK, 0);
                 
            return false;
               }

               
            try
               
            {
                   CoInitialize(NULL);
                   vEx
            =CreateOleObject("Excel.Application");
                   vEx.OPS(
            "Visible",false);
                   vEx.OPG(
            "WorkBooks").OPR("Open",strFileName.c_str());
                   vWb
            =vEx.OPG("ActiveWorkBook");
                   vSh
            =vWb.OPG("ActiveSheet");

                       nSheetCount
            =vWb.OPG("sheets").OPG("count");

               }

               
            catch()
               
            {
                    ShowMessage(
            "礚猭幣笆Excel,琌⊿Τ桿!");
                     
            return false;
               }

               
            return true;
            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::NewExcel()
            // Purpose:    Implementation of TExcel::NewExcel()
            // Return:     bool
            ////////////////////////////////////////////////////////////////////////

            bool __fastcall TExcel::NewExcel(void)
            {
               
            try
               
            {
                       CoInitialize(NULL);
                   vEx
            =Variant::CreateObject("Excel.Application");
                   vEx.OPS(
            "Visible",false);
                   vEx.OPG(
            "WorkBooks").OFN("Add",1);
                   vWb
            =vEx.OPG("ActiveWorkBook");
                   vSh
            =vWb.OPG("ActiveSheet");

                       Sheet
            ->vSh=&vSh;
                       nSheetCount
            =1;

               }

               
            catch()
               
            {
                    ShowMessage(
            "礚猭幣笆Excel,琌⊿Τ桿!");
                     
            return false;
               }

               
            return true;
            }


            //---------------------------------------------------------------------
            //睰痢
            void __fastcall TExcel::AddSheet(AnsiString SheetName)
            {
                vEx.OPG(
            "WorkBooks").OFN("Add",1);
                nSheetCount
            =vWb.OPG("sheets").OPG("count");
                Sheets[nSheetCount]
            ->Name=SheetName;
            }

            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::SaveExcel()
            // Purpose:    Implementation of TExcel::SaveExcel()
            // Return:     bool
            ////////////////////////////////////////////////////////////////////////

            bool  __fastcall TExcel::SaveExcel(void)
            {
               
            try
               
            {
                vWb.OFN(
            "save");
               }

               
            catch()
               
            {
                  MessageDlg(
            "玂Excelゅ郎ア毖!",
                        mtWarning, TMsgDlgButtons() 
            << mbOK, 0);
                     
            return false;
               }

               
            return true;

            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::SaveAsExcel()
            // Purpose:    Implementation of TExcel::SaveAsExcel()
            // Return:     bool
            ////////////////////////////////////////////////////////////////////////

            bool  __fastcall TExcel::SaveAsExcel(AnsiString fileName)
            {
               
            if(FileExists(fileName))
               
            {
                      
            if(MessageDlg("ゅン,琌灤籠?",mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)==mrYes)
                      
            {
                          DeleteFile(fileName);
                      }

                      
            else
                      
            {
                            vWb.OPG(
            "Application").OPS("DisplayAlerts",false);
                            
            return false;
                      }

               }

               
            try
               
            {
                 vWb.OFN(
            "SaveAs",fileName.c_str());
               }

               
            catch()
               
            {
                     MessageDlg(
            "玂Excelゅ郎ア毖!",
                        mtWarning, TMsgDlgButtons() 
            << mbOK, 0);
                     
            return false;
               }

               
            return true;
            }


            ////////////////////////////////////////////////////////////////////////
            // Name:       TExcel::CloseExcel()
            // Purpose:    Implementation of TExcel::CloseExcel()
            // Return:     void
            ////////////////////////////////////////////////////////////////////////

            void  __fastcall TExcel::CloseExcel(void)
            {
               vWb.OFN(
            "close");
               vEx.OFN(
            "quit");

               vWb
            =Unassigned;
               vEx
            =Unassigned;
               CoUninitialize();
            }


            //---------------------------------------------------------------------
            //妮┦:莉材i茂
            TSheet *  __fastcall TExcel::GetSheet(int i)
            {
                
            //狦諷玡痢i,玥玥痢i
                if(i==nCurrSheet)
                   
            return Sheet;
                
            else
                   nCurrSheet
            =i;
                
            if(i<1||i>nSheetCount)
                    
            throw "ぃ茂"+i;
                vSh
            =vWb.OPG("Sheets",i);

                nRows
            =vSh.OPG("UsedRange").OPG("Rows").OPG("count");
                nColumns
            =vSh.OPG("UsedRange").OPG("Columns").OPG("count");

                Sheet
            ->nRows=nRows;
                Sheet
            ->nColumns=nColumns;
                Sheet
            ->vSh=&vSh;
                
            return Sheet;

            }
            暈死,變成亂碼。
            TExcel.h 文件頭

            工作表類:

            TSheet 文件頭

                   使用方法:
                 1.打開文件
                     TExcel *excel=new TExcel("要打開的文件名");
                 2.新建文件
                     TExcel *excel=new TExcel();
                 3.獲取工作薄n的單元格(i,j)
                     excel->Sheets[n]->GetCells(i,j);返回值是一個(gè)Varaint類型。
                 4.設(shè)置工作薄n的單元格(i,j)值
                    excel->Sheets[n]->SetCell(i,j,value);
                  5.獲取或設(shè)置工作薄名稱
                    excel->Sheets[n]->Name="...";或sheetname=excel->Sheets[n]->Name;
                  6.保存和另存為。
                   excel->SaveExcel();
                   excel->SaveAsExcel("新的文件名");
                  7.關(guān)閉
                   excel->CloseExcel();

            posted on 2007-08-03 10:29 飛天 閱讀(1551) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++Builder

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            Blogs

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久久久亚洲AV无码观看| 久久久久99精品成人片欧美 | 亚洲精品视频久久久| 人妻无码精品久久亚瑟影视| 亚洲精品乱码久久久久66| 久久99精品久久久久久动态图| 国内精品久久久久久中文字幕| 一本色道久久88综合日韩精品| 国产精品美女久久久久网| 亚洲欧美国产日韩综合久久| 久久99精品久久久久久久不卡 | 九九99精品久久久久久| 亚洲精品无码久久久| 久久中文娱乐网| 久久久久久亚洲AV无码专区| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 精品久久久久久无码专区 | 久久国产精品无码网站| 日产精品久久久一区二区| 欧美成a人片免费看久久| 亚洲国产成人久久综合一| 99精品久久久久久久婷婷| 久久伊人中文无码| 久久996热精品xxxx| 国产成人香蕉久久久久| 青青草国产成人久久91网| 久久久久人妻一区精品色| 亚洲精品无码久久一线| 伊人久久大香线蕉成人| 无码精品久久一区二区三区| 久久久精品国产Sm最大网站| 国产精品无码久久久久| 91亚洲国产成人久久精品| 国产精品久久99| 2021国产成人精品久久| 国产精品欧美亚洲韩国日本久久| 亚洲成色999久久网站| 精品久久久久久国产三级| 久久久久亚洲AV成人网人人软件 | 欧美日韩精品久久久久| 欧美日韩中文字幕久久久不卡|