BCB生成EXCEL表并实现表格的设置数据的填写.doc
/将数据导入excel表中201010 /保存数据到excel文件中 SaveDialog1->FileName =s_ReportName+EDt.FormatString("yyyymmdd"); if(SaveDialog1->Execute() AnsiString filenamex=SaveDialog1->FileName+".xls" Variant Wb,Sh1; Wb=CreateOleObject("Excel.Sheet"); int i=0; AnsiString cc; Sh1=Wb.OlePropertyGet("ActiveSheet"); /设置1行1列单元格属性 /设置字号为18,粗体 Sh1.PG("Cells",1,1).PG("Font").PS("Size",18); Sh1.PG("Cells",1,1).PG("Font").PS("Bold",true); Sh1.PG("Cells",1,1).PG("Font").PS("Name","宋体"); /设置行高 Sh1.PG("Rows",1).PS("RowHeight",1.155/0.035); / 1/0.035=1cm /合并单元格 Sh1.PG("Range", "A1:K1 ").PR("Merge"); /水平对齐 1:顶端对齐,2:居中, 3:底端对齐 Sh1.PG("Cells",1,1).PS("HorizontalAlignment",3); /垂直对齐 2:左端对齐,3:居中, 4:右端对齐 Sh1.PG("Cells",1,1).PS("VerticalAlignment",2); /设置2行1列单元格属性 /设置字号为18,粗体 Sh1.PG("Cells",2,1).PG("Font").PS("Size",16); Sh1.PG("Cells",2,1).PG("Font").PS("Bold",true); Sh1.PG("Cells",2,1).PG("Font").PS("Name","宋体"); Sh1.PG("Rows",2).PS("RowHeight",1.155/0.035); / 1/0.035=1cm Sh1.PG("Range", "A2:K2 ").PR("Merge"); Sh1.PG("Cells",2,1).PS("HorizontalAlignment",3); Sh1.PG("Cells",2,1).PS("VerticalAlignment",2); /设置3行1列单元格属性 Sh1.PG("Cells",3,1).PG("Font").PS("Size",10); Sh1.PG("Cells",3,1).PG("Font").PS("Bold",true); Sh1.PG("Cells",3,1).PG("Font").PS("Name","宋体"); Sh1.PG("Range", "A3:H3").PR("Merge"); /设置3行9列单元格属性 Sh1.PG("Cells",3,9).PG("Font").PS("Size",10); Sh1.PG("Cells",3,9).PG("Font").PS("Bold",true); Sh1.PG("Cells",3,9).PG("Font").PS("Name","宋体"); Sh1.PG("Rows",3).PS("RowHeight",1/0.035); / 1/0.035=1cm /设置列宽 Sh1.PG("Columns",1).PS("ColumnWidth",5.2); Sh1.PG("Columns",2).PS("ColumnWidth",8); Sh1.PG("Columns",3).PS("ColumnWidth",8.4); Sh1.PG("Columns",4).PS("ColumnWidth",7.6); Sh1.PG("Columns",5).PS("ColumnWidth",7.2); Sh1.PG("Columns",6).PS("ColumnWidth",7.2); Sh1.PG("Columns",7).PS("ColumnWidth",7.2); Sh1.PG("Columns",8).PS("ColumnWidth",7.2); Sh1.PG("Columns",9).PS("ColumnWidth",7.2); Sh1.PG("Columns",10).PS("ColumnWidth",7.2); Sh1.PG("Columns",11).PS("ColumnWidth",6); /填写标题栏 Sh1.OlePropertyGet("Cells",1,1).OlePropertySet("Value",s_StationName); /填写报表名称 Sh1.OlePropertyGet("Cells",2,1).OlePropertySet("Value",s_ReportName); /填写日期栏 Sh1.OlePropertyGet("Cells",3,1).OlePropertySet("Value","报表日期:"+s_ReportDate); Sh1.OlePropertyGet("Cells",3,9).OlePropertySet("Value","单位:RMB 元"); /填写列名称 Sh1.PG("Cells",4,1).PG("Font").PS("Size",10); Sh1.PG("Cells",4,1).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,1).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,1).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,1).PS("VerticalAlignment",2); cc="序号" Sh1.OlePropertyGet("Cells",4,1).OlePropertySet("Value",cc); Sh1.PG("Cells",4,2).PG("Font").PS("Size",10); Sh1.PG("Cells",4,2).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,2).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,2).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,2).PS("VerticalAlignment",2); cc="车牌号码" Sh1.OlePropertyGet("Cells",4,2).OlePropertySet("Value",cc); Sh1.PG("Cells",4,3).PG("Font").PS("Size",10); Sh1.PG("Cells",4,3).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,3).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,3).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,3).PS("VerticalAlignment",2); cc="车队小号" Sh1.OlePropertyGet("Cells",4,3).OlePropertySet("Value",cc); Sh1.PG("Cells",4,4).PG("Font").PS("Size",10); Sh1.PG("Cells",4,4).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,4).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,4).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,4).PS("VerticalAlignment",2); cc="车主名称" Sh1.OlePropertyGet("Cells",4,4).OlePropertySet("Value",cc); Sh1.PG("Cells",4,5).PG("Font").PS("Size",10); Sh1.PG("Cells",4,5).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,5).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,5).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,5).PS("VerticalAlignment",2); cc="交费次数" Sh1.OlePropertyGet("Cells",4,5).OlePropertySet("Value",cc); Sh1.PG("Cells",4,6).PG("Font").PS("Size",10); Sh1.PG("Cells",4,6).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,6).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,6).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,6).PS("VerticalAlignment",2); cc="验票次数" Sh1.OlePropertyGet("Cells",4,6).OlePropertySet("Value",cc); Sh1.PG("Cells",4,7).PG("Font").PS("Size",10); Sh1.PG("Cells",4,7).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,7).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,7).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,7).PS("VerticalAlignment",4); cc="单次收费金额" Sh1.OlePropertyGet("Cells",4,7).OlePropertySet("Value",cc); Sh1.PG("Cells",4,8).PG("Font").PS("Size",10); Sh1.PG("Cells",4,8).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,8).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,8).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,8).PS("VerticalAlignment",4); cc="通过次数合计" Sh1.OlePropertyGet("Cells",4,8).OlePropertySet("Value",cc); Sh1.PG("Cells",4,9).PG("Font").PS("Size",10); Sh1.PG("Cells",4,9).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,9).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,9).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,9).PS("VerticalAlignment",4); cc="收费金额合计" Sh1.OlePropertyGet("Cells",4,9).OlePropertySet("Value",cc); Sh1.PG("Cells",4,10).PG("Font").PS("Size",10); Sh1.PG("Cells",4,10).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,10).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,10).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,10).PS("VerticalAlignment",4); cc="卡内余额" Sh1.OlePropertyGet("Cells",4,10).OlePropertySet("Value",cc); Sh1.PG("Cells",4,11).PG("Font").PS("Size",10); Sh1.PG("Cells",4,11).PG("Font").PS("Bold",true); Sh1.PG("Cells",4,11).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",4,11).PS("HorizontalAlignment",3); Sh1.PG("Cells",4,11).PS("VerticalAlignment",2); cc="备注" Sh1.OlePropertyGet("Cells",4,11).OlePropertySet("Value",cc); i=4; cardcnt=0; while (!DataModules->ADOQuery->Eof) i+; cardcnt+; /填写列内容 Sh1.PG("Cells",i,1).PG("Font").PS("Size",10); Sh1.PG("Cells",i,1).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,1).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,1).PS("VerticalAlignment",2); cc= IntToStr(cardcnt); Sh1.OlePropertyGet("Cells",i,1).OlePropertySet("Value",cc); Sh1.PG("Cells",i,2).PG("Font").PS("Size",10); Sh1.PG("Cells",i,2).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,2).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,2).PS("VerticalAlignment",2); cc= DataModules->ADOQuery->Fields->FieldByName("CPUVLPNumber")->AsString.Trim(); Sh1.OlePropertyGet("Cells",i,2).OlePropertySet("Value",cc); Sh1.PG("Cells",i,3).PG("Font").PS("Size",10); Sh1.PG("Cells",i,3).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,3).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,3).PS("VerticalAlignment",2); cc= DataModules->ADOQuery->Fields->FieldByName("CPUSVLPNumber")->AsString.Trim(); Sh1.OlePropertyGet("Cells",i,3).OlePropertySet("Value",cc); Sh1.PG("Cells",i,4).PG("Font").PS("Size",10); Sh1.PG("Cells",i,4).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,4).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,4).PS("VerticalAlignment",2); cc= DataModules->ADOQuery->Fields->FieldByName("CardOwnerName")->AsString.Trim(); Sh1.OlePropertyGet("Cells",i,4).OlePropertySet("Value",cc); Sh1.PG("Cells",i,5).PG("Font").PS("Size",10); Sh1.PG("Cells",i,5).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,5).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,5).PS("VerticalAlignment",2); TotalJFCnt+=DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger; cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger); Sh1.OlePropertyGet("Cells",i,5).OlePropertySet("Value",cc); Sh1.PG("Cells",i,6).PG("Font").PS("Size",10); Sh1.PG("Cells",i,6).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,6).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,6).PS("VerticalAlignment",2); TotalYPCnt+=DataModules->ADOQuery->Fields->FieldByName("YapCount")->AsInteger; cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("YapCount")->AsInteger); Sh1.OlePropertyGet("Cells",i,6).OlePropertySet("Value",cc); Sh1.PG("Cells",i,7).PG("Font").PS("Size",10); Sh1.PG("Cells",i,7).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,7).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,7).PS("VerticalAlignment",4); cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("Price")->AsInteger); Sh1.OlePropertyGet("Cells",i,7).OlePropertySet("Value",cc); Sh1.PG("Cells",i,8).PG("Font").PS("Size",10); Sh1.PG("Cells",i,8).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,8).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,8).PS("VerticalAlignment",2); TotalTGCnt+=DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger+DataModules->ADOQuery->Fields->FieldByName("YapCount")->AsInteger; cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger+DataModules->ADOQuery->Fields->FieldByName("YapCount")->AsInteger); Sh1.OlePropertyGet("Cells",i,8).OlePropertySet("Value",cc); Sh1.PG("Cells",i,9).PG("Font").PS("Size",10); Sh1.PG("Cells",i,9).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,9).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,9).PS("VerticalAlignment",2); TotalJFPrice+=DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger*DataModules->ADOQuery->Fields->FieldByName("Price")->AsInteger; cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("AutoType")->AsInteger*DataModules->ADOQuery->Fields->FieldByName("Price")->AsInteger); Sh1.OlePropertyGet("Cells",i,9).OlePropertySet("Value",cc); Sh1.PG("Cells",i,10).PG("Font").PS("Size",10); Sh1.PG("Cells",i,10).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",i,10).PS("HorizontalAlignment",3); Sh1.PG("Cells",i,10).PS("VerticalAlignment",2); cc= IntToStr(DataModules->ADOQuery->Fields->FieldByName("RestCost")->AsInteger); Sh1.OlePropertyGet("Cells",i,10).OlePropertySet("Value",cc); DataModules->ADOQuery->Next(); /交费次数合计 Sh1.PG("Cells",cardcnt+5,5).PG("Font").PS("Size",10); Sh1.PG("Cells",cardcnt+5,5).PG("Font").PS("Bold",true); Sh1.PG("Cells",cardcnt+5,5).PG("Font").PS("Name","宋体"); Sh1.PG("Cells",cardcnt+5,5).PS("HorizontalAlignment",3); Sh1.PG("Cells",cardcnt+5,5).PS("VerticalAlignment",4); /利用excel中的函数进行求和运算 cc= "=SUM(E5:E"+IntToStr(cardcnt+4)+ ") " Sh1.OlePropertyGet("Cells",cardcnt+5,5).OlePropertySet("Value",cc); . /设置打印页边距:上 下 左 右 水平 垂直 Sh1.PG("PageSetup").PS("TopMargin",2.0/0.035); Sh1.PG("PageSetup").PS("BottomMargin",2.0/0.035); Sh1.PG("PageSetup").PS("LeftMargin",1.0/0.035); Sh1.PG("PageSetup").PS("RightMargin",1.0/0.035); Sh1.PG("PageSetup").PS("CenterHorizontally",1.5/0.035); /Sh1.PG("PageSetup").PS("CenterVertically",1.5/0.035); /Sh1.PG("PageSetup").PS("PrintGridLines",true); /画边框表格 Variant ERange,EBorders; AnsiString strRange; strRange = "A"+IntToStr(4)+":K"+IntToStr(cardcnt+5); /获取操作范围 ERange = Sh1.OlePropertyGet("Range",strRange.c_str(); EBorders = ERange.OlePropertyGet("Borders");