delphi调用EXCEl打印报表 ( 积分: 200 )

  • 主题发起人 主题发起人 shp1997
  • 开始时间 开始时间
S

shp1997

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在做一程序,需要用到EXCEl,已在工作目录下预先生成EXCEL文件,只要把表单中的内容输出至EXECl文件中,现有两个问题,1、不知如何使用EXCEL输出报表 2、报表中有一个操作步骤列表可能会超过一页,如何分成两页
 
我做过把dbgrid里的数据导到excel中,不过现在找不到源码.
下面是在asp中把数据导到excel中,你看看有没有参考价值
只要把这些代码保存在文本文件中,后缀改为.htm,然后双击运行,就会打开excel程序
往里面灌数据.(前提是你的机器装了excel)

<html>
<body>
<script language=&quot;vbscript&quot;>
Dim xlApp, xlBook, xlSheet1
Dim mystr,nowrow
on error resume next
Set xlApp = CreateObject(&quot;Excel.Application&quot;)
if xlApp=Empty then
window.open &quot;help.asp&quot;,&quot;ddd&quot;,&quot;width=400,HEIGHT=510,screenY=20,scrolly=true&quot;
end if
Set xlBook = xlApp.Workbooks.Add
Set xlSheet1 = xlBook.Worksheets(1)
xlSheet1.Application.Visible = True
With xlBook.Styles(&quot;Normal&quot;).Font
.Name = &quot;宋体&quot;
.Size = 12
.Bold = False
.Italic = False
.Strikethrough = False
.ColorIndex = 1
End With
xlBook.Styles(&quot;Normal&quot;).HorizontalAlignment = 2
xlApp.Selection.Style = &quot;Normal&quot;

xlSheet1.Range(&quot;A1:Q1&quot;).Merge
xlSheet1.cells(1,1).value =&quot;演示报表&quot;
xlApp.Rows(&quot;1:1&quot;).RowHeight = 85
xlSheet1.cells(1,1).font.name=&quot;隶书&quot;
xlSheet1.cells(1,1).font.size=25
xlSheet1.cells(1,1).HorizontalAlignment=3

xlSheet1.Range(&quot;A2:Q2&quot;).Merge
xlSheet1.cells(2,1).value =&quot;库存商品明细表&quot;
xlApp.Rows(&quot;2:2&quot;).RowHeight = 24
xlSheet1.cells(2,1).font.name=&quot;隶书&quot;
xlSheet1.cells(2,1).font.size=25
xlSheet1.cells(2,1).HorizontalAlignment=3
nowrow=2

mystr=&quot;A&quot;
&amp;
nowrow &amp;
&quot;:Q&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
mystr=nowrow &amp;
&quot;:&quot;
&amp;
nowrow
xlApp.Rows(mystr).RowHeight = 40
nowrow = nowrow+1
mystr=&quot;A&quot;
&amp;
nowrow &amp;
&quot;:C&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,1).value =&quot;货主:&quot;
&amp;
&quot;abc&quot;
xlSheet1.cells(nowrow,1).HorizontalAlignment=2

mystr=&quot;D&quot;
&amp;
nowrow &amp;
&quot;:H&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,4).value =&quot;仓间:&quot;
&amp;
&quot;asd&quot;
xlSheet1.cells(nowrow,4).HorizontalAlignment=2

mystr=&quot;L&quot;
&amp;
nowrow &amp;
&quot;:N&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,12).value =&quot;起始日期:&quot;
&amp;
&quot;sdf&quot;
xlSheet1.cells(nowrow,12).HorizontalAlignment=4

mystr=&quot;O&quot;
&amp;
nowrow &amp;
&quot;:Q&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,15).value =&quot;截止日期:&quot;
&amp;
&quot;dcv&quot;
xlSheet1.cells(nowrow,15).HorizontalAlignment=4
nowrow = nowrow+1

mystr=&quot;A&quot;
&amp;
nowrow &amp;
&quot;:A&quot;
&amp;
nowrow+1
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,1).value =&quot;入出库单号&quot;
mystr=&quot;B&quot;
&amp;
nowrow &amp;
&quot;:B&quot;
&amp;
nowrow+1
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,2).value =&quot;原始凭证号&quot;
mystr=&quot;C&quot;
&amp;
nowrow &amp;
&quot;:C&quot;
&amp;
nowrow+1
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,3).value =&quot;送提货单位&quot;
mystr=&quot;D&quot;
&amp;
nowrow &amp;
&quot;:D&quot;
&amp;
nowrow+1
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,4).value =&quot;库房&quot;
mystr=&quot;E&quot;
&amp;
nowrow &amp;
&quot;:E&quot;
&amp;
nowrow+1
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,5).value =&quot;库位&quot;
mystr=&quot;F&quot;
&amp;
nowrow &amp;
&quot;:H&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,6).value =&quot;上月结存&quot;
xlSheet1.cells(nowrow+1,6).value =&quot;件数&quot;
xlSheet1.cells(nowrow+1,7).value =&quot;数量&quot;
xlSheet1.cells(nowrow+1,8).value =&quot;层级数量&quot;

mystr=&quot;I&quot;
&amp;
nowrow &amp;
&quot;:K&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,9).value =&quot;本月进仓&quot;
xlSheet1.cells(nowrow+1,9).value =&quot;件数&quot;
xlSheet1.cells(nowrow+1,10).value =&quot;数量&quot;
xlSheet1.cells(nowrow+1,11).value =&quot;层级数量&quot;

mystr=&quot;L&quot;
&amp;
nowrow &amp;
&quot;:N&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,12).value =&quot;本月出仓&quot;
xlSheet1.cells(nowrow+1,12).value =&quot;件数&quot;
xlSheet1.cells(nowrow+1,13).value =&quot;数量&quot;
xlSheet1.cells(nowrow+1,14).value =&quot;层级数量&quot;

mystr=&quot;O&quot;
&amp;
nowrow &amp;
&quot;:Q&quot;
&amp;
nowrow
xlSheet1.Range(mystr).Merge
xlSheet1.cells(nowrow,15).value =&quot;本月结存&quot;
xlSheet1.cells(nowrow+1,15).value =&quot;件数&quot;
xlSheet1.cells(nowrow+1,16).value =&quot;数量&quot;
xlSheet1.cells(nowrow+1,17).value =&quot;层级数量&quot;

mystr=nowrow &amp;
&quot;:&quot;
&amp;
nowrow+1
xlSheet1.Rows(mystr).HorizontalAlignment = 3
xlSheet1.Rows(mystr).VerticalAlignment = 2

nowrow = nowrow+2
mystr=&quot;A&quot;
&amp;
nowrow &amp;
&quot;:Q&quot;
&amp;
nowrow
xlSheet1.range(mystr).value=array(&quot;a1&quot;,&quot;a2&quot;,&quot;a3&quot;,&quot;a4&quot;,&quot;a5&quot;,&quot;a6&quot;,&quot;a7&quot;,&quot;a8&quot;,&quot;a9&quot;,&quot;a10&quot;,&quot;a11&quot;,&quot;a12&quot;,&quot;a13&quot;,&quot;a14&quot;,&quot;a15&quot;,&quot;a16&quot;,&quot;a17&quot;)
nowrow = nowrow+1


xlSheet1.cells(nowrow,1).value =&quot;合计&quot;

xlSheet1.cells(nowrow,6).value =11
xlSheet1.cells(nowrow,7).value =22
xlSheet1.cells(nowrow,8).value =33
xlSheet1.cells(nowrow,9).value =44
xlSheet1.cells(nowrow,10).value =55
xlSheet1.cells(nowrow,11).value =66
xlSheet1.cells(nowrow,12).value =77
xlSheet1.cells(nowrow,13).value =88
xlSheet1.cells(nowrow,14).value =99
xlSheet1.cells(nowrow,15).value =111
xlSheet1.cells(nowrow,16).value =222
xlSheet1.cells(nowrow,17).value =333

xlSheet1.Columns(&quot;A:Q&quot;).EntireColumn.AutoFit
</script>
asdf
</body>
</html>
 
學習學習。。。。。
 
原来数据可输入excel,就是无法实现预览功能
 
我自己做了个把数据集中值导入到Excel中的工具,以Excel文件为模板,按照格式添数据,可填充主从表数据集。。。似乎挺适合你的要求的。。。
用模板的好处是可以打开Excel来设置格式,导出来的格式跟你设置好的一样。。。不过速度一般,没有直接处理文件的那种快。。。
代码比较长。。。有需要说一声。。。
 
var
ExpClass:TDBGridEhExportClass;
Ext:String;
s_name:string;
begin
SaveDialog1.FileName := '*****';
// if (ActiveControl is TDBGridEh) then
if SaveDialog1.Execute then
begin
case SaveDialog1.FilterIndex of
1: begin
ExpClass := TDBGridEhExportAsxls;
Ext := 'xls';
end;
else
ExpClass := nil;
Ext := '';
end;

if ExpClass <> nil then
begin
if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <> UpperCase(Ext) then

SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;
s_name:= extractfilename(SaveDialog1.FileName);
SaveDBGridEhToExportFile(ExpClass,DBGridEh5,SaveDialog1.FileName,true);
end;
ShellExecute(0,'open','EXCEL.EXE',pchar(s_name),nil,SW_SHOW);
end;
这个或许对你有用
 
下面代码 我测试过了 没有错误.实现了打印预览功能,
记得加COMOBJ单元

procedure TForm1.Button1Click(Sender: TObject);
var
v:variant;
Cell: Variant;
begin
v:=CreateOleobject('Excel.Application');
// 运行EXCEL
v.WorkBooks.Open( 'C:/MSF.xls' );
// 设置EXCEL中的WORKBOOKS
Cell := v.WorkSheets[1].Cells[3, 1];
Cell.Value := 'test';
Cell.wraptext:=true;
Cell.borders.linestyle:=1;
//加边框线
Cell.HorizontalAlignment:=3;
}
v.visible:=true;
//居中对齐
v.ActiveSheet.PrintPreview;
v.quit;
end;
 
问题已解决,谢谢大家
 
后退
顶部