以下资料可能对你有帮助,好好看看<br><br><br>学完这个你就成为excel高手了!(Delphi对Excel的所有操作)逐个试试!<br> <br>一) 使用动态创建的方法 <br><br>首先创建 Excel 对象,使用ComObj: <br>var ExcelApp: Variant; <br>ExcelApp := CreateOleObject( 'Excel.Application' ); <br><br>1) 显示当前窗口: <br>ExcelApp.Visible := True; <br><br>2) 更改 Excel 标题栏: <br>ExcelApp.Caption := '应用程序调用 Microsoft Excel'; <br><br>3) 添加新工作簿: <br>ExcelApp.WorkBooks.Add; <br><br>4) 打开已存在的工作簿: <br>ExcelApp.WorkBooks.Open( 'C:/Excel/Demo.xls' ); <br><br>5) 设置第2个工作表为活动工作表: <br>ExcelApp.WorkSheets[2].Activate; <br>或 <br>ExcelApp.WorksSheets[ 'Sheet2' ].Activate; <br><br>6) 给单元格赋值: <br>ExcelApp.Cells[1,4].Value := '第一行第四列'; <br><br>7) 设置指定列的宽度(单位:字符个数),以第一列为例: <br>ExcelApp.ActiveSheet.Columns[1].ColumnsWidth := 5; <br><br>8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例: <br>ExcelApp.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米 <br><br>9) 在第8行之前插入分页符: <br>ExcelApp.WorkSheets[1].Rows.PageBreak := 1; <br><br>10) 在第8列之前删除分页符: <br>ExcelApp.ActiveSheet.Columns[4].PageBreak := 0; <br><br>11) 指定边框线宽度: <br>ExcelApp.ActiveSheet.Range[ 'B3
4' ].Borders[2].Weight := 3; <br>1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / ) <br><br>12) 清除第一行第四列单元格公式: <br>ExcelApp.ActiveSheet.Cells[1,4].ClearContents; <br><br>13) 设置第一行字体属性: <br>ExcelApp.ActiveSheet.Rows[1].Font.Name := '隶书'; <br>ExcelApp.ActiveSheet.Rows[1].Font.Color := clBlue; <br>ExcelApp.ActiveSheet.Rows[1].Font.Bold := True; <br>ExcelApp.ActiveSheet.Rows[1].Font.UnderLine := True; <br><br>14) 进行页面设置: <br><br>a.页眉: <br>ExcelApp.ActiveSheet.PageSetup.CenterHeader := '报表演示'; <br>b.页脚: <br>ExcelApp.ActiveSheet.PageSetup.CenterFooter := '第&P页'; <br>c.页眉到顶端边距2cm: <br>ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; <br>d.页脚到底端边距3cm: <br>ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 3/0.035; <br>e.顶边距2cm: <br>ExcelApp.ActiveSheet.PageSetup.TopMargin := 2/0.035; <br>f.底边距2cm: <br>ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2/0.035; <br>g.左边距2cm: <br>ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2/0.035; <br>h.右边距2cm: <br>ExcelApp.ActiveSheet.PageSetup.RightMargin := 2/0.035; <br>i.页面水平居中: <br>ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035; <br>j.页面垂直居中: <br>ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2/0.035; <br>k.打印单元格网线: <br>ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True; <br><br>15) 拷贝操作: <br><br>a.拷贝整个工作表: <br>ExcelApp.ActiveSheet.Used.Range.Copy; <br>b.拷贝指定区域: <br>ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy; <br>c.从A1位置开始粘贴: <br>ExcelApp.ActiveSheet.Range.[ 'A1' ].PasteSpecial; <br>d.从文件尾部开始粘贴: <br>ExcelApp.ActiveSheet.Range.PasteSpecial; <br><br>16) 插入一行或一列: <br>a. ExcelApp.ActiveSheet.Rows[2].Insert; <br>b. ExcelApp.ActiveSheet.Columns[1].Insert; <br><br>17) 删除一行或一列: <br>a. ExcelApp.ActiveSheet.Rows[2].Delete; <br>b. ExcelApp.ActiveSheet.Columns[1].Delete; <br><br>18) 打印预览工作表: <br>ExcelApp.ActiveSheet.PrintPreview; <br><br>19) 打印输出工作表: <br>ExcelApp.ActiveSheet.PrintOut; <br><br>20) 工作表保存: <br>if not ExcelApp.ActiveWorkBook.Saved then <br>ExcelApp.ActiveSheet.PrintPreview; <br><br>21) 工作表另存为: <br>ExcelApp.SaveAs( 'C:/Excel/Demo1.xls' ); <br><br>22) 放弃存盘: <br>ExcelApp.ActiveWorkBook.Saved := True; <br><br>23) 关闭工作簿: <br>ExcelApp.WorkBooks.Close; <br><br>24) 退出 Excel: <br>ExcelApp.Quit; <br><br>(二) 使用Delphi 控件方法 <br>在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。 <br><br>1) 打开Excel <br>ExcelApplication1.Connect; <br><br>2) 显示当前窗口: <br>ExcelApplication1.Visible[0]:=True; <br><br>3) 更改 Excel 标题栏: <br>ExcelApplication1.Caption := '应用程序调用 Microsoft Excel'; <br><br>4) 添加新工作簿: <br>ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0)); <br><br>5) 添加新工作表: <br>var Temp_Worksheet: _WorkSheet; <br>begin <br>Temp_Worksheet:=ExcelWorkbook1. <br>WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet; <br>ExcelWorkSheet1.ConnectTo(Temp_WorkSheet); <br>End; <br><br>6) 打开已存在的工作簿: <br>ExcelApplication1.Workbooks.Open (c:/a.xls <br>EmptyParam,EmptyParam,EmptyParam,EmptyParam, <br>EmptyParam,EmptyParam,EmptyParam,EmptyParam, <br>EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) <br><br>7) 设置第2个工作表为活动工作表: <br>ExcelApplication1.WorkSheets[2].Activate; 或 <br>ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate; <br><br>8) 给单元格赋值: <br>ExcelApplication1.Cells[1,4].Value := '第一行第四列'; <br><br>9) 设置指定列的宽度(单位:字符个数),以第一列为例: <br>ExcelApplication1.ActiveSheet.Columns[1].ColumnsWidth := 5; <br><br>10) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例: <br>ExcelApplication1.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米 <br><br>11) 在第8行之前插入分页符: <br>ExcelApplication1.WorkSheets[1].Rows.PageBreak := 1; <br><br>12) 在第8列之前删除分页符: <br>ExcelApplication1.ActiveSheet.Columns[4].PageBreak := 0; <br><br>13) 指定边框线宽度: <br>ExcelApplication1.ActiveSheet.Range[ 'B3
4' ].Borders[2].Weight := 3; <br>1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / ) <br><br>14) 清除第一行第四列单元格公式: <br>ExcelApplication1.ActiveSheet.Cells[1,4].ClearContents; <br><br>15) 设置第一行字体属性: <br>ExcelApplication1.ActiveSheet.Rows[1].Font.Name := '隶书'; <br>ExcelApplication1.ActiveSheet.Rows[1].Font.Color := clBlue; <br>ExcelApplication1.ActiveSheet.Rows[1].Font.Bold := True; <br>ExcelApplication1.ActiveSheet.Rows[1].Font.UnderLine := True; <br><br>16) 进行页面设置: <br>a.页眉: <br>ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '报表演示'; <br>b.页脚: <br>ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '第&P页'; <br>c.页眉到顶端边距2cm: <br>ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; <br>d.页脚到底端边距3cm: <br>ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3/0.035; <br>e.顶边距2cm: <br>ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2/0.035; <br>f.底边距2cm: <br>ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2/0.035; <br>g.左边距2cm: <br>ExcelApplication1.ActiveSheet.PageSetup.LeftMargin := 2/0.035; <br>h.右边距2cm: <br>ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2/0.035; <br>i.页面水平居中: <br>ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035; <br>j.页面垂直居中: <br>ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2/0.035; <br>k.打印单元格网线: <br>ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True; <br><br>17) 拷贝操作: <br><br>a.拷贝整个工作表: <br> ExcelApplication1.ActiveSheet.Used.Range.Copy; <br><br>b.拷贝指定区域: <br> ExcelApplication1.ActiveSheet.Range[ 'A1:E2' ].Copy; <br><br>c.从A1位置开始粘贴: <br> ExcelApplication1.ActiveSheet.Range.[ 'A1' ].PasteSpecial; <br><br>d.从文件尾部开始粘贴: <br> ExcelApplication1.ActiveSheet.Range.PasteSpecial; <br><br>18) 插入一行或一列: <br>a. ExcelApplication1.ActiveSheet.Rows[2].Insert; <br>b. ExcelApplication1.ActiveSheet.Columns[1].Insert; <br><br>19) 删除一行或一列: <br>a. ExcelApplication1.ActiveSheet.Rows[2].Delete; <br>b. ExcelApplication1.ActiveSheet.Columns[1].Delete; <br><br>20) 打印预览工作表: <br> ExcelApplication1.ActiveSheet.PrintPreview; <br><br>21) 打印输出工作表: <br> ExcelApplication1.ActiveSheet.PrintOut; <br><br>22) 工作表保存: <br> if not ExcelApplication1.ActiveWorkBook.Saved then <br> ExcelApplication1.ActiveSheet.PrintPreview; <br><br>23) 工作表另存为: <br> ExcelApplication1.SaveAs( 'C:/Excel/Demo1.xls' ); <br><br>24) 放弃存盘: <br> ExcelApplication1.ActiveWorkBook.Saved := True; <br><br>25) 关闭工作簿: <br> ExcelApplication1.WorkBooks.Close; <br><br>26) 退出 Excel: <br> ExcelApplication1.Quit; <br> ExcelApplication1.Disconnect; <br><br><br><br>(三) 使用Delphi 控制Excle二维图 <br>在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet <br>var asheet1,achart, range:variant; <br><br>1)选择当第一个工作薄第一个工作表 <br>asheet1:=ExcelApplication1.Workbooks[1].Worksheets[1]; <br><br>2)增加一个二维图 <br>achart:=asheet1.chartobjects.add(100,100,200,200); <br><br>3)选择二维图的形态 <br>achart.chart.charttype:=4; <br><br>4)给二维图赋值 <br>series:=achart.chart.seriescollection; <br>range:=sheet1!r2c3:r3c9; <br>series.add(range,true); <br><br>5)加上二维图的标题 <br>achart.Chart.HasTitle:=True; <br>achart.Chart.ChartTitle.Characters.Text:=’ Excle二维图’ <br><br><br>学完这个你就成为excel高手了!^&^ <br><br><br>下面,以Delphi为例,说明这种调用方法。 <br>Unit excel; <br>Interface <br>Uses <br>Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ComObj, <br>{ ComObj是操作OLE对象的函数集} <br>Type <br>TForm1=class(TForm) <br>Button1:TButton; <br>Procedure Button1Click(Sender:Tobject); <br>Private <br>{ Private declaration} <br>Public <br>{ Public declaration } <br>end; <br>var <br>Form1:Tform1; <br>Implementation <br>{$R *.DFM} <br>procedure TForm1.Button1Click(sender:Tobject); <br>var <br>eclApp,WordBook:Variant; {声明为OLE Automation对象} <br>xlsFileName:string; <br>begin <br>xlsFileName:=’ex.xls’; <br>try <br>{创建OLE对象:Excel Application与WordBook} <br>eclApp:=CreateOleObject(‘Excel.Application’); <br>WorkBook:=CreateOleObject(Excel.Sheet’); <br>Except <br>Application.MessageBox(‘你的机器没有安装Microsoft Excel’, <br>’使用Microsoft Excel’,MB_OK+MB_ICONWarning); <br>Exit; <br>End; <br>Try <br>ShowMessage(‘下面演示:新建一个XLS文件,并写入数据,并关闭它。’); <br>WorkBook:=eclApp.workbooks.Add; <br>EclApp.Cells(1,1):=’字符型’; <br>EclApp.Cells(2,1):=’Excel文件’; <br>EclApp.Cells(1,2):=’Money’; <br>EclApp.Cells(2,2):=10.01; <br>EclApp.Cells(1,3):=’日期型’; <br>EclApp.Cells(2,3):=Date; <br>WorkBook.SaveAS(xlsFileName); <br>WorkBook.close; <br>ShowMessage(‘下面演示:打开刚创建的XLS文件,并修改其中的内容,然后,由用户决定是否保存。’); <br>Workbook:=eclApp.WorkBooks.Open(xlsFileName); <br>EclApp.Cells(1,4):=’Excel文件类型’; <br>If MessageDlg(xlsFileName+’已经被修改,是否保存?’, <br>mtConfirmation,[mbYes,mbNo],0)=mrYes then <br>WorkBook.Save <br>Else <br>WorkBook.Saved:=True; {放弃保存} <br>Workbook.Close; <br>EclApp.Quit; //退出Excel Application <br>{释放Variant变量} <br>eclApp:=Unassigned; <br>except <br>showMessage(‘不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。’); <br>WorkBook.close; <br>EclApp.Quit; <br>{释放Variant变量} <br>eclApp:=Unassigned; <br>end; <br>end; <br>end <br><br><br>-------------------------------------------- <br><br>一个操作Excel的单元 <br>这里给出一个Excel的操作单元,函概了部分常用Excel操作,不是我写的,是从Experts-Exchange <br>看到后收藏起来的,给大家参考。 <br>// 该文件操作单元封装了大部分的Excel操作 <br>// use to manipulate Excel xls File <br>// Dragon P.C. &lt;2000.05.10&gt; <br>unit ExcelUnit; <br>interface <br>uses <br>Dialogs, Messages, SysUtils, Grids, Cmp_Sec, ComObj, Ads_Misc; <br>{!~Add a blank WorkSheet} <br>Function ExcelAddWorkSheet(Excel : Variant): Boolean; <br>{!~Close Excel} <br>Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean; <br>{!~Returns the Column String Value from its integer equilavent.} <br>Function ExcelColIntToStr(ColNum: Integer): ShortString; <br>{!~Returns the Column Integer Value from its Alpha equilavent.} <br>Function ExcelColStrToInt(ColStr: ShortString): Integer; <br>{!~Close All Workbooks. All workbooks can be saved or not.} <br>Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean; <br>{!~Copies a range of Excel Cells to a Delphi StringGrid. If successful <br>True is returned, False otherwise. If SizeStringGridToFit is True <br>then the StringGrid is resized to be exactly the correct dimensions to <br>receive the input Excel cells, otherwise the StringGrid is not resized. <br>If ClearStringGridFirst is true then any cells outside the input range <br>are cleared, otherwise existing values are retained. Please not that the <br>Excel cell coordinates are "1" based and the Delphi StringGrid coordinates <br>are zero based.} <br>Function ExcelCopyToStringGrid( <br>Excel : Variant; <br>ExcelFirstRow : Integer; <br>ExcelFirstCol : Integer; <br>ExcelLastRow : Integer; <br>ExcelLastCol : Integer; <br>StringGrid : TStringGrid; <br>StringGridFirstRow : Integer; <br>StringGridFirstCol : Integer; <br>{Make the StringGrid the same size as the input range} <br>SizeStringGridToFit : Boolean; <br>{cells outside input range in StringGrid are cleared} <br>ClearStringGridFirst : Boolean <br>): Boolean; <br>{!~Delete a WorkSheet by Name} <br>Function ExcelDeleteWorkSheet( <br>Excel : Variant; <br>SheetName : ShortString): Boolean; <br>{!~Moves the cursor to the last row and column} <br>Function ExcelEnd(Excel : Variant): Boolean; <br>{!~Finds A value and moves the cursor there. <br>If the value is not found then the cursor does not move. <br>If nothing is found then false is returned, True otherwise.} <br>Function ExcelFind( <br>Excel : Variant; <br>FindString : ShortString): Boolean; <br>{!~Finds A value in a range and moves the cursor there. <br>If the value is not found then the cursor does not move. <br>If nothing is found then false is returned, True otherwise.} <br>Function ExcelFindInRange( <br>Excel : Variant; <br>FindString : ShortString; <br>TopRow : Integer; <br>LeftCol : Integer; <br>LastRow : Integer; <br>LastCol : Integer): Boolean; <br>{!~Finds A value in a range and moves the cursor there. If the value is <br>not found then the cursor does not move. If nothing is found then <br>false is returned, True otherwise. The search directions can be defined. <br>If you want row searches to go from left to right then SearchRight should <br>be set to true, False otherwise. If you want column searches to go from <br>top to bottom then SearchDown should be set to true, false otherwise. <br>If RowsFirst is set to true then all the columns in a complete row will be <br>searched.} <br>Function ExcelFindValue( <br>Excel : Variant; <br>FindString : ShortString; <br>TopRow : Integer; <br>LeftCol : Integer; <br>LastRow : Integer; <br>LastCol : Integer; <br>SearchRight : Boolean; <br>SearchDown : Boolean; <br>RowsFirst : Boolean <br>): Boolean; <br>{!~Returns The First Col} <br>Function ExcelFirstCol(Excel : Variant): Integer; <br>{!~Returns The First Row} <br>Function ExcelFirstRow(Excel : Variant): Integer; <br>{!~Returns the name of the currently active worksheet <br>as a shortstring} <br>Function ExcelGetActiveSheetName(Excel : Variant): ShortString; <br>{!~Gets the formula in a cell.} <br>Function ExcelGetCellFormula( <br>Excel : Variant; <br>RowNum, ColNum: Integer): ShortString; <br>{!~Returns the contents of a cell as a shortstring} <br>Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString; <br>{!~Returns the the current column} <br>Function ExcelGetCol(Excel : Variant): Integer; <br>{!~Returns the the current row} <br>Function ExcelGetRow(Excel : Variant): Integer; <br>{!~Moves the cursor to the last column} <br>Function ExcelGoToLastCol(Excel : Variant): Boolean; <br>{!~Moves the cursor to the last row} <br>Function ExcelGoToLastRow(Excel : Variant): Boolean; <br>{!~Moves the cursor to the Leftmost Column} <br>Function ExcelGoToLeftmostCol(Excel : Variant): Boolean; <br>{!~Moves the cursor to the Top row} <br>Function ExcelGoToTopRow(Excel : Variant): Boolean; <br>{!~Moves the cursor to Home position, i.e., A1} <br>Function ExcelHome(Excel : Variant): Boolean; <br>{!~Returns The Last Column} <br>Function ExcelLastCol(Excel : Variant): Integer; <br>{!~Returns The Last Row} <br>Function ExcelLastRow(Excel : Variant): Integer; <br>{!~Open the file you want to work within Excel. If you want to <br>take advantage of optional parameters then you should use <br>ExcelOpenFileComplex} <br>Function ExcelOpenFile(Excel : Variant; FileName : String): Boolean; <br>{!~Open the file you want to work within Excel. If you want to <br>take advantage of optional parameters then you should use <br>ExcelOpenFileComplex} <br>Function ExcelOpenFileComplex( <br>Excel : Variant; <br>FileName : String; <br>UpdateLinks : Integer; <br>ReadOnly : Boolean; <br>Format : Integer; <br>Password : ShortString): Boolean; <br>{!~Saves the range on the currently active sheet <br>to to values only.} <br>Function ExcelPasteValuesOnly( <br>Excel : Variant; <br>ExcelFirstRow : Integer; <br>ExcelFirstCol : Integer; <br>ExcelLastRow : Integer; <br>ExcelLastCol : Integer): Boolean; <br>{!~Renames a worksheet.} <br>Function ExcelRenameSheet( <br>Excel : Variant; <br>OldName : ShortString; <br>NewName : ShortString): Boolean; <br>{!~Saves the range on the currently active sheet <br>to a DBase 4 table.} <br>Function ExcelSaveAsDBase4( <br>Excel : Variant; <br>ExcelFirstRow : Integer; <br>ExcelFirstCol : Integer; <br>ExcelLastRow : Integer; <br>ExcelLastCol : Integer; <br>OutFilePath : ShortString; <br>OutFileName : ShortString): Boolean; <br>{!~Saves the range on the currently active sheet <br>to a text file.} <br>Function ExcelSaveAsText( <br>Excel : Variant; <br>ExcelFirstRow : Integer; <br>ExcelFirstCol : Integer; <br>ExcelLastRow : Integer; <br>ExcelLastCol : Integer; <br>OutFilePath : ShortString; <br>OutFileName : ShortString): Boolean; <br>{!~Selects a range on the currently active sheet. From the <br>current cursor position a block is selected down and to the right. <br>The block proceeds down until an empty row is encountered. The <br>block proceeds right until an empty column is encountered.} <br>Function ExcelSelectBlock( <br>Excel : Variant; <br>FirstRow : Integer; <br>FirstCol : Integer): Boolean; <br>{!~Selects a range on the currently active sheet. From the <br>current cursor position a block is selected that contains <br>the currently active cell. The block proceeds in each <br>direction until an empty row or column is encountered.} <br>Function ExcelSelectBlockWhole(Excel: Variant): Boolean; <br>{!~Selects a cell on the currently active sheet} <br>Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean; <br>{!~Selects a range on the currently active sheet} <br>Function ExcelSelectRange( <br>Excel : Variant; <br>FirstRow : Integer; <br>FirstCol : Integer; <br>LastRow : Integer; <br>LastCol : Integer): Boolean; <br>{!~Selects an Excel Sheet By Name} <br>Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean; <br>{!~Sets the formula in a cell. Remember to include the equals sign "=". <br>If the function fails False is returned, True otherwise.} <br>Function ExcelSetCellFormula( <br>Excel : Variant; <br>FormulaString : ShortString; <br>RowNum, ColNum: Integer): Boolean; <br>{!~Sets the contents of a cell as a shortstring} <br>Function ExcelSetCellValue( <br>Excel : Variant; <br>RowNum, ColNum: Integer; <br>Value : ShortString): Boolean; <br>{!~Sets a Column Width on the currently active sheet} <br>Function ExcelSetColumnWidth( <br>Excel : Variant; <br>ColNum : Integer; <br>ColumnWidth: Integer): Boolean; <br>{!~Set Excel Visibility} <br>Function ExcelSetVisible( <br>Excel : Variant; <br>IsVisible: Boolean): Boolean; <br>{!~Saves the range on the currently active sheet <br>to values only.} <br>Function ExcelValuesOnly( <br>Excel : Variant; <br>ExcelFirstRow : Integer; <br>ExcelFirstCol : Integer; <br>ExcelLastRow : Integer; <br>ExcelLastCol : Integer): Boolean; <br>{!~Returns the Excel Version as a ShortString.} <br>Function ExcelVersion(Excel: Variant): ShortString; <br>Function IsBlockColSide( <br>Excel : Variant; <br>RowNum: Integer; <br>ColNum: Integer): Boolean; Forward; <br>unction IsBlockRowSide( <br>Excel : Variant; <br>RowNum: Integer; <br>ColNum: Integer): Boolean; Forward; <br> <br>implementation <br> <br><br>type <br>//Declare the constants used by Excel <br>SourceType = (xlConsolidation, xlDatabase, xlExternal, xlPivotTable); <br>Orientation = (xlHidden, xlRowField, xlColumnField, xlPageField, xlDataField); <br>RangeEnd = (NoValue, xlToLeft, xlToRight, xlUp, xlDown); <br>ExcelPasteType = (xlAllExceptBorders,xlNotes,xlFormats,xlValues,xlFormulas,xlAll); <br>{CAUTION!!! THESE OUTPUTS ARE ALL GARBLED! YOU SELECT xlDBF3 AND EXCEL <br>OUTPUTS A xlCSV.} <br>FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2, <br>xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4, <br>xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal, <br>xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS, <br>xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS, <br>xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL); <br>{Add a blank WorkSheet} <br>Function ExcelAddWorkSheet(Excel : Variant): Boolean; <br>Begin <br>Result := True; <br>Try <br>Excel.Worksheets.Add; <br>Except <br>MessageDlg('Unable to add a new worksheet', mtError, [mbOK], 0); <br>Result := False; <br>End; <br>End; <br>{Sets Excel Visibility} <br>Function ExcelSetVisible(Excel : Variant;IsVisible: Boolean): Boolean; <br>Begin <br>Result := True; <br>Try <br>Excel.Visible := IsVisible; <br>Except <br>MessageDlg('Unable to Excel Visibility', mtError, [mbOK], 0); <br>Result := False; <br>End; <br>End; <br>{Close Excel} <br>Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean; <br>Begin <br>Result := True; <br>Try <br>ExcelCloseWorkBooks(Excel, SaveAll); <br>Excel.Quit; <br>Except <br>MessageDlg('Unable to Close Excel', mtError, [mbOK], 0); <br>Result := False; <br>End; <br>End; <br>{Close All Workbooks. All workbooks can be saved or not.} <br>Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean; <br>var <br>loop: byte; <br>Begin <br>Result := True; <br>Try <br>For loop := 1 to Excel.Workbooks.Count Do <br>Excel.Workbooks[1].Close[SaveAll]; <br>Except <br>Result := False; <br>End; <br>End; <br>{Selects an Excel Sheet By Name} <br>Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean; <br>Begin <br>Result := True; <br>Try <br>Excel.Sheets[SheetName].Select; <br>Except <br>Result := False; <br>End; <br>End; <br>{Selects a cell on the currently active sheet} <br>Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean; <br>Begin <br>Result := True; <br>Try <br>Excel.ActiveSheet.Cells[RowNum, ColNum].Select; <br>Except <br>Result := False; <br>End; <br>End; <br>{Returns the contents of a cell as a shortstring} <br>Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString; <br>Begin <br>Result := ''; <br>Try <br>Result := Excel.Cells[RowNum, ColNum].Value; <br>Except <br>Result := ''; <br>End; <br>End; <br>{Returns the the current row} <br>Function ExcelGetRow(Excel : Variant): Integer; <br>Begin <br>Result := 1; <br><br> <br><br><br><br>(一) 使用动态创建的方法 <br>首先创建 Excel 对象,使用ComObj: <br> var ExcelApp: Variant; <br> ExcelApp := CreateOleObject( 'Excel.Application' ); <br>1) 显示当前窗口: <br> ExcelApp.Visible := True; <br>2) 更改 Excel 标题栏: <br> ExcelApp.Caption := '应用程序调用 Microsoft Excel'; <br>3) 添加新工作簿: <br> ExcelApp.WorkBooks.Add; <br>4) 打开已存在的工作簿: <br> ExcelApp.WorkBooks.Open( 'C:/Excel/Demo.xls' ); <br>5) 设置第2个工作表为活动工作表: <br> ExcelApp.WorkSheets[2].Activate; 或 <br> ExcelApp.WorksSheets[ 'Sheet2' ].Activate; <br>6) 给单元格赋值: <br> ExcelApp.Cells[1,4].Value := '第一行第四列'; <br>7) 设置指定列的宽度(单位:字符个数),以第一列为例: <br> ExcelApp.ActiveSheet.Columns[1].ColumnsWidth := 5; <br>8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例: <br> ExcelApp.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米 <br>9) 在第8行之前插入分页符: <br> ExcelApp.WorkSheets[1].Rows[8].PageBreak := 1; <br>10) 在第8列之前删除分页符: <br> ExcelApp.ActiveSheet.Columns[4].PageBreak := 0; <br>11) 指定边框线宽度: <br> ExcelApp.ActiveSheet.Range[ 'B3
4' ].Borders[2].Weight := 3; <br> 1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / ) <br>12) 清除第一行第四列单元格公式: <br> ExcelApp.ActiveSheet.Cells[1,4].ClearContents; <br>13) 设置第一行字体属性: <br> ExcelApp.ActiveSheet.Rows[1].Font.Name := '隶书'; <br> ExcelApp.ActiveSheet.Rows[1].Font.Color := clBlue; <br> ExcelApp.ActiveSheet.Rows[1].Font.Bold := True; <br> ExcelApp.ActiveSheet.Rows[1].Font.UnderLine := True; <br>14) 进行页面设置: <br>a.页眉: <br> ExcelApp.ActiveSheet.PageSetup.CenterHeader := '报表演示'; <br>b.页脚: <br> ExcelApp.ActiveSheet.PageSetup.CenterFooter := '第&P页'; <br>c.页眉到顶端边距2cm: <br> ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; <br>d.页脚到底端边距3cm: <br> ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 3/0.035; <br>e.顶边距2cm: <br> ExcelApp.ActiveSheet.PageSetup.TopMargin := 2/0.035; <br>f.底边距2cm: <br> ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2/0.035; <br>g.左边距2cm: <br> ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2/0.035; <br>h.右边距2cm: <br> ExcelApp.ActiveSheet.PageSetup.RightMargin := 2/0.035; <br>i.页面水平居中: <br> ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035; <br>j.页面垂直居中: <br> ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2/0.035; <br>k.打印单元格网线: <br> ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True; <br>15) 拷贝操作: <br>a.拷贝整个工作表: <br> ExcelApp.ActiveSheet.Used.Range.Copy; <br>b.拷贝指定区域: <br> ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy; <br>c.从A1位置开始粘贴: <br> ExcelApp.ActiveSheet.Range.[ 'A1' ].PasteSpecial; <br>d.从文件尾部开始粘贴: <br> ExcelApp.ActiveSheet.Range.PasteSpecial; <br>16) 插入一行或一列: <br>a. ExcelApp.ActiveSheet.Rows[2].Insert; <br>b. ExcelApp.ActiveSheet.Columns[1].Insert; <br>17) 删除一行或一列: <br>a. ExcelApp.ActiveSheet.Rows[2].Delete; <br>b. ExcelApp.ActiveSheet.Columns[1].Delete; <br>18) 打印预览工作表: <br> ExcelApp.ActiveSheet.PrintPreview; <br>19) 打印输出工作表: <br> ExcelApp.ActiveSheet.PrintOut; <br>20) 工作表保存: <br> if not ExcelApp.ActiveWorkBook.Saved then <br> ExcelApp.ActiveSheet.PrintPreview; <br>21) 工作表另存为: <br> ExcelApp.SaveAs( 'C:/Excel/Demo1.xls' ); <br>22) 放弃存盘: <br> ExcelApp.ActiveWorkBook.Saved := True; <br>23) 关闭工作簿: <br> ExcelApp.WorkBooks.Close; <br>24) 退出 Excel: <br> ExcelApp.Quit; <br><br>(二) 使用Delphi 控件方法 <br> 在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。 <br>1) 打开Excel <br> ExcelApplication1.Connect; <br>2) 显示当前窗口: <br> ExcelApplication1.Visible[0]:=True; <br>3) 更改 Excel 标题栏: <br> ExcelApplication1.Caption := '应用程序调用 Microsoft Excel'; <br>4) 添加新工作簿: <br> ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0)); <br>5) 添加新工作表: <br> var Temp_Worksheet: _WorkSheet; <br> begin <br> Temp_Worksheet:=ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet; <br> ExcelWorkSheet1.ConnectTo(Temp_WorkSheet); <br> End; <br>6) 打开已存在的工作簿: <br> ExcelApplication1.Workbooks.Open (c:/a.xls <br> EmptyParam,EmptyParam,EmptyParam,EmptyParam, <br> EmptyParam,EmptyParam,EmptyParam,EmptyParam, <br> EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) <br>7) 设置第2个工作表为活动工作表: <br> ExcelApplication1.WorkSheets[2].Activate; 或 <br> ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate; <br>8) 给单元格赋值: <br> ExcelApplication1.Cells[1,4].Value := '第一行第四列'; <br>9) 设置指定列的宽度(单位:字符个数),以第一列为例: <br> ExcelApplication1.ActiveSheet.Columns[1].ColumnsWidth := 5; <br>10) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例: <br> ExcelApplication1.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米 <br>11) 在第8行之前插入分页符: <br> ExcelApplication1.WorkSheets[1].Rows[8].PageBreak := 1; <br>12) 在第8列之前删除分页符: <br> ExcelApplication1.ActiveSheet.Columns[4].PageBreak := 0; <br>13) 指定边框线宽度: <br> ExcelApplication1.ActiveSheet.Range[ 'B3
4' ].Borders[2].Weight := 3; <br> 1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / ) <br>14) 清除第一行第四列单元格公式: <br> ExcelApplication1.ActiveSheet.Cells[1,4].ClearContents; <br>15) 设置第一行字体属性: <br> ExcelApplication1.ActiveSheet.Rows[1].Font.Name := '隶书'; <br> ExcelApplication1.ActiveSheet.Rows[1].Font.Color := clBlue; <br> ExcelApplication1.ActiveSheet.Rows[1].Font.Bold := True; <br> ExcelApplication1.ActiveSheet.Rows[1].Font.UnderLine := True; <br>16) 进行页面设置: <br>a.页眉: <br> ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '报表演示'; <br>b.页脚: <br> ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '第&P页'; <br>c.页眉到顶端边距2cm: <br> ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; <br>d.页脚到底端边距3cm: <br> ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3/0.035; <br>e.顶边距2cm: <br> ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2/0.035; <br>f.底边距2cm: <br> ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2/0.035; <br>g.左边距2cm: <br> ExcelApplication1.ActiveSheet.PageSetup.LeftMargin := 2/0.035; <br>h.右边距2cm: <br> ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2/0.035; <br>i.页面水平居中: <br> ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035; <br>j.页面垂直居中: <br> ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2/0.035; <br>k.打印单元格网线: <br> ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True; <br>17) 拷贝操作: <br>a.拷贝整个工作表: <br> ExcelApplication1.ActiveSheet.Used.Range.Copy; <br>b.拷贝指定区域: <br> ExcelApplication1.ActiveSheet.Range[ 'A1:E2' ].Copy; <br>c.从A1位置开始粘贴: <br> ExcelApplication1.ActiveSheet.Range.[ 'A1' ].PasteSpecial; <br><br>d.从文件尾部开始粘贴: <br> ExcelApplication1.ActiveSheet.Range.PasteSpecial; <br>18) 插入一行或一列: <br>a. ExcelApplication1.ActiveSheet.Rows[2].Insert; <br>b. ExcelApplication1.ActiveSheet.Columns[1].Insert; <br>19) 删除一行或一列: <br>a. ExcelApplication1.ActiveSheet.Rows[2].Delete; <br>b. ExcelApplication1.ActiveSheet.Columns[1].Delete; <br>20) 打印预览工作表: <br> ExcelApplication1.ActiveSheet.PrintPreview; <br>21) 打印输出工作表: <br> ExcelApplication1.ActiveSheet.PrintOut; <br>22) 工作表保存: <br> if not ExcelApplication1.ActiveWorkBook.Saved then <br> ExcelApplication1.ActiveSheet.PrintPreview; <br>23) 工作表另存为: <br> ExcelApplication1.SaveAs( 'C:/Excel/Demo1.xls' ); <br>24) 放弃存盘: <br> ExcelApplication1.ActiveWorkBook.Saved := True; <br>25) 关闭工作簿: <br> ExcelApplication1.WorkBooks.Close; <br>26) 退出 Excel: <br> ExcelApplication1.Quit; <br> ExcelApplication1.Disconnect; <br><br>(三) 使用Delphi 控制Excle二维图 <br> 在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet <br> var asheet1,achart, range:variant; <br>1)选择当第一个工作薄第一个工作表 <br> asheet1:=ExcelApplication1.Workbooks[1].Worksheets[1]; <br>2)增加一个二维图 <br> achart:=asheet1.chartobjects.add(100,100,200,200); <br>3)选择二维图的形态 <br> achart.chart.charttype:=4; <br>4)给二维图赋值 <br> series:=achart.chart.seriescollection; <br> range:=sheet1!r2c3:r3c9; <br> series.add(range,true); <br>5)加上二维图的标题 <br> achart.Chart.HasTitle:=True; <br> achart.Chart.ChartTitle.Characters.Text:=’ Excle二维图’ <br>6)改变二维图的标题字体大小 <br> achart.Chart.ChartTitle.Font.size:=6; <br>7)给二维图加下标说明 <br> achart.Chart.Axes(xlCategory, xlPrimary).HasTitle := True; <br> achart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text := '下标说明'; <br>8)给二维图加左标说明 <br> achart.Chart.Axes(xlValue, xlPrimary).HasTitle := True; <br> achart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text := '左标说明'; <br>9)给二维图加右标说明 <br> achart.Chart.Axes(xlValue, xlSecondary).HasTitle := True; <br> achart.Chart.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text := '右标说明'; <br>10)改变二维图的显示区大小 <br> achart.Chart.PlotArea.Left := 5; <br> achart.Chart.PlotArea.Width := 223; <br> achart.Chart.PlotArea.Height := 108; <br>11)给二维图坐标轴加上说明 <br> achart.chart.seriescollection[1].NAME:='坐标轴说明'; <br><br><br> 提供在DELPHI中用程序实现EXCEL单元格合并的源码<br>Begin <br> CapStr:=trim(exApp.Cells[Row,1].value); <br> Col1:=2; <br> Col2:=FldCount; <br> For Col1:=2 to Col2 Do <br> begin <br> NewCapStr:=trim(exApp.Cells[Row,Col1].value); <br> if (NewCapStr=CapStr) then <br> Begin <br> Cell1:=exApp.Cells.Item[Row,Col1-1]; <br> Cell2:=exApp.Cells.Item[Row,Col1]; <br> exApp.Cells[Row,Col1].value:=''; <br> exApp.Range[Cell1,Cell2].Merge(True); <br> end <br> else <br> begin <br> CapStr:=NewCapStr; <br> end; <br> end; <br>end; <br><br> 数据库图片插入到excel中uses:clipbrd <br>var <br>MyFormat:Word; <br>AData:THandle; //临时句柄变量。 <br>APalette:HPALETTE; //临时变量。 <br>Stream1:TMemoryStream;//TBlobStream <br>xx:tbitmap; <br> Stream1:= TMemoryStream.Create; <br> TBlobField(query.FieldByName('存储图片的字段')).SaveToStream(Stream1); <br> Stream1.Position :=0; <br> xx:=tbitmap.Create ; <br> xx.LoadFromStream(Stream1); <br> xx.SaveToClipboardFormat(MyFormat,AData,APalette); <br> ClipBoard.SetAsHandle(MyFormat, AData); <br> myworksheet1.Range['g3','h7'].select;//myworksheet1是当前活动的sheet页 <br> myworksheet1.Paste; <br><br><br> <br> <br>程序中写的一个例子,导出库存到Excel中。 <br>可参看有关Excel操作部分 <br><br>procedure TfrmExcel.StoreToExcel; <br>var <br> data: TADODataSet; <br> ExcelApp, Ra:Variant; <br> row: Integer; <br>begin <br> if not InitExcel(ExcelApp) then <br> exit; <br> data := TADODataSet.Create(nil); <br> data.Connection := ADOConn; <br> try <br> data.CommandText := 'select * from ProInfo'; <br> data.Open; <br> with TADODataSet.Create(nil) do <br> begin <br> Connection := ADOConn; <br> CommandText := 'select ProNO, sum(ProNum) as sNum from AreaProInfo group by ProNO'; <br> Open; <br> row := 1; <br> ExcelApp.Rows[row].RowHeight := 30; <br> Ra := ExcelApp.Range[ExcelApp.Cells[row, 1], ExcelApp.Cells[row, 7]]; <br> Ra.font.size := 18; <br> Ra.font.Bold := true; <br> Ra.MergeCells := true; <br> Ra.HorizontalAlignment := xlcenter; <br> Ra.VerticalAlignment := xlcenter; <br> ExcelApp.Cells[row, 1] := '部件库存情况表'; <br> inc(row); <br> Ra := ExcelApp.Range[ExcelApp.Cells[row, 1], ExcelApp.Cells[row, 7]]; <br> Ra.font.size := 10; <br> Ra.HorizontalAlignment := xlRight; <br> Ra.VerticalAlignment := xlcenter; <br> Ra.MergeCells := true; <br> ExcelApp.Cells[row, 1] := FormatDateTime('yyyy-mm-dd', Now); <br><br> inc(row); <br> ExcelApp.Cells[row, 1] := '部件编号'; <br> ExcelApp.Cells[row, 2] := '部件名称'; <br> ExcelApp.Columns[2].ColumnWidth := 15; <br> ExcelApp.Cells[row, 3] := '单位'; <br> ExcelApp.Columns[3].ColumnWidth := 4; <br> ExcelApp.Cells[row, 4] := '型号规格'; <br> ExcelApp.Columns[4].ColumnWidth := 20; <br> ExcelApp.Cells[row, 5] := '部件单价'; <br> ExcelApp.Cells[row, 6] := '库存数量'; <br> ExcelApp.Cells[row, 7] := '库存金额'; <br> while not Eof do <br> begin <br> if data.Locate('ProNO', FieldByName('ProNO').AsString, []) then <br> begin <br> inc(row); <br> ExcelApp.Cells[row, 1] := FieldByName('ProNO').AsString; <br> ExcelApp.Cells[row, 2] := data.FieldByName('ProName').AsString; <br> ExcelApp.Cells[row, 3] := data.FieldByName('ProUnit').AsString; <br> ExcelApp.Cells[row, 4] := data.FieldByName('ProKind').AsString; <br> ExcelApp.Cells[row, 5] := data.FieldByName('ProMoney').AsString; <br> ExcelApp.Cells[row, 6] := FieldByName('sNum').Value; <br> ExcelApp.Cells[row, 7] := '=E' + IntToStr(row) + '*F' + IntToStr(row); <br> end; <br> Next; <br>{ if RecNO = 10 then <br> Break;} <br> ProgressBar.Position := RecNO * 100 div RecordCount; <br> Show; <br> end; <br> ExcelApp.Cells[row + 1, 2] := '合计'; <br> ExcelApp.Cells[row + 1, 7] := '=SUM(G2:G' + IntToStr(Row); <br> Free; <br> end; <br> finally <br> data.Free; <br> ExcelApp.ScreenUpdating := true; <br> end; <br>end; <br><br>function TfrmExcel.InitExcel(var excel: Variant): Boolean; <br>begin <br> try <br> excel := CreateOleObject('Excel.Application'); <br> except <br> result := false; <br> showMsg('调用Excel出错!'); <br> exit; <br> end; <br><br> excel.WorkBooks.Add; <br> excel.WorkSheets[1].Activate; <br> excel.Visible := true; <br> excel.ScreenUpdating := false; <br> excel.Rows.RowHeight := 18; <br> excel.ActiveSheet.PageSetup.PrintGridLines := false; <br> result := true; <br>end;