原文地址:http://topic.csdn.net/t/20041026/17/3493075.html
procedure TCftjFrm.Button2Click(Sender: TObject);
var
chexingshu:integer;
Excelx1:string;
Excelx2:string;
xl,Cell1: olevariant;
Sum:integer;
begin
if ADOQuery1.RecordCount <> 0 then
begin
ExcelApp := CreateOleObject( 'Excel.Application ' );
ExcelApp.Caption := '学生处罚报表打印 ';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkSheets[1].Activate;
ExcelApp.Visible := True;
ExcelApp.ActiveWindow.Zoom := 75;
chexingshu:=9;
excelx1:=excelop(chexingshu)+ '1 ';
excelx2:= 'a1 ';
ExcelApp.Range[excelx2, excelx1].Merge(xl);
ExcelApp.Cells[1,1].Value := '学生处罚报表 ';
ExcelApp.cells.Item[1, 1].font.size := 20;
ExcelApp.cells.Item[1, 1].RowHeight := 25;
ExcelApp.cells.Item[1 , 1].Font.Bold := True;
ExcelApp.cells.Item[1, 1].HorizontalAlignment := xlCenter;
ExcelApp.cells.Item[1, 1].font.Name := '隶书 ';
ExcelApp.cells.Item[2, 1].HorizontalAlignment := xlCenter;
ExcelApp.Range[ 'a2 ', 'a3 '].Merge(xl);
ExcelApp.Cells[2,1].Value := '年级 ';
ExcelApp.cells.Item[2 , 1].Font.Bold := True;
ExcelApp.Range[ 'a2 ', 'a3 '].ColumnWidth := 20;
ExcelApp.cells.Item[2, 1].RowHeight := 20;
ExcelApp.Range[ 'b2 ', 'b3 '].Merge(xl);
ExcelApp.Cells[2,2].Value := '班级 ';
ExcelApp.cells.Item[2 , 2].Font.Bold := True;
ExcelApp.Range[ 'c2 ', 'c3 '].Merge(xl);
ExcelApp.cells.Item[2, 2].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,3].Value := '学期 ';
ExcelApp.cells.Item[2 , 3].Font.Bold := True;
ExcelApp.Range[ 'd2 ', 'd3 '].Merge(xl);
ExcelApp.cells.Item[2, 3].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,4].Value := '姓名 ';
ExcelApp.cells.Item[2 , 4].Font.Bold := True;
ExcelApp.Range[ 'e2 ', 'e3 '].Merge(xl);
ExcelApp.Range[ 'd2 ', 'd2 '].ColumnWidth := 15;
ExcelApp.cells.Item[2, 4].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,5].Value := '学号 ';
ExcelApp.cells.Item[2 , 5].Font.Bold := True;
ExcelApp.Range[ 'e2 ', 'e3 '].Merge(xl);
ExcelApp.cells.Item[2, 5].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,6].Value := '性别 ';
ExcelApp.cells.Item[2 , 6].Font.Bold := True;
ExcelApp.Range[ 'f2 ', 'f3 '].Merge(xl);
ExcelApp.cells.Item[2, 6].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,7].Value := '处罚时间 ';
ExcelApp.cells.Item[2 , 7].Font.Bold := True;
ExcelApp.Range[ 'g2 ', 'g3 '].Merge(xl);
ExcelApp.cells.Item[2, 7].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,8].Value := '处罚类型 ';
ExcelApp.cells.Item[2 , 8].Font.Bold := True;
ExcelApp.Range[ 'h2 ', 'h3 '].Merge(xl);
ExcelApp.cells.Item[2, 8].HorizontalAlignment := xlCenter;
ExcelApp.Cells[2,9].Value := '处罚原因 ';
ExcelApp.cells.Item[2 , 9].Font.Bold := True;
ExcelApp.Range[ 'i2 ', 'i3 '].Merge(xl);
ExcelApp.cells.Item[2, 9].HorizontalAlignment := xlCenter;
Sum:=4;
While ADOQuery1.Eof <> True do
begin
ExcelApp.Cells[sum,1].Value :=ADOQuery1.FieldByName( 'C_Nj ').AsString;
ExcelApp.cells.Item[sum,1].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,2].Value :=ADOQuery1.FieldByName( 'C_Class ').AsString;
ExcelApp.cells.Item[sum,2].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,3].Value :=ADOQuery1.FieldByName( 'C_Xq ').AsString;
ExcelApp.cells.Item[sum,3].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,4].Value :=ADOQuery1.FieldByName( 'C_Name ').AsString;
ExcelApp.cells.Item[sum,4].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,5].Value :=ADOQuery1.FieldByName( 'C_Xh ').AsString;
ExcelApp.cells.Item[sum,5].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,6].Value :=ADOQuery1.FieldByName( 'C_Sex ').AsString;
ExcelApp.cells.Item[sum,6].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,7].Value :=ADOQuery1.FieldByName( 'c_Date ').AsString;
ExcelApp.cells.Item[sum,7].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,8].Value :=ADOQuery1.FieldByName( 'C_Nx ').AsString;
ExcelApp.cells.Item[sum,8].HorizontalAlignment := xlCenter;
ExcelApp.Cells[sum,9].Value :=ADOQuery1.FieldByName( 'C_Yy ').AsString;
ExcelApp.cells.Item[sum,9].HorizontalAlignment := xlCenter;
sum:=sum+1;
ADOQuery1.next;
end;
ExcelApp.ActiveSheet.Protect(DrawingObjects:=True,Contents:=True,Scenarios:=True,AllowFormattingCells:=True,AllowFormattingColumns:=True,AllowFormattingRows:=True,PassWord:= 'lxdshihongchun ');
end
else
MessageBox(CFtjfrm.Handle, '没有可打印的查询结果! ', '打印提示 ',MB_OK+MB_ICONINFORMATION);
end;