• <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操作類

                  最近做一個專案,要求能將Excel表中的數據轉入數據庫,反之將數據庫的內容導入到Excel表中。看了網上寫的關于操作Excel的方法,寫了一個簡單的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:    帶參數的構造函數
            // 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);返回值是一個Varaint類型。
                 4.設置工作薄n的單元格(i,j)值
                    excel->Sheets[n]->SetCell(i,j,value);
                  5.獲取或設置工作薄名稱
                    excel->Sheets[n]->Name="...";或sheetname=excel->Sheets[n]->Name;
                  6.保存和另存為。
                   excel->SaveExcel();
                   excel->SaveAsExcel("新的文件名");
                  7.關閉
                   excel->CloseExcel();

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

            導航

            統計

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            Blogs

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            噜噜噜色噜噜噜久久| 久久精品免费网站网| 开心久久婷婷综合中文字幕| AV无码久久久久不卡网站下载| 久久天天躁狠狠躁夜夜2020老熟妇 | 精品久久人妻av中文字幕| 久久综合伊人77777| 欧美久久一区二区三区| 久久AAAA片一区二区| 久久久久中文字幕| 日韩一区二区久久久久久 | 久久久久亚洲AV无码专区首JN| 久久精品国产99久久久香蕉| 精品久久久久中文字| 久久精品国产一区二区| 亚洲精品成人久久久| 久久人人青草97香蕉| 伊人久久大香线焦AV综合影院| 无码专区久久综合久中文字幕 | 亚洲AV日韩精品久久久久久久| 国产精品久久成人影院| 精品一区二区久久久久久久网站| 久久免费美女视频| 久久精品国产99久久丝袜| 麻豆久久久9性大片| 精品久久久久久亚洲精品 | 久久人人爽人人人人爽AV| 久久精品国产色蜜蜜麻豆| 久久久久人妻精品一区| 91精品国产色综久久| 人人狠狠综合久久亚洲| 国产精品久久久久AV福利动漫| 2021精品国产综合久久| 伊人久久大香线焦综合四虎| 欧美午夜精品久久久久久浪潮| 久久午夜羞羞影院免费观看| 久久中文娱乐网| 中文字幕热久久久久久久| 国产精品成人无码久久久久久 | 2020国产成人久久精品| 九九精品99久久久香蕉|