中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Delphi > 综合文章
转贴:Delphi Office 组件集常见问答
作者:未知 时间:2004-10-06 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无

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.

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有