分享
 
 
 

转贴:Delphi Office 组件集常见问答

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

http://www.winsoft.sk/officfaq.htm

Office Component Suite FAQ

Last Updated Friday January 03, 2003

Q1. How do I open an Excel application, workbook, worksheet?

Q2. How do I close an Excel application?

Q3. How do I specify a range of cells?

Q4. How do I set cell values?

Q5. How do I access directly Excel application COM object?

Q6. How do I access directly Excel workbook COM object?

Q7. How do I access directly Excel worksheet COM object?

Q8. How do I access directly Excel range COM object?

Q9. How do I access directly Excel chart COM object?

Q10. How do I display note indicators?

Q11. How do I print a worksheet?

Q12. How do I copy content and format of cells?

Q13. How do I set cell borders?

Q14. How do I set cell font?

Q15. How do I find and replace some text?

Q16. How do I close Excel Worksheet?

Q17. How do I save the specified Word document in HTML format?

Q18. How do I insert the current page number at the begining of each line?

Q19. How do I specify a workbook name?

Q20. How do I specify a worksheet name?

Q21. How do I draw a line in Excel worksheet?

Q22. How do I place a picture into worksheet?

Q23. How do I open a password protected workbook? New!!!

Q24. How do I add a table in the word document? New!!!

Q1. How do I open an Excel application, workbook, worksheet?

ExcelApplication.Active := True; // opens Excel application

ExcelWorkbook.Parent := ExcelApplication;

ExcelWorkbook.WorkbookName := 'C:\MyFile.xls';

ExcelWorkbook.Active := True; // opens Excel workbook

ExcelWorksheet.Parent := ExcelWorkbook;

ExcelWorksheet.WorksheetName := 'MySheet';

ExcelWorksheet.Active := True; // opens Excel worksheet

Q2. How do I close an Excel application?

ExcelApplication.Active := False;

Q3. How do I specify a range of cells?

ExcelRange.Range := 'B3'; // one cell selected

ExcelRange.Range := 'A1:B2'; // four cells A1, A2, B1, B2 selected

Q4. How do I set cell values?

ExcelRange.Formula := 3; // integer constant

ExcelRange.Formula := 'Hello, world!'; // string constant

ExcelRange.Formula := '=SUM(B1:B5)'; // formula specification

Q5. How do I access directly Excel application COM object?

use ExcelApplication property:

with ExcelApplication1.ExcelApplication.ActiveCell do

ShowMessage(IntToStr(Row) + ':' + IntToStr(Column)); // shows active cell position

Q6. How do I access directly Excel workbook COM object?

use ExcelWorkbook property:

ShowMessage(ExcelWorkbook1.ExcelWorkbook.Windows[1].Caption); // shows workbook window caption

Q7. How do I access directly Excel worksheet COM object?

use ExcelWorksheet property:

ShowMessage(IntToStr(ExcelWorksheet1.ExcelWorksheet.Comments.Count)); // shows numer of worksheet comments

Q8. How do I access directly Excel range COM object?

use ExcelRange property:

ShowMessage(IntToStr(ExcelRange1.ExcelRange.Rows.Count)); // shows numer of range rows

Q9. How do I access directly Excel chart COM object?

use ExcelChart property:

ShowMessage(ExcelChart1.ExcelChart.ChartTitle[0].Text); // shows chart title

Q10. How do I display note indicators?

ExcelApplication.DisplayNoteIndicators := True;

Note indicators are small dots in upper-right corner of cell. They display cell tips for cells containing notes.

Q11. How do I print a worksheet?

ExcelWorksheet.PrintOut(

EmptyParam, // from

EmptyParam, // to

EmptyParam, // copies

True, // preview

EmptyParam, // active printer

EmptyParam, // print to file

EmptyParam, // collate

EmptyParam); // file name for print to file

Q12. How do I copy content and format of cells?

ExcelRange.FillDown; // takes content and format of cells from top row of range and fills other rows from top to bottom

ExcelRange.FillUp; // takes content and format of cells from bottom row of range and fills other rows from bottom to top

ExcelRange.FillRight; // takes content and format of cells from left column of range and fills other columns from left to right

ExcelRange.FillLeft; // takes content and format of cells from right column of range and fills other columns from right to left

Q13. How do I set cell borders?

with ExcelRange.ExcelRange.Borders do

begin

Color := clNavy; // navy color

LineStyle := xlDouble; // double style

end;

applicable line styles are: xlContinuous, xlDash, xlDashDot, xlDashDotDot, xlDot, xlDouble, xlSlantDashDot and xlLineStyleNone

Q14. How do I set cell font?

with ExcelRange.ExcelRange.Font do

begin

Name := 'Terminal';

Color := clBlue;

Size := 12;

Italic := False;

Bold := True;

end;

Q15. How do I find and replace some text?

var

FindText: OleVariant;

ReplaceText: OleVariant;

Replace: OleVariant;

FindText := 'a';

ReplaceText := 'b';

Replace := wdReplaceOne;

with WordDocument1.WordDocument.Content do

Find.Execute(FindText, // text to find

EmptyParam, // match case

EmptyParam, // match whole word

EmptyParam, // match wildcards

EmptyParam, // match sounds like

EmptyParam, // match all word forms

EmptyParam, // forward

EmptyParam, // wrap

EmptyParam, // format

ReplaceText, // replace with

Replace, // replace

EmptyParam, // match Kashida

EmptyParam, // match diacritics

EmptyParam, // match AlefHamza

EmptyParam); // match control

Q16. How do I close Excel Worksheet?

ExcelWorkBook1.Close(False, // save changes

EmptyParam, // filename

EmptyParam); // route workbook

Q17. How do I save the specified Word document in HTML format?

var

FileName: OleVariant;

FileFormat: OleVariant;

FileName := 'c:\mydoc.html';

FileFormat := wdFormatHTML;

WordDocument1.SaveAs(FileName, // document name

FileFormat, // file format

EmptyParam, // lock the document for comments

EmptyParam, // password for opening the document

EmptyParam, // add the document to the list of recently used files

EmptyParam, // password for saving changes to the document

EmptyParam, // Word suggest read-only status

EmptyParam, // save TrueType fonts with the document

EmptyParam, // save only the Windows version of the imported graphics

EmptyParam, // save the form data

EmptyParam); // save the document as AOCE letter (the mailer is saved)

Q18. How do I insert the current page number at the begining of each line?

with WordDocument1.WordDocument.Paragraphs do

for i := 1 to Count do

with Item(i).Range do

begin

End_ := Start; // select start of range (line)

Text := IntToStr(Information[wdActiveEndAdjustedPageNumber]) + ': '; // add text

end

Q19. How do I specify a workbook name?

ExcelWorkbook1.SaveAs('MyWorkbook.xls', EmptyParam, EmptyParam, EmptyParam, EmptyParam,

EmptyParam, xlNoChange, EmptyParam, EmptyParam, EmptyParam, EmptyParam);

Q20. How do I specify a worksheet name?

ExcelWorksheet1.ExcelWorksheet.Name := 'MyWorksheet';

Q21. How do I draw a line in Excel worksheet?

// draw line from (10, 10) to (300, 300)

// X, Y measured in points

ExcelWorksheet1.ExcelWorksheet.Shapes.AddLine(10, 10, 300, 300);

Q22. How do I place a picture into worksheet?

ExcelWorksheet1.ExcelWorksheet.Shapes.AddPicture(

'C:\MyBitmap.bmp', // filename

msoFalse, // LinkToFile (True = make link to file, False = make copy of the file in document)

msoCTrue, // SaveWithDocument (must be True if LinkToFile is False)

10, // Left

10, // Top

300, // Width

300); // Height

Q23. How do I open a password protected workbook?

ExcelApplication1.ExcelApplication.Workbooks.Open(

'C:\MyWorkbook.xls', // Filename

3, // UpdateLinks

False, // ReadOnly

EmptyParam, // Format

'passwd', // Password

'passwdwrite', // WriteResPassword

True, // IgnoreReadOnlyRecommended

EmptyParam, // Origin

EmptyParam, // Delimiter

EmptyParam, // Editable

EmptyParam, // Notify

EmptyParam, // Converter

True, // AddToMru

0); // lcid

Q24. How do I add a table in the word document?

var

DefaultTableBehavior, AutoFitBehavior: OleVariant;

Table: Word.Table;

WordRange1.RangeStart := 1;

WordRange1.RangeEnd := 2;

WordRange1.Active := True;

DefaultTableBehavior := wdWord8TableBehavior;

AutoFitBehavior := wdAutoFitContent;

Table := WordApplication1.WordApplication.ActiveDocument.Tables.Add(

WordRange1.WordRange, // Range

10, // NumRows

10, // NumColumns

DefaultTableBehavior, // DefaultTableBehavior

AutoFitBehavior); // AutoFitBehavior

Table.Cell(1,1).Range.Text := 'Hello!';

Have a question? Ask here

Copyright ?1999-2004 Winsoft. All rights reserved.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有