⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit2.~cpp

📁 此程序将Acesses文件调入EXCEL,WORD
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <comobj.hpp>
#include <utilcls.h>
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button1Click(TObject *Sender)
{
if(!DBGrid1->DataSource->DataSet->Active) // 数据集没有打开就返回
        return;
    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
                "DBGrid2Excel", MB_OK | MB_ICONERROR);
        return;
    }
    // 隐藏Excel界面
    vExcelApp.OlePropertySet("Visible", false);
    // 新建一个工作表
    vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
    // 操作这个工作表
    vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OlePropertyGet("Sheets", 1);
    // 设置Excel文档的字体
    vSheet.OleProcedure("Select");
    vSheet.OlePropertyGet("Cells").OleProcedure("Select");
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size", DBGrid1->Font->Size);
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name", DBGrid1->Font->Name.c_str());
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("FontStyle", "常规");
    vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
    // 表格的行数
    int nRowCount(DBGrid1->DataSource->DataSet->RecordCount + 1);
    nRowCount = nRowCount < 2? 2: nRowCount;
    // 表格的列数
    int nColCount(DBGrid1->Columns->Count);
    nColCount = nColCount < 1? 1: nColCount;
    // 设置单元格的宽度
    for(int i=0; i<nColCount; i++)
    {
        int nColWidth = DBGrid1->Columns->Items[i]->Width;
        vExcelApp.OlePropertyGet("Columns", i + 1).OlePropertySet("ColumnWidth", nColWidth / 7);
    }

    // 先将列名写入Excel表格
    for(int j=0; j<DBGrid1->Columns->Count; j++)
    {
        // 标题行的行高
        vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
        //
        vSheet.OlePropertyGet("Cells", 1, j + 1).OlePropertySet("Value",DBGrid1->Columns->Items[j]->FieldName.c_str());
        // 设置列名单元格的背景色
        Variant vInter = vSheet.OlePropertyGet(
                "Cells", 1, j + 1).OlePropertyGet("Interior");
        vInter.OlePropertySet("ColorIndex", 15); // 灰色
        vInter.OlePropertySet("Pattern", 1); // xlSolid
        vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
    }
    // 将DBGrid中的数据写入Excel表格
    DBGrid1->DataSource->DataSet->First();
    for(int i=0; i<nRowCount; i++)
    {
        // 普通数据行的行高16
        vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
        // 63 63 72 75 6E 2E 63 6F 6D
        for(int j=0; j<DBGrid1->Columns->Count; j++)
        {
            vSheet.OlePropertyGet("Cells", i + 2, j + 1)
                .OlePropertySet("Value",
                DBGrid1->DataSource->DataSet->FieldByName(DBGrid1->Columns->Items[j]->FieldName)->AsString.c_str());
        }
        DBGrid1->DataSource->DataSet->Next();
    }
    // 保存Excel文档并退出
   // vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs", strXlsFile.c_str());
    vExcelApp.OleFunction("Quit");
    vSheet = Unassigned;
    vExcelApp = Unassigned;
    // 工作结束
    MessageBox(0, "DBGrid2Excel 转换结束!",
            "DBGrid2Excel", MB_OK | MB_ICONINFORMATION);

}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
       Variant vExcelApp, vWorkbook, vSheet, vRange;
        int iCount=0;
        AnsiString strExcelFile=GetCurrentDir() + "\\student.xls";
        if(!FileExists(strExcelFile)){
                MessageBox(NULL,"文件不存在!","提示",MB_OK);
                return;
        }

        try{

                vExcelApp = Variant::CreateObject("Excel.Application");
        }
        catch(...)
        {
                vExcelApp=Unassigned;
                MessageBox(NULL,"无法启动Excel,可能尚未安装或文件已经损坏!","提示",MB_OK);
                return;
        }

        try{
                vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", strExcelFile.c_str());
                vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
                vSheet = vWorkbook.OlePropertyGet("ActiveSheet");
        }
        catch(...)
        {
                vExcelApp.OleFunction("Quit");
                vExcelApp = Unassigned;
                MessageBox(NULL,"文件被破坏!","提示",MB_OK);
                return;
        }

        int iRowCount=vSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows")
                .OlePropertyGet("Count");
        int iColCount=vSheet.OlePropertyGet("UsedRange").OlePropertyGet("Columns")
                .OlePropertyGet("Count");

        int iFieldCount=ADODataSet1->FieldCount;

        if(iColCount!=iFieldCount)
        {
                vExcelApp.OleFunction("Quit");
                vExcelApp = Unassigned;
                MessageBox(NULL,"文件格式不正确!","提示",MB_OK);
                return;
        }
        for(int i=1; i<iRowCount; i++)
        {
                ADODataSet1->Append();
                for(int j=0; j<iColCount; j++)
                {
                        ADODataSet1->Fields->Fields[j]->Value =  vSheet.OlePropertyGet("Cells", i + 1, j + 1)
                                .OlePropertyGet("Value");
                }
                ADODataSet1->Post();
                iCount++;
        }
        
        vExcelApp.OleFunction("Quit");
        vExcelApp = Unassigned;

        MessageBox(NULL,(AnsiString("ExcelToAccess共导入记录数:")+IntToStr(iCount)).c_str(),"ExcelToAccess",MB_OK);




}
//---------------------------------------------------------------------------



void __fastcall TForm2::Button3Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -