高手帮一下忙,在线等!!!!!!!!!!!!!!!(200分)

  • 主题发起人 主题发起人 weianwen
  • 开始时间 开始时间
W

weianwen

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure SaveToExcelFile(DbgridName: TDbGrid);
var
XLApp: Variant;
Sheet: Variant;
WordApp,WordDoc,WordParagraph,WordRange,WordTable:variant;
I, J: Integer;
SaveDialog:TSaveDialog;
pBookMark: TBookMark;
StrSaveFile:string;
IntFileType:integer;
SltRec,SltCol :integer;
ColIndex,RowIndex : Integer;
begin
if dbgridname.DataSource.DataSet.IsEmpty then
begin
messagebox(application.Handle,'没有任何数据,不能进行保存','警告',mb_ok);
abort;
end;
SaveDialog:=TSaveDialog.Create(Nil);
SaveDialog.Filter := 'Microsoft Excel 文件|*.xls|Microsoft Word 文件|*.doc';
SaveDialog.Execute;
IntFileType:=SaveDialog.FilterIndex;
StrSaveFile:=SaveDialog.FileName;
if length(StrSaveFile)=0 then exit;
try
screen.Cursor:=crHourGlass;
case IntFileType of
1: begin
try
XLApp := CreateOleObject('Excel.Application');
XLApp.WorkBooks.Add(-4167);
XLApp.WorkBooks[1].WorkSheets[1].Name := '导出数据';
Sheet := XLApp.WorkBooks[1].WorkSheets['导出数据'];
J := 1;
except
MessageBox(GetActiveWindow,'无法调用Mircorsoft Excel! '+chr(13)+chr(10)+
'请检查是否安装了Mircorsoft Excel。','提示',MB_OK+MB_ICONINFORMATION);
Exit;
end;
with Dbgridname.DataSource.DataSet do
begin
pBookMark := GetBookmark;
DisableControls;
for I:=0 to dbgridname.Columns.Count-1 do
begin
if dbgridname.Columns.Visible=False then
Continue;
Sheet.Cells[J,I+1] := dbgridname.Columns.Title.Caption;
end;
Inc(J);
First;
while not Eof do
begin
for I:=0 to dbgridname.Columns.Count-1 do
begin
if dbgridname.Columns.Visible=False then
Continue;
Sheet.Cells[J,I+1] := trim(dbgridname.DataSource.DataSet.Fieldbyname(dbgridname.Columns.FieldName).asstring);
end;
Inc(J);
Next;
end;
GotoBookmark(pBookMark);
FreeBookmark(pBookMark);
EnableControls;
end;
XLApp.activeworkbook.saveas(StrSaveFile);
Application.ProcessMessages;
XLApp.Application.Quit;
end;
2: begin
try
if VarIsEmpty(WordApp) then
WordApp := CreateOleObject('word.Application');
WordDoc := WordApp.documents.add;
WordApp.ActiveDocument.PageSetup.TopMargin := 57;
WordApp.ActiveDocument.PageSetup.BottomMargin := 57;
WordApp.ActiveDocument.PageSetup.RightMargin := 57;
WordApp.ActiveDocument.PageSetup.LeftMargin := 57;
WordParagraph := WordApp.activedocument.paragraphs.add;
WordRange := WordParagraph.range;
WordRange.Font.Size := 10;
WordRange.Font.Name := '宋体';
except
MessageBox(GetActiveWindow,'无法调用Mircorsoft Word! '+chr(13)+chr(10)+
'请检查是否安装了Mircorsoft Word。','提示',MB_OK+MB_ICONINFORMATION);
Abort;
end;
SltRec:=dbgridname.SelectedRows.Count;
SltCol := 0;
for j := 0 to dbgridname.Columns.Count-1 do
begin
if dbgridname.Columns[J].Visible then
SltCol := SltCol +1;
end;
WordRange := WordApp.ActiveDocument.Content;
WordTable:= WordApp.activedocument.tables.add(WordRange,SltRec + 1,SltCol);
ColIndex := 1;
for j := 0 to dbgridname.Columns.Count-1 do
begin
if (dbgridname.Columns[j].Visible = false) then
Continue;
WordTable.columns[j].setwidth(dbgridname.Columns[j].Width,1);
WordTable.Cell(1,ColIndex).Range.InsertAfter(dbgridname.Columns[j].Title.Caption+#13);
ColIndex := ColIndex + 1;
end;
RowIndex := 2;
ColIndex := 1;
with dbgridname.DataSource.DataSet do
begin
First;
pBookMark := GetBookmark;
DisableControls;
while not Eof do
begin
for j := 0 to dbgridname.Columns.Count-1 do
begin
if (dbgridname.Columns[j].Visible<>false) then
begin
WordTable.Cell(RowIndex,ColIndex).Range.InsertAfter
(dbgridname.DataSource.DataSet.Fieldbyname(dbgridname.Columns[j].FieldName).asstring+#13);
ColIndex := ColIndex + 1;
end;
end;
RowIndex := RowIndex + 1;
ColIndex := 1;
Next;
end;
GotoBookmark(pBookMark);
FreeBookmark(pBookMark);
EnableControls;
end;
WordApp.activedocument.saveas(StrSaveFile);
Application.ProcessMessages;
WordApp.Application.Quit;
end;
end;
Finally
SaveDialog.Free;
screen.Cursor:=crDefault;
end;
end;
现在一执行到 WordTable.columns[j].setwidth(dbgridname.Columns[j].Width,1);就提示错误,请问怎么可以解决.
 
在线等!!!!!!!!!!,分少可以在加.
 
好像word表列从1开始计数,改为
WordTable.columns[j+1].setwidth(dbgridname.Columns[j].Width,1);
试试
 
myid0602:不是这的问题,还有没有别的
 
自己顶一下
 
1.注释掉该行,试试。
2.检查dbgridname.Columns[j].Width的值
 
那肯定行呀,我就是想把dbgridname.Columns[j].Width的长度负到Word文档中
 
谢谢你的回答,那样不是我想要实现的结果.
 
WordTable.columns.item(j+1).setwidth(dbgridname.Columns[j].Width,1);
 
接受答案了.
 
后退
顶部