var MSWord: Variant; i,j,k: integer; begin MSWord := CreateOleObject('Word.BASIC'); MSWord.FileNew('Normal'); with StringGrid1 do //给StringGrid1填数字 for I := 0 to ColCount - 1 do for J:= 0 to RowCount - 1 do begin K := K + 1; Cells[I,J] := IntToStr(K); end; with StringGrid1 do //将StringGrid1的内容导出 for I := 0 to ColCount - 1 do begin MSWord.InsertBreak; for J:= 0 to RowCount - 1 do begin MSWord.Insert(',' + Cells[i,j]); end; end; //将选定的数据区转换成二维表格 MSWord.EditSelectAll; MSWord.TextToTable(ConvertFrom :=2 , NumColumns := StringGrid1.ColCount ); //设置表格内线、上线和下线 MSWord.BorderInSide; MSWord.BorderTop; MSWord.BorderBottom; MSWord.AppShow; end;别忘了uses Word97, Comobj, OleServer;
上面的代码有问题,用这个: var MSWord: Variant; i,j,k: integer; begin MSWord := CreateOleObject('Word.BASIC'); MSWord.FileNew('Normal'); with StringGrid1 do //给StringGrid1填数字 for I := 0 to RowCount - 1 do for J:= 0 to ColCount- 1 do begin K := K + 1; Cells[j,i] := IntToStr(K); end; with StringGrid1 do //将StringGrid1的内容导出 for I := 0 to RowCount - 1 do begin for J:= 0 to ColCount - 1 do begin if J>0 then MSWord.Insert(','+Cells[j,i]) else MSWord.Insert(Cells[j,i]) end; if I<RowCount-1 then MSWord.InsertBreak; end; //将选定的数据区转换成二维表格 MSWord.EditSelectAll; MSWord.TextToTable(ConvertFrom :=2 , NumColumns := StringGrid1.ColCount ); //设置表格内线、上线和下线 MSWord.BorderInSide; MSWord.BorderTop; MSWord.BorderBottom; MSWord.AppShow; end;
看看Word 的VBA帮助,你就会作更多的效果!
uses comobj;procedure inserttable(AWordApp:olevariant;agrid:Tstringgrid); var AWordDocument,mytable,Insertpoint,ARange:olevariant; i,j:byte; begin if AWordApp.documents.count>0 then AWordDocument:=awordapp.activedocument else AwordDocument:=awordapp.documents.add; Insertpoint:=Awordapp.selection.end; mytable:=AWordDocument.Tables.Add(AWordDocument.range(insertpoint,insertpoint), Agrid.rowcount,Agrid.colcount); //新表格 mytable.rows.item(1).height:=30; mytable.columns.item(1).width:=50; mytable.rows.item(1).shading.BackgroundPatternColorIndex := wdgray25; mytable.columns.item(1).shading.BackgroundPatternColorIndex := wdgray25; for i:=1 to Agrid.rowcount do for j:=1 to Agrid.colcount do begin mytable.cell(i,j).range.text:=Agrid.cells[j-1,i-1]; mytable.cell(i,j).range.font.size:=12; end; end;procedure TForm1.Button1Click(Sender: TObject); var app:olevariant; begin app:=createoleobject('word.application'); app.visible:=true; inserttable(app,stringgrid1); end;
MSWord: Variant;
i,j,k: integer;
begin
MSWord := CreateOleObject('Word.BASIC');
MSWord.FileNew('Normal');
with StringGrid1 do //给StringGrid1填数字
for I := 0 to ColCount - 1 do
for J:= 0 to RowCount - 1 do
begin
K := K + 1;
Cells[I,J] := IntToStr(K);
end;
with StringGrid1 do //将StringGrid1的内容导出
for I := 0 to ColCount - 1 do
begin
MSWord.InsertBreak;
for J:= 0 to RowCount - 1 do
begin
MSWord.Insert(',' + Cells[i,j]);
end;
end;
//将选定的数据区转换成二维表格
MSWord.EditSelectAll;
MSWord.TextToTable(ConvertFrom :=2 , NumColumns := StringGrid1.ColCount );
//设置表格内线、上线和下线
MSWord.BorderInSide;
MSWord.BorderTop;
MSWord.BorderBottom;
MSWord.AppShow;
end;别忘了uses Word97, Comobj, OleServer;
stringgrid和sql的结果集都是二维的,用代码控制stringgrid显示sql结果也是容易的。
var
MSWord: Variant;
i,j,k: integer;
begin
MSWord := CreateOleObject('Word.BASIC');
MSWord.FileNew('Normal');
with StringGrid1 do //给StringGrid1填数字
for I := 0 to RowCount - 1 do
for J:= 0 to ColCount- 1 do
begin
K := K + 1;
Cells[j,i] := IntToStr(K);
end;
with StringGrid1 do //将StringGrid1的内容导出
for I := 0 to RowCount - 1 do
begin
for J:= 0 to ColCount - 1 do
begin
if J>0 then
MSWord.Insert(','+Cells[j,i])
else
MSWord.Insert(Cells[j,i])
end;
if I<RowCount-1 then
MSWord.InsertBreak;
end;
//将选定的数据区转换成二维表格
MSWord.EditSelectAll;
MSWord.TextToTable(ConvertFrom :=2 , NumColumns := StringGrid1.ColCount );
//设置表格内线、上线和下线
MSWord.BorderInSide;
MSWord.BorderTop;
MSWord.BorderBottom;
MSWord.AppShow;
end;
comobj;procedure inserttable(AWordApp:olevariant;agrid:Tstringgrid);
var
AWordDocument,mytable,Insertpoint,ARange:olevariant;
i,j:byte;
begin
if AWordApp.documents.count>0 then
AWordDocument:=awordapp.activedocument
else
AwordDocument:=awordapp.documents.add; Insertpoint:=Awordapp.selection.end; mytable:=AWordDocument.Tables.Add(AWordDocument.range(insertpoint,insertpoint),
Agrid.rowcount,Agrid.colcount); //新表格
mytable.rows.item(1).height:=30;
mytable.columns.item(1).width:=50;
mytable.rows.item(1).shading.BackgroundPatternColorIndex := wdgray25;
mytable.columns.item(1).shading.BackgroundPatternColorIndex := wdgray25; for i:=1 to Agrid.rowcount do
for j:=1 to Agrid.colcount do
begin
mytable.cell(i,j).range.text:=Agrid.cells[j-1,i-1];
mytable.cell(i,j).range.font.size:=12;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
app:olevariant;
begin
app:=createoleobject('word.application');
app.visible:=true;
inserttable(app,stringgrid1);
end;