存为word的文挡var v_app,v_doc,v_range:variant; i,j:integer; begin try v_app:=CreateOleObject('word.application'); v_app.visible:=true; v_app.documents.add; v_doc:=v_app.ActiveDocument; v_range:=v_doc.range((v_doc.paragraphs.item(v_doc.paragraphs.count)).range.end-1); v_doc.tables.add(v_range,StringGrid1.rowCount,StringGrid1.ColCount); for i:=0 to StringGrid1.ColCount-1 do for j:=0 to StringGrid1.RowCount-1 do begin v_doc.tables.item(v_doc.tables.count).cell(j+1,i+1).range.InsertAfter(StringGrid1.cells[i,j]); end; except showmessage('你没装word!'); end;
Unit excel;
interface
uses Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ComObj;
//ComObj是操作OLE对象的函数集
type TForm1 =class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
implementation
{$R *.DFM} procedure TForm1.Button1Click(Sender: TObject);
var
eclApp,WorkBook:Variant;
//声明为OLE Automation 对象
xlsFileName:string;begin
xlsFileName:='ex.xls';
try
//创建OLE对象Excel Application与 WorkBook
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;
try
ShowMessage('下面演示:新建一个XLS文件,并写入数据,最后关闭它。');
workBook:=eclApp.workBooks.Add;
eclApp.Cells(1 , 1):='字符型';
eclApp.Cells(2 , 1):='Excel文件';
eclApp.Cells(1 , 2):='Money型';
eclApp.Cells(2 , 2):=10.01;
eclApp.Cells(1 , 3):='日期型';eclApp.Cells(2 , 3):=Date;
WorkBook.saveas(xlsFileName);
WorkBook.close;
ShowMessage('下面演示:打开刚创建的XLS文件,并修改其中的内容,然后,由用户决定是否保存。');
WorkBook:=eclApp.workBooks.Open(xlsFileName);
eclApp.Cells(2 , 1):='Excel文件类型';
if MessageDlg(xlsFileName+'文件已被修改,是否保存?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
WorkBook.save
else
workBook.Saved := True; //放弃修改
WorkBook.Close;
eclApp.Quit;
//退出Excel Application
//释放VARIANT变量
eclApp:=Unassigned;
except
ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。');
WorkBook.close;
eclApp.Quit;
//释放VARIANT变量
eclApp:=Unassigned;
end;
end; end. word表格没试过,不过DELPHI专门有操作Word的控件,你自己慢慢试吧。
txt的很简单,就是自己将表格线划出来就行(每种表格线都有固定的ASCII码)
i,j:integer;
begin
try
v_app:=CreateOleObject('word.application');
v_app.visible:=true;
v_app.documents.add; v_doc:=v_app.ActiveDocument;
v_range:=v_doc.range((v_doc.paragraphs.item(v_doc.paragraphs.count)).range.end-1); v_doc.tables.add(v_range,StringGrid1.rowCount,StringGrid1.ColCount);
for i:=0 to StringGrid1.ColCount-1 do
for j:=0 to StringGrid1.RowCount-1 do
begin
v_doc.tables.item(v_doc.tables.count).cell(j+1,i+1).range.InsertAfter(StringGrid1.cells[i,j]);
end;
except
showmessage('你没装word!');
end;