Ввод данных в Excel
Хорошо, если приведете пример.
(Borland C++ Builder 5.0)
Использую в головной программе
SP_Otchet->Open();
RunExportToExcel(SP_Otchet);
SP_Otchet->Close();
/*******************************************/
#include <vcl.h>
#include "ExportToExcel.h"
#include "Excel.h"
void __fastcall RunExportToExcel(TDataSet *_DataSet)
{
TfrmToExcel *frmToExcel;
if (_DataSet->Active == false)
{
ShowMessage("Impossible run this operation. Activuate request!");
exit(-1);
}
Application->CreateForm(__classid(TfrmToExcel), &frmToExcel);
frmToExcel->MainDataSet = _DataSet;
frmToExcel->ShowModal();
}
/*******************************************/
#include <vcl.h>
#include "Excel.h"
#include "ExportToExcel.h"
TfrmToExcel *frmToExcel;
__fastcall TfrmToExcelTfrmToExcel(TComponent* Owner)
TForm(Owner){}
void __fastcall TfrmToExcelBitBtn1Click(TObject *Sender)
{
BOOL CheckFields;
Variant ExcelLists, ExcelList, ListSheet;
int ARec, ACol, i;
TBookmark _BookMark;
_FileName = "";
ExcelObject = CreateOleObject("Excel.Application");
ExcelLists = ExcelObject.OlePropertyGet("Workbooks");
if(SaveDialog1->Execute())
{
_FileName = SaveDialog1->FileName;
if(!FileExists(_FileName))
{
int iFileHandle;
iFileHandle = FileCreate(_FileName);
if(!iFileHandle)
throw Exception("Cannot create file");
FileClose(iFileHandle);
}
}
else exit(0);
ExcelLists.Exec(Procedure("Open")<<_FileName);
ExcelList = ExcelLists.OlePropertyGet("item",1);
ListSheet= ExcelList.OlePropertyGet("WorkSheets",1);
_BookMark = MainDataSet->GetBookmark();
MainDataSet->DisableControls();
MainDataSet->First();
ACol = 0;
TStringList *list;
list = new TStringList;
MainDataSet->GetFieldNames(list);
for(int i=0; i<list->Count; i++)
{
setValue(0, ACol, list->Strings, ListSheet);
ACol++;
}
ARec = 1;
while (!MainDataSet->Eof)
{
ACol = 0;
for(int i=0; i<list->Count; i++)
if(!MainDataSet->FieldByName(list->Strings)->Value.IsNull())
try
{
setValue(ARec, ACol, MainDataSet->FieldByName(list->Strings)->Value, ListSheet);
}
catch(...){}
ACol++;
}
Application->ProcessMessages();
MainDataSet->Next();
ARec++;
}
MainDataSet->GotoBookmark(_BookMark);
MainDataSet->FreeBookmark(_BookMark);
MainDataSet->EnableControls();
ShowMessage("Export complete!");
ExcelObject.OlePropertySet("Visible", 1);
}
char* __fastcall TfrmToExceltoText(int row, int col)
{
static char cellText[256] ;
cellText[0] = 'A' + col ;
sprintf(&cellText[1],"%d",row+1);
return cellText;
}
void __fastcall TfrmToExcelsetValue(int row, int col, AnsiString as, Variant ListSheet)
{
Variant r = ListSheet.OlePropertyGet("Range", toText(row,col) );
r.OlePropertySet("Value", String(as));
}
On 2001-10-17 0759, Ivan wrote
У меня нормально данный код работает в BC5
Использую в головной программе
SP_Otchet->Open();
RunExportToExcel(SP_Otchet);
SP_Otchet->Close();
/*******************************************/
#include <vcl.h>
#include "ExportToExcel.h"
#include "Excel.h"
void __fastcall RunExportToExcel(TDataSet *_DataSet)
{
TfrmToExcel *frmToExcel;
if (_DataSet->Active == false)
{
ShowMessage("Impossible run this operation. Activuate request!");
exit(-1);
}
Application->CreateForm(__classid(TfrmToExcel), &frmToExcel);
frmToExcel->MainDataSet = _DataSet;
frmToExcel->ShowModal();
}
/*******************************************/
#include <vcl.h>
#include "Excel.h"
#include "ExportToExcel.h"
TfrmToExcel *frmToExcel;
__fastcall TfrmToExcelTfrmToExcel(TComponent* Owner)
TForm(Owner){}
void __fastcall TfrmToExcelBitBtn1Click(TObject *Sender)
{
BOOL CheckFields;
Variant ExcelLists, ExcelList, ListSheet;
int ARec, ACol, i;
TBookmark _BookMark;
_FileName = "";
ExcelObject = CreateOleObject("Excel.Application");
ExcelLists = ExcelObject.OlePropertyGet("Workbooks");
if(SaveDialog1->Execute())
{
_FileName = SaveDialog1->FileName;
if(!FileExists(_FileName))
{
int iFileHandle;
iFileHandle = FileCreate(_FileName);
if(!iFileHandle)
throw Exception("Cannot create file");
FileClose(iFileHandle);
}
}
else exit(0);
ExcelLists.Exec(Procedure("Open")<<_FileName);
ExcelList = ExcelLists.OlePropertyGet("item",1);
ListSheet= ExcelList.OlePropertyGet("WorkSheets",1);
_BookMark = MainDataSet->GetBookmark();
MainDataSet->DisableControls();
MainDataSet->First();
ACol = 0;
TStringList *list;
list = new TStringList;
MainDataSet->GetFieldNames(list);
for(int i=0; i<list->Count; i++)
{
setValue(0, ACol, list->Strings, ListSheet);
ACol++;
}
ARec = 1;
while (!MainDataSet->Eof)
{
ACol = 0;
for(int i=0; i<list->Count; i++)
if(!MainDataSet->FieldByName(list->Strings)->Value.IsNull())
try
{
setValue(ARec, ACol, MainDataSet->FieldByName(list->Strings)->Value, ListSheet);
}
catch(...){}
ACol++;
}
Application->ProcessMessages();
MainDataSet->Next();
ARec++;
}
MainDataSet->GotoBookmark(_BookMark);
MainDataSet->FreeBookmark(_BookMark);
MainDataSet->EnableControls();
ShowMessage("Export complete!");
ExcelObject.OlePropertySet("Visible", 1);
}
char* __fastcall TfrmToExceltoText(int row, int col)
{
static char cellText[256] ;
cellText[0] = 'A' + col ;
sprintf(&cellText[1],"%d",row+1);
return cellText;
}
void __fastcall TfrmToExcelsetValue(int row, int col, AnsiString as, Variant ListSheet)
{
Variant r = ListSheet.OlePropertyGet("Range", toText(row,col) );
r.OlePropertySet("Value", String(as));
}
Спасибо большое!