D6中,如何将Access中的数据导出为Excel文件,不管机器中是否安装了Office?
解决方案 »
- 发布数据库维护工具,纯绿色,功能直逼pl/sql
- DXDBGRID的使用问题
- 菜鸟的问题?怎样配置网络才能使我的isapi dll得到运行呀?
- [菜鸟请教]在输入时间的时候一个奇怪的问题
- 关于过程调用
- 请教TreeView的ToolTips的用法
- 如何使程序不出现在任务列表中?
- 用Delphi作ActiveX时调用SaveDialogh或是其他Dialog返回文件名为乱码,在线等待
- ActionManager 加载问题。。。
- 如何设置打印机的映射模式,setMapMode(printer.handle,MM_TEXT)为什么出错
- infoPower中的wwDBRichEdit的bug怎么解决,在线等....
- Corba连接的问题
导为EXCEL文件而不管是否安装EXCEL。
然后看程序:procedure DataToExcel(Grid:TDbGrid;DataSet:TDataset;Title:String;sExcelFile:String);
var
i,j,Row:integer;
WB: _WorkBook;
WBs: Workbooks;
FExcelWasFound:Boolean;
ExcelFile:string;
FileHandle: integer;
irange:range;
iWidth:integer;
//oFont:olevariant;
begin
try
Screen.Cursor :=crHourGlass ;
{for i:=0 to Grid.Columns.Count -1 do
begin
Dataset.Fields[i].DisplayWidth :=Grid.Columns[i].Width;
end; }
ExcelFile:=sExcelFile;
if not fileExists(ExcelFile) then
begin
FileHandle:=FileCreate(ExcelFile);
Fileclose(FileHandle);
end;
FExcelWasFound := True;
try
FApp := CreateOleObject('Excel.Application.9') as _Application; //调用Excel2000
except
FExcelWasFound := False;
end;
if not FExcelWasFound then //如果不存在,则调用Excel97
try
FApp := CreateOleObject('Excel.Application.8') as _Application;
FExcelWasFound := True;
except
FExcelWasFound := False;
ShowMessage('Excel调用失败!');
end;
if FExcelWasFound then
begin
InitVariables;
New(FSPms);
with FApp ,FSPms^ do
begin
App_SheetsInNewWorkbook := Get_SheetsInNewWorkbook(0);
App_DisplayFormulaBar := Get_DisplayFormulaBar(0);
App_ReferenceStyle := Get_ReferenceStyle(0);
App_DisplayStatusBar := Get_DisplayStatusBar(0);
Set_SheetsInNewWorkbook(0, 1);
WBs := Get_Workbooks; //打开Excel文件
WB := WBs.Open(excelFile, 3, false, 1,
'', '', True, $00000002, 1, False,
False, Null, False, 0);
MakeVBScript(WB); //初始化文件属性
end;
with FApp do
begin
Set_DisplayFormulaBar(0, False);
Set_ReferenceStyle(0, Integer(xlR1C1));
Set_DisplayStatusBar(0, False);
Set_Caption(Title);
end;
try
Row:=1;
irange:=Fapp.ActiveCell ;
irange.Font.Size :=9;
for j:=0 to Grid.FieldCount -1 do
begin
if Grid.Columns[j].Visible =true then
begin
if DataSet.Fields[j].displaywidth>254 then
iRange.ColumnWidth:=100
else
begin
//iWidth:=Grid.Columns[j].Width;
iRange.ColumnWidth :=Grid.Columns[j].Field.DisplayWidth ;
end;
irange.Font.Size :=9; //ljq 2001/03/09
irange.value:=Grid.Columns[j].Title.Caption ;
irange:=irange.Next;
end;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
Row:=Row+2;
DataSet.DisableControls;
DataSet.First;
FApp.Get_ActiveWindow.DisplayZeros := True;
irange.NumberFormat:=10;
for i:=0 to DataSet.RecordCount -1 do
begin
irange:=Fapp.Range['A'+IntToStr(Row),'A'+intToStr(Row)];
for j:=0 to Grid.FieldCount -1 Do
begin
if Grid.Columns[j].Visible =True then
begin
if Grid<>nil then
begin
iRange.Font.Size :=Grid.Font.Size;
iRange.Font.Name :=Grid.Font.Name;
end
else
begin
irange.Font.Size :=FFontSize;
irange.Font.Name :=FFontName;
end; //edit by ljq 2001/03/09
iRange.Value :=Grid.Columns[j].Field.AsString ;
irange:=iRange.Next ;
end;
end;
DataSet.next;
Row:=Row+1;
end;
Screen.Cursor :=crDefault ;
DataSet.EnableControls;
irange:=FApp.Range['A1','K'+intToStr(Row-1)];
FApp.Set_Visible(0,True);
CreateToolBar(False); //屏蔽Excel的系统菜单,采用自定义菜单实现
end else
begin
ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+' 如果未安装,请先安装office');
Screen.Cursor :=crDefault ;
end;
except
ShowMessage('调用Excel出错!');
fApp._Release;
Screen.Cursor :=crDefault ;
exit;
end;
end;
procedure TrepForm.doit;
var
i,j,k1,k2,kkkk:integer;
memolist:tstringlist;
s_dj,s_zj:string;
tot:real;
begin
tot:=0;
try
MsExcel:=CreateOleObject('Excel.Application');
MsExcelWorkBook:=MsExcel.WorkBooks.Add;
MsExcelWorkSheet:=MsExcel.Sheets['Sheet1'];
except
showerror('无法打开 EXCEL 97');
exit;
end;
memolist:=tstringlist.Create;
MsExcel.Visible:=True;
MsExcelWorkSheet.Range['A2:E2'].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=3;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A2:E2'].value:='采购合同';
kkkk:=4;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='合同号码:'+maintable.fieldbyname('ht_id').asstring;
memolist.assign(maintable.fieldbyname('bftt'));
if memolist.count=0 then
memolist.add(maintable.fieldbyname('wfgs').asstring);
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='买方:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].value:=kong(7)+memolist.strings[i];
end;
kkkk:=kkkk+memolist.count;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='合同日期:'+maintable.fieldbyname('dateis').asstring;
memolist.assign(maintable.fieldbyname('ttr'));
if memolist.count=0 then
memolist.add(maintable.fieldbyname('supplier').asstring);
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='卖方:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':B'+inttostr(kkkk+i)].value:=kong(8)+memolist.strings[i];
end;
kkkk:=kkkk+memolist.count;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':B'+inttostr(kkkk)].value:='签约地点:'+maintable.fieldbyname('qddd').asstring;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='交货日期:'+maintable.fieldbyname('jhdate').asstring;
kkkk:=kkkk+2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='一、品名、规格、数量、金额:';
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)].Value:='货号';
MsExcelWorkSheet.Range['A'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=15;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk)].Value:='品质说明';
MsExcelWorkSheet.Range['B'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=30;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk)].Value:='数量';
MsExcelWorkSheet.Range['C'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=10;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk)].Value:='单价';
MsExcelWorkSheet.Range['D'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=12;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk)].Value:='总价';
MsExcelWorkSheet.Range['E'+inttostr(kkkk)].select;
MsExcel.selection.columnwidth:=10;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':'+'E'+inttostr(kkkk)].select;
msexcel.selection.Borders[7].LineStyle:=1;//left
msexcel.selection.Borders[8].LineStyle:=1;//top
msexcel.selection.Borders[9].LineStyle:=1;//bottom
msexcel.selection.Borders[10].LineStyle:=1;//right
msexcel.selection.Borders[11].LineStyle:=1;//内部垂直
if qtsql.active then
qtsql.close;
qtsql.sql.clear;
qtsql.sql.Add('select * from nxhtsheet99');
qtsql.sql.Add('where ht_id='+''''+maintable.fieldbyname('ht_id').asstring+'''');
qtsql.open;
qtsql.first;
j:=1;
while qtsql.eof=false do
begin
memolist.Assign(qtsql.fieldbyname('shm'));
if memolist.count=0 then
memolist.add('');
s_dj:=maintable.fieldbyname('hbdm').asstring+qtsql.fieldbyname('unite_price').asstring+'/'+qtsql.fieldbyname('dw1').asstring;
s_zj:=maintable.fieldbyname('hbdm').asstring+format('%.2f',[qtsql.fieldbyname('unite_price').asfloat*qtsql.fieldbyname('pcs').asfloat]);
tot:=tot+qtsql.fieldbyname('unite_price').asfloat*qtsql.fieldbyname('pcs').asfloat;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].Value:=qtsql.fieldbyname('product_id').asstring;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].Value:=memolist.strings[0];
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].Value:=qtsql.fieldbyname('pcs').asstring+qtsql.fieldbyname('dw1').asstring;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].Value:=s_dj;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].Value:=s_zj;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
k1:=j;
for i:=1 to (memolist.count-1) do
begin
j:=j+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['A'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].Value:=memolist.strings[i];
MsExcelWorkSheet.Range['B'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['C'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['D'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].Value:='';
MsExcelWorkSheet.Range['E'+inttostr(kkkk+j)].select;
MsExcel.Selection.HorizontalAlignment:=2;
end;
k2:=j;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+k1)+':A'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
// MsExcelWorkSheet.Range['B'+inttostr(kkkk+k1)+':B'+inttostr(kkkk+k2)].select;
// MsExcel.Selection.merge;
MsExcelWorkSheet.Range['C'+inttostr(kkkk+k1)+':C'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['D'+inttostr(kkkk+k1)+':D'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['E'+inttostr(kkkk+k1)+':E'+inttostr(kkkk+k2)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+k1)+':E'+inttostr(kkkk+k2)].select;
msexcel.selection.Borders[7].LineStyle:=1;//left
msexcel.selection.Borders[8].LineStyle:=1;//top
msexcel.selection.Borders[9].LineStyle:=1;//bottom
msexcel.selection.Borders[10].LineStyle:=1;//right
msexcel.selection.Borders[11].LineStyle:=1;//内部垂直
j:=j+1;
qtsql.next;
end;
kkkk:=kkkk+j;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':D'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=4;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':D'+inttostr(kkkk)].value:='金额合计:';
MsExcelWorkSheet.Range['E'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['E'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:=maintable.fieldbyname('hbdm').asstring+format('%.2f',[tot]);
kkkk:=kkkk+2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='二、质量技术标准:'+maintable.fieldbyname('zlyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='三、交货地点:'+maintable.fieldbyname('fhyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='四、包装要求:'+maintable.fieldbyname('bzyq').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='五、质量检验:'+maintable.fieldbyname('jfcl').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='六、结算方式:'+maintable.fieldbyname('jsfs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='七、运输要求:'+maintable.fieldbyname('ysfs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='八、损耗计算:'+maintable.fieldbyname('shjs').asstring;
kkkk:=kkkk+1;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='九、配件供应:'+maintable.fieldbyname('pjgy').asstring;
memolist.assign(maintable.fieldbyname('memo'));
if memolist.count=0 then
memolist.add('');
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk)+':E'+inttostr(kkkk)].value:='十、合同备注:'+memolist.strings[0];
for i:=1 to (memolist.count-1) do
begin
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':E'+inttostr(kkkk+i)].select;
MsExcel.Selection.merge;
MsExcel.Selection.HorizontalAlignment:=2;
MsExcel.Selection.VerticalAlignment:=2;
MsExcelWorkSheet.Range['A'+inttostr(kkkk+i)+':E'+inttostr(kkkk+i)].value:=kong(19)+memolist.strings[i];
end;
memolist.free;
end;