본문 바로가기
Borland

Excel의 셀에 이미지 삽입 방법

by leo21c 2010. 1. 19.
아래 두 함수를 한번 살표 보세요

ExcelInsertImage
SaveBitmapToExcel


// Variant V;
Variant excel_app;
Variant excel_window;
Variant excel_book;
Variant my_worksheet;

//----------------------------------------------------------------------------
void __fastcall TDLG_RPT_EXCEL::ExcelOpen()
{
excel_app=Variant::CreateObject("excel.application");
excel_app.OlePropertySet("Visible", (Variant)false);
}
//---------------------------------------------------------------------------
void __fastcall TDLG_RPT_EXCEL::ExcelExit()
{
excel_app.OleFunction("Quit");
excel_app = Unassigned;
}
//---------------------------------------------------------------------------

//excel파일을 읽어온다.
void __fastcall TDLG_RPT_EXCEL::ExcelReportFileOpen(AnsiString asFileName)
{
excel_app.OlePropertyGet("WorkBooks").OleProcedure("Open",asFileName.c_str());
excel_window = excel_app.OlePropertyGet("ActiveWindow");
excel_book=excel_app.OlePropertyGet("ActiveWorkbook");
my_worksheet=excel_book.OlePropertyGet("ActiveSheet");

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

// image파일을 특정sheet의 특정 cell에 insert 하는 코드

Variant __fastcall TDLG_RPT_EXCEL::ExcelInsertImage(AnsiString sh_name, int iRow, int iCol, AnsiString iFileName)
{
Variant cells;
my_worksheet = excel_book.OlePropertyGet("WorkSheets", sh_name.c_str());
my_worksheet.OleFunction("Select");
cells = my_worksheet.OlePropertyGet("Cells", iRow, iCol);
cells.OleFunction("Select");
Variant c_image;
c_image = my_worksheet.OlePropertyGet("Pictures").OleFunction("Insert", WideString(iFileName));
c_image.OleFunction("Select");
return c_image;
}
//---------------------------------------------------------------------------

//Bitmap을 특정 Sheet 특정 Cell에 Paste하는 코드
//클립보드를 이용함

void __fastcall TDLG_RPT_EXCEL::SaveBitmapToExcel(Graphics::TBitmap *bmp,String sSheetName,int iRow,int iCol)
{
if(bmp)
{
Clipboard()->Assign(bmp);
my_worksheet = excel_book.OlePropertyGet("WorkSheets", WideString(sSheetName));
my_worksheet.OlePropertyGet("Cells", iRow, iCol).OleProcedure("Select");
my_worksheet.OleProcedure("Paste");
}
}
//---------------------------------------------------------------------------