Exceldan import qilish - Microsoft Word umk d2 sirtqi docx

Exceldan import qilish.
Oldingi laboratoriya mashg’ulotimizda hisobotni Microsoft Excelga export
qilgan edik. Endigi laboratoriya mashg’ulotida ma’lumotlarni Microsoft Exceldan
import qilishga to’xtalib o’tamiz.
Microsoft Access dasturi yordamida kitoblar nomli ma’lumotlar ombori
yaratamiz:
112
Microsoft Access dasturi yordamida ma’lumotlar ombori yaratish.
Ma’lumotlar omborini C++ Builder dasturiga bo’glab quyidagicha
interfeysni yaratamiz:
C++ Builder XE3.
Yuklanayotgan faylda necha satr ma’lumot borligini oldindan bilish talab
etiladi. Satrlar sonini kiritib OK tugmasini bosish orqali faylni ko’rsatish oynasi
ochiladi.
Dasturchi foydalanuvchiga qulay bo'lishi uchun "shablon.xls" nomli fayl
tayyorlab qo'yildi. Siz maydonlarni o'zgartirmagan xolda ularni to'ldirib kerakli
joyga saqlaysiz va faylingizdagi ma'lumotlar qator(satr)ini eslab qoling.
113
Shablon fayli.
ARMga
kitoblarni
import
qilish
uchun
Kutubxona
bo'limidan
tugmasi bosiladi. Natijada quyidagi oyna xosil bo'ladi.
Satrni kiritish.
Excel faylingizdagi ma'lumotlar satrini kiritasiz va OK tugmasini bosasiz.
Natijada "*.xls" (Excel) faylni ko'rsatish uchun oyna xosil bo'ladi. Siz oldindan
tayyorlab qo'yilgan "*.xls" faylingizni ko'rsatasiz va "Открыть" tugmasini bosasiz.
114
Faylni ko’rsatish.
Dastur kodi:
tugmasi bosilishiga:
void __fastcall Tasosiyoyna::Action5Execute(TObject *Sender)
{
Form6->Show();
}
Form6 oynasi quyidagi oynadir:
Ushbu formaga mos holda quyidagi kodlar yoziladi:
//----------------------------------------------------------------
-----------
#include
115
#pragma hdrstop
#include
#include
#include "Unit6.h"
#include "Unit1.h"
#include "Unit2.h"
//----------------------------------------------------------------
-----------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm6 *Form6;
//----------------------------------------------------------------
-----------
__fastcall TForm6::TForm6(TComponent* Owner)
: TForm(Owner)
{}
//----------------------------------------------------------------
-----------
void __fastcall TForm6::BitBtn1Click(TObject *Sender)
{Integer satr=2, ustun=2;
AnsiString n=StrToInt(Edit1->Text);
AnsiString s;
Variant excel, sheet;
if (OpenDialog1->Execute())
{
s=OpenDialog1->FileName;
excel=CreateOleObject("Excel.Application");
excel.OlePropertyGet("Application").OlePropertyGet("Workbooks"
).OleProcedure("Open",WideString(s));
excel.OlePropertySet("Visible",false);
sheet=excel.OlePropertyGet("ActiveSheet");
for (int i = 0; i < n; i++)
{
DM->ADOkitoblar->Insert();
DM->ADOkitoblar->FieldByName("BookName")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun).OlePropertyGet("Value");
DM->ADOkitoblar->FieldByName("BookAvtor")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun+1).OlePropertyGet("Value");;
DM->ADOkitoblar->FieldByName("BookNashr")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun+2).OlePropertyGet("Value");;
DM->ADOkitoblar->FieldByName("BookSonni")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun+3).OlePropertyGet("Value");;
DM->ADOkitoblar->FieldByName("QolganSoni")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun+4).OlePropertyGet("Value");;
DM->ADOkitoblar->FieldByName("QoldaSoni")-
>AsString=sheet.OlePropertyGet("Cells").OlePropertyGet("Item",i+sa
tr,ustun+5).OlePropertyGet("Value");;
DM->ADOkitoblar->Post();
DM->ADOkitoblar->Active=False;
DM->ADOkitoblar->Active=True;
116
}
excel.OlePropertyGet("Application").OleProcedure("Quit");
sheet=Unassigned;
excel=Unassigned;
}
Form6->Close();
}//---------------------------------------------------------------
------------
void __fastcall TForm6::BitBtn2Click(TObject *Sender)
{Form6->Close();}
//----------------------------------------------------------------
-----------
void __fastcall TForm6::FormShow(TObject *Sender)
{Form6->Edit1->SetFocus();}
//----------------------------------------------------------------
-----------
|