我给你一段代码,是格式化输出到excel
MyExcel:=CreateOleObject('Excel.Application');
Flag:=true;
with DataM.DataModule1do
begin
if CheckBox2.Checked then
cx.First;
//需要打印全部数据...
repeat//循环打印....
// MyExcel.Visible := true;
//????????????///////////
MyWorkbook:=MyExcel.Workbooks.Add;
MyExcel.ActiveSheet.PageSetup.TopMargin:=1.5/0.035;
MyExcel.ActiveSheet.PageSetup.BottomMargin:=1.5/0.035;
MyExcel.ActiveSheet.PageSetup.LeftMargin:=0.2/0.035;
MyExcel.ActiveSheet.PageSetup.RightMargin:=0.2/0.035;
// MyExcel.ActiveSheet.PageSetup.Orientation := 'xlLandscape';//横向打印。。
MyExcel.Cells[2,6].Value:='单据号:'+Trim(cx.FieldValues['单号'])+'('+Trim(cx.FieldValues['备注'])+')';
MyExcel.Cells[2,9].Value:=' '+FormatDateTime('yyyy',Now)+'年'+FormatDateTime('mm',Now)+'月'+FormatDateTime('dd',Now)+'日 保管工:'+cx.FieldValues['保管工'];
MyExcel.Cells[2,1].Value:='领料单位: '+Trim(cx.FieldValues['领料单位'])+' 收料地点:'+Trim(cx.FieldValues['收料地点']);
//标题...
MyExcel.Cells[1,3].Value:=' 车 间 结 算 料 表';
//标题...
MyExcel.ActiveSheet.Range['a1
50'].Font.size:=10;
//设置全部字体大小为10
MyExcel.Cells[1,3].Font.size:=16;
//设置标题大小为20
MyExcel.Cells[1,3].Font.bold:=true;
//设置标题为粗体
MyExcel.ActiveSheet.Rows[3].Font.bold:=true;
//设置字体为粗体
MyExcel.ActiveSheet.Rows[3].HorizontalAlignment:=1;
//设置字体对中.
//设置列宽.
MyExcel.ActiveSheet.Columns[1].ColumnWidth:=3;
MyExcel.ActiveSheet.Columns[2].ColumnWidth:=7;
MyExcel.ActiveSheet.Columns[3].ColumnWidth:=18;
MyExcel.ActiveSheet.Columns[4].ColumnWidth:=8;
MyExcel.ActiveSheet.Columns[5].ColumnWidth:=22;
MyExcel.ActiveSheet.Columns[6].ColumnWidth:=4;
MyExcel.ActiveSheet.Columns[7].ColumnWidth:=10;
MyExcel.ActiveSheet.Columns[8].ColumnWidth:=7;
MyExcel.ActiveSheet.Columns[9].ColumnWidth:=7;
MyExcel.ActiveSheet.Columns[10].ColumnWidth:=11;
MyExcel.ActiveSheet.Columns[11].ColumnWidth:=11;
MyExcel.ActiveSheet.Columns[12].ColumnWidth:=12;
rowno:=2;
//从第3行开始
//处理标题...
inc(rowno);
// for j:=0 to 6do
MyExcel.Cells[rowno,j+1].Value:=stringgrid1.Cells[j,0];
MyExcel.Cells[rowno,1].Value:='序号';
MyExcel.Cells[rowno,2].Value:='目录编号';
MyExcel.Cells[rowno,3].Value:='材料名称';
MyExcel.Cells[rowno,4].Value:=' 材 质';
MyExcel.Cells[rowno,5].Value:='规格型号';
MyExcel.Cells[rowno,6].Value:='计量单位';
MyExcel.Cells[rowno,7].Value:='单价';
MyExcel.Cells[rowno,8].Value:='请领数量';
MyExcel.Cells[rowno,9].Value:='实收数量';
MyExcel.Cells[rowno,10].Value:='工作号';
MyExcel.Cells[rowno,11].Value:='总价';
MyExcel.Cells[rowno,12].Value:='备注';
cxView3DblClick(Sender);
//刷新需要打印单据的数据.
with cxview2.DataControllerdo
begin
DataSet.First;
zj:=0;
//double;
repeat
inc(rowno);
MyExcel.Cells[rowno,1].Value:=rowno-3;
MyExcel.Cells[rowno,2].Value:=Trim(DataSet.FieldValues['目录编号']);
if DataSet.fieldbyname('材料名称').IsNull then
MyExcel.Cells[rowno,3].Value:=''
else
MyExcel.Cells[rowno,3].Value:=Trim(DataSet.FieldValues['材料名称']);
if DataSet.fieldbyname('材质').IsNull then
MyExcel.Cells[rowno,4].Value:=''
else
MyExcel.Cells[rowno,4].Value:=Trim(DataSet.FieldValues['材质']);
if DataSet.fieldbyname('规格型号').IsNull then
MyExcel.Cells[rowno,5].Value:=''
else
MyExcel.Cells[rowno,5].Value:=Trim(DataSet.FieldValues['规格型号']);
if DataSet.fieldbyname('计量单位').IsNull then
MyExcel.Cells[rowno,6].Value:=''
else
MyExcel.Cells[rowno,6].Value:=Trim(DataSet.FieldValues['计量单位']);
if DataSet.fieldbyname('单价').IsNull then
MyExcel.Cells[rowno,7].Value:=''
else
MyExcel.Cells[rowno,7].Value:=Trim(DataSet.FieldValues['单价']);
MyExcel.Cells[rowno,8].Value:=FormatFloat('###########0.##',DataSet.FieldValues['实收数量']);
MyExcel.Cells[rowno,10].Value:=Trim(DataSet.FieldValues['母号'])+'-'+Trim(DataSet.FieldValues['子号'])+'-'+Trim(DataSet.FieldValues['细号']);
MyExcel.Cells[rowno,11].Value:=FormatFloat('###,###,###,##0.00',DataSet.FieldValues['单价']*DataSet.FieldValues['实收数量']);
if DataSet.fieldbyname('备注').IsNull then
MyExcel.Cells[rowno,12].Value:=''
else
MyExcel.Cells[rowno,12].Value:=Trim(DataSet.FieldValues['备注']);
zj:=zj+MyExcel.Cells[rowno,11].Value;
DataSet.next;
until DataSet.Eof;
end;
inc(rowno);
MyExcel.Cells[rowno,2].Value:='本页小计';
MyExcel.Cells[rowno,11].Value:=FormatFloat('###,###,###,##0.00',zj);
drawgrid('a3:l'+IntToStr(rowno));
inc(rowno);
MyExcel.Cells[rowno,2].Value:='单位主管: 领料人: 审核: 发料人:';
// MyExcel.ActiveSheet.PrintPreview ;
//else
for i:=1 to StrToInt(Edit1.Text)do
MyExcel.ActiveSheet.PrintOut;
MyExcel.ActiveWorkBook.Saved:=true;
MyWorkbook.close;
if CheckBox2.Checked=false then
//只打印当前的一张票据...
Break;
cx.next;
//需要打印全部票据.
until cx.Eof;
end;