2010年3月31日 星期三

EXCEL插入圖檔並指定圖檔大小

EXCEL插入圖檔並指定圖檔大小
Use Variants, COMOBJ;

var MyExcel: Variant;
aQA,aGONO1,aCUNO,aCLR,Exa:string [18];
x,y, Z,iSheets,iCOL:integer;
ExcelSheet: Variant;
begin
Exa:='ABCDEFGHIJKLMNOPQRS';
Z:=dbgrid.FieldCount;
deletefile(ExcelFileName);
MyExcel := CreateOleOBject('Excel.Application');
MyExcel.WorkBooks.Add;
MyExcel.Visible := True;
MyExcel.WorkBooks[1].Saveas(ExcelFileName);
dbgrid.DataSource.DataSet.First;
MyExcel.Range['A1:J1'].Select;//選取一整行
MyExcel.Selection.Merge;

iSheets:=1;//切換業籤用
MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells[1,1] := ''''+ GetRefLookupValue(cdsData, 'QA', 'QANM');
//設定sheet名稱
MyExcel.WorkBooks[1].WorkSheets[iSheets].Name :=GetRefLookupValue(cdsData, 'QA', 'QANM');
y:=2;
for x:=1 to dbgrid.FieldCount do
begin
//MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x] := dbgrid.Columns[x-1].title.caption;
// 將該欄設為標選
//MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x].Select;
// 將標題欄位變粗體字
//MyExcel.Selection.Font.Bold := true;
// 設定欄位寬度
if dbgrid.Fields[x-1].FieldName = 'PICNAM' then
begin
MyExcel.WorkBooks[1].WorkSheets[iSheets].Columns[x].ColumnWidth := 15;
end
else
MyExcel.WorkBooks[1].WorkSheets[iSheets].Columns[x].ColumnWidth := dbgrid.Fields[x-1].DisplayWidth;
end;

//inc(y);
aQA:=dbgrid.DataSource.DataSet.FieldByName('QA').AsString;

while not dbgrid.DataSource.DataSet.eof do
begin
//品牌不同時換頁
if aQA<>dbgrid.DataSource.DataSet.FieldByName('QA').AsString then
begin
MyExcel.WorkSheets.Add;
MyExcel.WorkSheets[iSheets].Activate;
//設定sheet名稱
MyExcel.WorkBooks[1].WorkSheets[iSheets].Name :=GetRefLookupValue(cdsData, 'QA', 'QANM');
MyExcel.Range['A1:J1'].Select;//選取一整行
MyExcel.Selection.Merge;
MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells[1,1] := ''''+GetRefLookupValue(cdsData, 'QA', 'QANM');
for x:=1 to dbgrid.FieldCount do
begin
// 設定欄位寬度
if dbgrid.Fields[x-1].FieldName = 'PICNAM' then
MyExcel.WorkBooks[1].WorkSheets[iSheets].Columns[x].ColumnWidth := 15
else
MyExcel.WorkBooks[1].WorkSheets[iSheets].Columns[x].ColumnWidth := dbgrid.Fields[x-1].DisplayWidth;
end;
y:=2;
end;

MyExcel.WorkBooks[1].WorkSheets[iSheets].Rows[y].RowHeight:=50;
MyExcel.WorkBooks[1].WorkSheets[iSheets].Columns[x].Autofit;
iCOL:=2; //明細資料從第2個欄位開始丟,第一ㄍ欄位丟圖檔
for x:=1 to dbgrid.FieldCount do
begin
if dbgrid.Fields[x-1].FieldName = 'PICNAM' then
begin
if FileExists(Pchar(dbgrid.Fields[x-1].AsString)) then
begin
//插入圖檔
//Note:1.圖檔會自動等比例縮放,所以先只設定寬度即可,否則圖檔會用最小像素顯示
//2.只適用EXCEL2003以下版本
//aPIC:= MyExcel.WorkBooks[1].WorkSheets[iSheets].Pictures.Insert(dbgrid.DataSource.DataSet.FieldByName('PICNAM').Value);
//aPIC.ShapeRange.IncrementLeft(0);
//aPIC.ShapeRange.IncrementTop(0);
//aPIC.ShapeRange.LockAspectRatio := msoFalse; //圖檔大小LOCK
//aPIC.ShapeRange.height:=200;
//aPIC.ShapeRange.width:=110;
//取消底色
//aPIC.ShapeRange.PictureFormat.TransparentBackground := 1;
//aPIC.ShapeRange.PictureFormat.TransparencyColor := RGB(255, 255, 255);
//aPIC.ShapeRange.Fill.Visible := 0;

MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells[y,1].Select;
MyExcel.WorkBooks[1].WorkSheets[iSheets].Pictures.Insert(dbgrid.Fields[x-1].Value);
//設定圖檔大小
MyExcel.WorkBooks[1].WorkSheets[iSheets].Pictures[1].ShapeRange.height:=40;
MyExcel.WorkBooks[1].WorkSheets[iSheets].Pictures[1].ShapeRange.width:=40;
end;
end
else
begin
if dbgrid.Fields[x-1].DataType =ftString then
begin
MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells[y,iCOL] := ''''+dbgrid.Fields[x-1].DisplayText;
end
else
MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells[y,iCOL] := dbgrid.Fields[x-1].DisplayText;
Inc(iCOL);
end;
end;
inc(y);

MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells.Select;
MyExcel.Selection.Columns.AutoFit;
dbgrid.DataSource.DataSet.next;
end;
//MyExcel.WorkBooks[1].WorkSheets[iSheets].Cells.Select;
//MyExcel.Selection.Columns.AutoFit;

//合計:
MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,4] := '合計:';
for x:=5 to 9 do
begin
MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x] := '=SUM('+Exa[x]+'3:'+Exa[x]+inttostr(y-1)+')';
MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x].Select;
MyExcel.Selection.Font.color:=clRed;
end;
MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,10] := '=H'+Inttostr(y)+'*100/G'+Inttostr(y);
MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,10].Select;
MyExcel.Selection.Font.color:=clRed;
end;

沒有留言: