导出EXCEL的简单问题 ( 积分: 100 )

  • 主题发起人 主题发起人 funny_0415
  • 开始时间 开始时间
F

funny_0415

Unregistered / Unconfirmed
GUEST, unregistred user!
菜鸟问题,在全文检索中找了好久,都没找到想要答案,请大家帮忙解答
我做了一个EXCEL模板,导出EXCEL时,会调用模板
程序如下:
xlsfilename1:='F:/自制模板/A.xls';
ExcelApplication1 := TExcelApplication.Create(Application);
ExcelWorksheet1 := TExcelWorksheet.Create(Application);
ExcelWorkbook1 := TExcelWorkbook.Create(Application);
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=true;
ExcelApplication1.Workbooks.Add(xlsfilename1, 0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);

adoquery2.first;
j:=1;
while not adoquery2.Eof do
begin
j:=j+1;

ExcelWorksheet1.Cells.Item[j,1]:=trim(adoquery2.FieldByName('A').AsString);
ExcelWorksheet1.Cells.Item[j,2]:=trim(adoquery2.FieldByName('B).asstring);
ExcelWorksheet1.Cells.Item[j,3]:=trim(adoquery2.FieldByName('C).asstring);
ExcelWorksheet1.Cells.Item[j,4]:=trim(adoquery2.FieldByName('D').asstring);
ExcelWorksheet1.Cells.Item[j,5]:=trim(adoquery2.FieldByName('E').asstring);
ExcelWorksheet1.Cells.Item[j,6]:=trim(adoquery2.FieldByName('F').asstring);
ExcelWorksheet1.Cells.Item[j,11]:=trim(adoquery2.FieldByName('ww').asstring);

adoquery2.Next;
end;
IF FileExists('C:/A.xls') Then
DeleteFile('C:/A.xls');
excelworksheet1.Saveas('C:/A.xls');
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;

excelapp:=createoleobject('Excel.application');
excelapp.visible:=true;
workbook:=excelapp.workbooks.open('C:/A.xls');

问题1:
由于资料量很大,所以导出EXCEL时,会跑很长时间,
在这段时间,如果鼠标点击到EXCEL的资料,就会出错,
请问怎样才能在跑资料时将EXCEL隐藏,跑完后再将EXCEL打开?

问题2:
如果我在导出EXCEL时,另外还有其他的EXCEL文件处于打开状态,
也会报错
 
第一个问题: ExcelApplication1.Visible[0]:=false; 最后在数据完成时再ExcelApplication1.Visible[0]:=true;
 
给你一个正确的,你看一下吧.
with MyQty do
begin
try
if not IsEmpty then
begin
First;
DisableControls;
for i:= 1 to RecordCount do
begin
MyStrA := FieldByName('ORD_NO').AsString;
if (MyStrA = MyStrB) then //and (m <> 1)
begin
j := 8+l;
k := i+7;
MsExcelWorkSheet.Range['A'+IntToStr(j)+':'+'A'+IntToStr(k)].merge;
MsExcelWorkSheet.Range['B'+IntToStr(j)+':'+'B'+IntToStr(k)].merge;
MsExcelWorkSheet.Range['C'+IntToStr(j)+':'+'C'+IntToStr(k)].merge;
MsExcelWorkSheet.Range['D'+IntToStr(j)+':'+'D'+IntToStr(k)].merge;
MsExcelWorkSheet.Range['E'+IntToStr(j)+':'+'E'+IntToStr(k)].merge;

end else begin
l := i-1 ;
end;
MsExcelWorkSheet.range['A'+IntToStr(i+7)].value := FieldByName('CUS_NM').AsString; //BUYER
MsExcelWorkSheet.range['B'+IntToStr(i+7)].value := FieldByName('ORD_NO').AsString;
MsExcelWorkSheet.range['C'+IntToStr(i+7)].value := FieldByName('STY_NO').AsString; //Style No.
MsExcelWorkSheet.range['D'+IntToStr(i+7)].value := FieldByName('ORD_QT').AsString; //QTY;
MsExcelWorkSheet.range['E'+IntToStr(i+7)].value := FieldByName('FOB_PR').AsString; //FOB
MsExcelWorkSheet.range['F'+IntToStr(i+7)].value := FieldByName('DES_01').AsString; //DESCRIPTION
MsExcelWorkSheet.range['G'+IntToStr(i+7)].value := FieldByName('UNT_PU').AsString; //單位
MsExcelWorkSheet.range['H'+IntToStr(i+7)].value := FieldByName('MAK_NM').AsString; //廠商
MsExcelWorkSheet.range['I'+IntToStr(i+7)].value := FieldByName('SUG_PU').AsString; //建議量
MsExcelWorkSheet.range['J'+IntToStr(i+7)].value := FieldByName('UNT_PRTWO').AsString; //單價
MsExcelWorkSheet.range['K'+IntToStr(i+7)].value := FieldByName('SUG_MONEY').AsString; //金額
MsExcelWorkSheet.range['L'+IntToStr(i+7)].value := FieldByName('MAK_NM').AsString; //廠商
MsExcelWorkSheet.range['M'+IntToStr(i+7)].value := FieldByName('PUR_QT').AsString; //採購量
MsExcelWorkSheet.range['N'+IntToStr(i+7)].value := FieldByName('UNT_PR').AsString; //單價
MsExcelWorkSheet.range['O'+IntToStr(i+7)].value := FieldByName('QT_PT').AsString; //金額
MsExcelWorkSheet.range['P'+IntToStr(i+7)].value := FieldByName('OVR_RT').AsString+'%'; // 可超交(%)
MsExcelWorkSheet.range['Q'+IntToStr(i+7)].value := FieldByName('TRN_QT').AsString; //驗收量

MsExcelWorkSheet.range['R'+IntToStr(i+7)].value := FieldByName('UNT_CK').AsString; //單價

MsExcelWorkSheet.range['S'+IntToStr(i+7)].value := FieldByName('C_QT_PT').AsString; //金額

MsExcelWorkSheet.range['T'+IntToStr(i+7)].value := FieldByName('SUM_QT').AsString; //結帳量
MsExcelWorkSheet.range['U'+IntToStr(i+7)].value := FieldByName('UNT_KJ').AsString; //單價
MsExcelWorkSheet.range['V'+IntToStr(i+7)].value := FieldByName('PR_RUND').AsString; //金額
MsExcelWorkSheet.range['W'+IntToStr(i+7)].value := '';
m :=10;
MyStrB:= FieldByName('ORD_NO').AsString;
Next;
end;
//Add Total --20060912
MsExcelWorkSheet.range['I'+IntToStr(i+7)].value := '=sum(I8:I'+IntToStr(i+6); // PRE-FUNCTION ?某秖
MsExcelWorkSheet.range['K'+IntToStr(i+7)].value := '=sum(K8:K'+IntToStr(i+6); // PRE-FUNCTION ?肂
MsExcelWorkSheet.range['M'+IntToStr(i+7)].value := '=sum(M8:M'+IntToStr(i+6); // 蹦潦 蹦潦秖
MsExcelWorkSheet.range['O'+IntToStr(i+7)].value := '=sum(O8:O'+IntToStr(i+6); // 蹦潦 ?肂
MsExcelWorkSheet.range['Q'+IntToStr(i+7)].value := '=sum(Q8:Q'+IntToStr(i+6); // ??喷Μ 喷Μ秖
MsExcelWorkSheet.range['S'+IntToStr(i+7)].value := '=sum(S8:S'+IntToStr(i+6); // ??喷Μ ?肂
MsExcelWorkSheet.range['T'+IntToStr(i+7)].value := '=sum(T8:T'+IntToStr(i+6); // 穦璸挡眀 挡眀秖
MsExcelWorkSheet.range['V'+IntToStr(i+7)].value := '=sum(V8:V'+IntToStr(i+6); // 穦璸挡眀 ?肂

//Add Total --20060912
EnableControls;
end;
finally

MsExcel.ActiveSheet.Range['A8:W'+IntToStr(RecordCount+7)].Borders[1].Weight := 1;
MsExcel.ActiveSheet.Range['A8:W'+IntToStr(RecordCount+7)].Borders[2].Weight := 1;
MsExcel.ActiveSheet.Range['A8:W'+IntToStr(RecordCount+7)].Borders[3].Weight := 1;
MsExcel.ActiveSheet.Range['A8:W'+IntToStr(RecordCount+7)].Borders[4].Weight := 1;
MsExcel.ActiveSheet.Range['A8:A'+IntToStr(RecordCount+7)].Borders[1].Weight := 3;
MsExcel.ActiveSheet.Range['G8:G'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;
MsExcel.ActiveSheet.Range['K8:K'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;
MsExcel.ActiveSheet.Range['P8:P'+IntToStr(RecordCount+7)].Borders[1].Weight := 3;
MsExcel.ActiveSheet.Range['P8:P'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;


MsExcel.ActiveSheet.Range['S8:S'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;
MsExcel.ActiveSheet.Range['V8:V'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;
MsExcel.ActiveSheet.Range['W8:W'+IntToStr(RecordCount+7)].Borders[2].Weight := 3;
MsExcel.ActiveSheet.Range['A'+IntToStr(RecordCount+7)+':'+'W'+IntToStr(RecordCount+7)].Borders[4].Weight := 3;


end;
Free;
end;
 
若将ExcelApplication1.Visible[0]:=false
那么导出的时候会出错“被呼叫方拒绝接受呼叫”
 
数据库的数据生成字段与字段用#9的StringList,
把StringList的内容Paste到Excel中
stemp:='';
while not eof do
begin
for i:=0 to Fields.count-1 do
begin
stemp:=stemp+fileds.field.asstring+#9;
end;
stringList.add(stemp);
stemp:='';
Next;
end;
Clipbroad.AsText:=stringList.Text;
Excel.Paste;
 

Similar threads

I
回复
0
查看
847
import
I
I
回复
0
查看
610
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
654
import
I
后退
顶部