OleContainer1对象如何使打开的文件嵌在OLE里面,而不是在外部打开文档.
解决方案 »
- 请问怎么用ClientSocket控件实现SSL协议的Post方法?
- 这种Access数据库一致化谁弄过?
- 如何判定没有扩展名的文件
- 有谁用过sncCurrencyEdit这个控件啊?用过的来看看!
- 请问我想编写一个监控软件。下载了VIDEOCAP2.3控件。用来播放avi,mpeg等格式文件。但最高支持delphi5,我用的是7,提示dsgnintf.dcu找不
- 该死的dll,这个怎么解决啊
- 小猫提问:安装制作问题(六)WISE版
- 出现这样的提示,是什么错误呢?
- delphi如何调用c语言程序???
- 关于listview的绘图问题
- 50分寻求一条较复杂的UPDATE语句。
- 数据导出问题?急救!
How do you manipulate MS Office applications? The Answer is to use TOLEContainer. Problem starts with accessing the correct object and their properties. Some samples are added to Delphi demos, but all of them are targeted at MSWord. Here is a sample or trick code for MSExcel.
// procedure is activated when OleOject activates user interface
// procedure copies TStringGrid content to an ( OleObject ) Excel sheet
procedure TForm1.OleContainer1Activate(Sender: TObject);
var ExcelSheet : Variant;
Count, Curent : Variant;
i,j : Integer;
Begin
{first we read how many sheets are open in a specified
Excel document}
Count:=OleContainer1.OleObject.Application.Sheets.Count;
{then we read the number of a sheet to witch user wants
to add StringGrid content}
Curent:=StrToInt(OKBottomDlg.Edit2.Text);
If Curent <> 0 Then
If Curent <= Count then
// if the sheet with index Curent exist then copy content
Begin
{first we activate the desiered sheet object}
OleContainer1.OleObject.Application.Sheets[Count].Activate;
{pass the object to a variant variable}
ExcelSheet:=OleContainer1.OleObject.Application.ActiveSheet;
{now we can do what ever we like with it}
ExcelSheet.Name:=OKBottomDlg.Edit3.Text+IntToStr(Count);
For i:=0 To StringGrid1.RowCount Do
For j:=0 To StringGrid1.ColCount Do
ExcelSheet.Cells(i,j):=StringGrid1.Cells[j,i];
// here we copy the content
End
else // else if the sheet we are trying to access doesn't exsist
Begin
// we add new sheets untill the requested
// user's index is reached ( curent variable )
For i:=Count+1 To Curent do
OleContainer1.OleObject.Application.Sheets.Add;
// again we do as above
OleContainer1.OleObject.Application.Sheets[Curent].Activate;
ExcelSheet:=OleContainer1.OleObject.Application.ActiveSheet;
ExcelSheet.Name:=OKBottomDlg.Edit3.Text+IntToStr(Count);
For i:=0 To StringGrid1.RowCount Do
For j:=0 To StringGrid1.ColCount Do
ExcelSheet.Cells(i,j):=StringGrid1.Cells[j,i];
End;
end; Now MSExcel has objects:
Application - Through which we can reach menus, toolbars and all of its commands
Workbooks - Is something like an array of all workbooks in a document you can use it to manipulate passwords of a document, sharing etc..
Workbook is an elemet of object specefied above
Sheet(s) is a general object used to manipulate actual data.
Worksheet(s) is something like above, but will raise an exception if doesn't exsist ( for instance if document contains graph sheet and you would try to access it through Worksheets or Worksheet object ) while Sheet object can be used on all types of sheets in MSExcel
var
xlApp, Sheet: Variant;
begin
xlApp := OleContainer1.OleObject.Application;
Sheet := xlApp.WorkBooks[1].WorkSheets[1];
Sheet.Cells[1, 1] := '1234';
Sheet.Range['A1:C10'].Font.Bold := True;
end;