Delphi操作Excel大全12007-11-24 16:23Delphi 控制Excel<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; 或 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.PageBreak := 1;<br>10) 在第8列之前删除分页符: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) 设置第一行字体属性: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.拷贝整个工作表: ExcelApp.ActiveSheet.Used.Range.Copy;<br>b.拷贝指定区域: ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy;<br>c.从A1位置开始粘贴: ExcelApp.ActiveSheet.Range.[ 'A1' ].PasteSpecial;<br>d.从文件尾部开始粘贴: 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>(二) 使用Delphi 控件方法<br>在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。<br>正在装载数据……<br><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.<br>WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;<br>ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);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.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>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><br>对不起我还需要一个锁定功能啊,就是输出到EXCEL后只能看,不能进行手工修改<br><br>Xl.Cells.Select;//Select All Cells<br>Xl.Selection.Locked = True;// Lock Selected Cells<br>//Xl:=CreateOleObject('Excel.Application');<br>________________________________________<br><br>procedure TForm1.BitBtn4Click(Sender: TObject);<br>var<br>ExcelApp, Sheet: Variant;<br>begin<br>if OpenDialog1.Execute then<br>begin<br> ExcelApp := CreateOleObject( 'Excel.Application' 
;<br> ExcelApp.Workbooks.Open(OpenDialog1.FileName);<br> Sheet := ExcelApp.ActiveSheet;<br> Caption := 'Row Count: ' + IntToStr(Sheet.UsedRange.Rows.Count);<br> ExcelApp.Quit;<br> Sheet := Unassigned;<br> ExcelApp := Unassigned;<br>end;<br>end;<br>________________________________________<br><br>procedure CopyDbDataToExcel(Target: TDbgrid);<br>var<br>iCount, jCount: Integer;<br>XLApp: Variant;<br>Sheet: Variant;<br>begin<br>Screen.Cursor := crHourGlass;<br>if not VarIsEmpty(XLApp) then<br>begin<br> XLApp.DisplayAlerts := False;<br> XLApp.Quit;<br> VarClear(XLApp);<br>end;<br>//通过ole创建Excel对象<br>try<br> XLApp := CreateOleObject('Excel.Application');<br>except<br> Screen.Cursor := crDefault;<br> Exit;<br>end;<br>XLApp.WorkBooks.Add[XLWBatWorksheet];<br>XLApp.WorkBooks[1].WorkSheets[1].Name := '测试工作薄';<br>Sheet := XLApp.Workbooks[1].WorkSheets['测试工作薄'];<br>if not Target.DataSource.DataSet.Active then<br>begin<br> Screen.Cursor := crDefault;<br> Exit;<br>end;<br>Target.DataSource.DataSet.first;<br>for iCount := 0 to Target.Columns.Count - 1 do<br>begin<br> Sheet.cells[1, iCount + 1] := Target.Columns.Items[iCount].Title.Caption;<br>end;<br>jCount := 1;<br>while not Target.DataSource.DataSet.Eof do<br>begin<br> for iCount := 0 to Target.Columns.Count - 1 do<br> begin<br> Sheet.cells[jCount + 1, iCount + 1] := Target.Columns.Items[iCount].Field.AsString;<br> end;<br> Inc(jCount);<br> Target.DataSource.DataSet.Next;<br>end;<br>XlApp.Visible := True;<br>Screen.Cursor := crDefault;<br>end;<br><br>看看我的函数<br>function ExportToExcel(Header: String;<br>vDataSet: TDataSet): Boolean;<br>var<br>I,VL_I,j: integer;<br>S,SysPath: string;<br>MsExcel:Variant;<br>begin<br>Result:=true;<br>if Application.MessageBox('您确信将数据导入到Excel吗?','提示!',MB_OKCANCEL + MB_DEFBUTTON1) = IDOK then<br>begin<br> SysPath:=ExtractFilePath(application.exename);<br> with TStringList.Create do<br> try<br> vDataSet.First ;<br> S:=S+Header;<br> // system.Delete(s,1,1);<br> add(s);<br> s:=';<br> For I:=0 to vDataSet.fieldcount-1 do<br> begin<br> If vDataSet.fields
.visible=true then<br> S:=S+#9+vDataSet.fields.displaylabel;<br> end;<br> system.Delete(s,1,1);<br> add(s);<br> while not vDataSet.Eof do<br> begin<br> S := ';<br> for I := 0 to vDataSet.FieldCount -1 do<br> begin<br> If vDataSet.fields.visible=true then<br> S := S + #9 + vDataSet.Fields.AsString;<br> end;<br> System.Delete(S, 1, 1);<br> Add(S);<br> vDataSet.Next;<br> end;<br> Try<br> SaveToFile(SysPath+'/Tem.xls');<br> Except<br> ShowMessage('写文件时发生保护性错误,Excel 如在运行,请先关闭!');<br> Result:=false;<br> exit;<br> end;<br> finally<br> Free;<br> end;<br> Try<br> MSExcel:=CreateOleObject('Excel.Application');<br> Except<br> ShowMessage('Excel 没有安装,请先安装!');<br> Result:=false;<br> exit;<br> end;<br> Try<br> MSExcel.workbooks.open(SysPath+'/Tem.xls');<br> Except<br> ShowMessage('打开临时文件时出错,请检查'+SysPath+'/Tem.xls');<br> Result:=false;<br> exit;<br> end;<br> MSExcel.visible:=True;<br> for VL_I :=1 to 4 do<br> MSExcel.Selection.Borders[VL_I].LineStyle := 0;<br> MSExcel.cells.select;<br> MSExcel.Selection.HorizontalAlignment :=3;<br> MSExcel.Selection.Borders[1].LineStyle := 0;<br> MSExcel.Range['A1'].Select;<br> MSExcel.Selection.Font.Size :=24;<br> J:=0 ;<br> for i:=0 to vdataset.fieldcount-1 do<br> if vDataSet.fields.visible then<br> J:=J+1;<br> VL_I :=J;<br> MSExcel.Range['A1:'+F_ColumnName(VL_I)+'1'].Select;<br> MSExcel.Range['A1:'+F_ColumnName(VL_I)+'1'].Merge;<br>end<br>else<br> Result:=false;<br>end;<br><br>转别人的组件<br>unit OleExcel;<br>interface<br>uses<br>Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>comobj, DBTables, Grids;<br>type<br>TOLEExcel = class(TComponent)<br>private<br> FExcelCreated: Boolean;<br> FVisible: Boolean;<br> FExcel: Variant;<br> FWorkBook: Variant;<br> FWorkSheet: Variant;<br> FCellFont: TFont;<br> FTitleFont: TFont;<br> FFontChanged: Boolean;<br> FIgnoreFont: Boolean;<br> FFileName: TFileName;<br> procedure SetExcelCellFont(var Cell: Variant);<br> procedure SetExcelTitleFont(var Cell: Variant);<br> procedure GetTableColumnName(const Table: TTable; var Cell: Variant);<br> procedure GetQueryColumnName(const Query: TQuery; var Cell: Variant);<br> procedure GetFixedCols(const StringGrid: TStringGrid; var Cell: Variant);<br> procedure GetFixedRows(const StringGrid: TStringGrid; var Cell: Variant);<br> procedure GetStringGridBody(const StringGrid: TStringGrid; var Cell: Variant);<br>protected<br> procedure SetCellFont(NewFont: TFont);<br> procedure SetTitleFont(NewFont: TFont);<br> procedure SetVisible(DoShow: Boolean);<br> function GetCell(ACol, ARow: Integer): string;<br> procedure SetCell(ACol, ARow: Integer; const Value: string);<br> function GetDateCell(ACol, ARow: Integer): TDateTime;<br> procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime);<br>public<br> constructor Create(AOwner: TComponent); override;<br> destructor Destroy; override;<br> procedure CreateExcelInstance;<br> property Cell[ACol, ARow: Integer]: string read GetCell write SetCell;<br> property DateCell[ACol, ARow: Integer]: TDateTime read GetDateCell write SetDateCell;<br> function IsCreated: Boolean;<br> procedure TableToExcel(const Table: TTable);<br> procedure QueryToExcel(const Query: TQuery);<br> procedure StringGridToExcel(const StringGrid: TStringGrid);<br> procedure SaveToExcel(const FileName: string);<br>published<br> property TitleFont: TFont read FTitleFont write SetTitleFont;<br> property CellFont: TFont read FCellFont write SetCellFont;<br> property Visible: Boolean read FVisible write SetVisible;<br> property IgnoreFont: Boolean read FIgnoreFont write FIgnoreFont;<br> property FileName: TFileName read FFileName write FFileName;<br>end;<br>procedure Register;<br>implementation<br>constructor TOLEExcel.Create(AOwner: TComponent);<br>begin<br>inherited Create(AOwner);<br>FIgnoreFont := True;<br>FCellFont := TFont.Create;<br>FTitleFont := TFont.Create;<br>FExcelCreated := False;<br>FVisible := False;<br>FFontChanged := False;<br>end;<br>destructor TOLEExcel.Destroy;<br>begin<br>FCellFont.Free;<br>FTitleFont.Free;<br>inherited Destroy;<br>end;<br>procedure TOLEExcel.SetExcelCellFont(var Cell: Variant);<br>begin<br>if FIgnoreFont then exit;<br>with FCellFont do<br> begin<br> Cell.Font.Name := Name;<br> Cell.Font.Size := Size;<br> Cell.Font.Color := Color;<br> Cell.Font.Bold := fsBold in Style;<br> Cell.Font.Italic := fsItalic in Style;<br> Cell.Font.UnderLine := fsUnderline in Style;<br> Cell.Font.Strikethrough := fsStrikeout in Style;<br> end;<br>end;<br>procedure TOLEExcel.SetExcelTitleFont(var Cell: Variant);<br>begin<br>if FIgnoreFont then exit;<br>with FTitleFont do<br> begin<br> Cell.Font.Name := Name;<br> Cell.Font.Size := Size;<br> Cell.Font.Color := Color;<br> Cell.Font.Bold := fsBold in Style;<br> Cell.Font.Italic := fsItalic in Style;<br> Cell.Font.UnderLine := fsUnderline in Style;<br> Cell.Font.Strikethrough := fsStrikeout in Style;<br> end;<br>end;<br><br>procedure TOLEExcel.SetVisible(DoShow: Boolean);<br>begin<br>if not FExcelCreated then exit;<br>if DoShow then<br> FExcel.Visible := True<br>else<br> FExcel.Visible := False;<br>end;<br>function TOLEExcel.GetCell(ACol, ARow: Integer): string;<br>begin<br>if not FExcelCreated then exit;<br>result := FWorkSheet.Cells[ARow, ACol];<br>end;<br>procedure TOLEExcel.SetCell(ACol, ARow: Integer; const Value: string);<br>var<br>Cell: Variant;<br>begin<br>if not FExcelCreated then exit;<br>Cell := FWorkSheet.Cells[ARow, ACol];<br>SetExcelCellFont(Cell);<br>Cell.Value := Value;<br>end;<br><br>function TOLEExcel.GetDateCell(ACol, ARow: Integer): TDateTime;<br>begin<br>if not FExcelCreated then<br> begin<br> result := 0;<br> exit;<br> end;<br>result := StrToDateTime(FWorkSheet.Cells[ARow, ACol]);<br>end;<br>procedure TOLEExcel.SetDateCell(ACol, ARow: Integer; const Value: TDateTime);<br>var<br>Cell: Variant;<br>begin<br>if not FExcelCreated then exit;<br>Cell := FWorkSheet.Cells[ARow, ACol];<br>SetExcelCellFont(Cell);<br>Cell.Value := '' + DateTimeToStr(Value);<br>end;<br>procedure TOLEExcel.CreateExcelInstance;<br>begin<br>try<br> FExcel := CreateOLEObject('Excel.Application');<br> FWorkBook := FExcel.WorkBooks.Add;<br> FWorkSheet := FWorkBook.WorkSheets.Add;<br> FExcelCreated := True;<br>except<br> FExcelCreated := False;<br>end;<br>end;<br>function TOLEExcel.IsCreated: Boolean;<br>begin<br>result := FExcelCreated;<br>end;<br>procedure TOLEExcel.SetTitleFont(NewFont: TFont);<br>begin<br>if NewFont <> FTitleFont then<br> FTitleFont.Assign(NewFont);<br>end;<br>procedure TOLEExcel.SetCellFont(NewFont: TFont);<br>begin<br>if NewFont <> FCellFont then<br> FCellFont.Assign(NewFont);<br>end;<br>procedure TOLEExcel.GetTableColumnName(const Table: TTable; var Cell: Variant);<br>var<br>Col: integer;<br>begin<br>for Col := 0 to Table.FieldCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[1, Col + 1];<br> SetExcelTitleFont(Cell);<br> Cell.Value := Table.Fields[Col].FieldName;<br> end;<br>end;<br>procedure TOLEExcel.TableToExcel(const Table: TTable);<br>var<br>Col, Row: LongInt;<br>Cell: Variant;<br>begin<br>if not FExcelCreated then exit;<br>if Table.Active = False then exit;<br>GetTableColumnName(Table, Cell);<br>Row := 2;<br>with Table do<br> begin<br> first;<br> while not EOF do<br> begin<br> for Col := 0 to FieldCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[Row, Col + 1];<br> SetExcelCellFont(Cell);<br> Cell.Value := Fields[Col].AsString;<br> end;<br> next;<br> Inc(Row);<br> end;<br> end;<br>end;<br><br>procedure TOLEExcel.GetQueryColumnName(const Query: TQuery; var Cell: Variant);<br>var<br>Col: integer;<br>begin<br>for Col := 0 to Query.FieldCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[1, Col + 1];<br> SetExcelTitleFont(Cell);<br> Cell.Value := Query.Fields[Col].FieldName;<br> end;<br>end;<br><br>procedure TOLEExcel.QueryToExcel(const Query: TQuery);<br>var<br>Col, Row: LongInt;<br>Cell: Variant;<br>begin<br>if not FExcelCreated then exit;<br>if Query.Active = False then exit;<br>GetQueryColumnName(Query, Cell);<br>Row := 2;<br>with Query do<br> begin<br> first;<br> while not EOF do<br> begin<br> for Col := 0 to FieldCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[Row, Col + 1];<br> SetExcelCellFont(Cell);<br> Cell.Value := Fields[Col].AsString;<br> end;<br> next;<br> Inc(Row);<br> end;<br> end;<br>end;<br>procedure TOLEExcel.GetFixedCols(const StringGrid: TStringGrid; var Cell: Variant);<br>var<br>Col, Row: LongInt;<br>begin<br>for Col := 0 to StringGrid.FixedCols - 1 do<br> for Row := 0 to StringGrid.RowCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[Row + 1, Col + 1];<br> SetExcelTitleFont(Cell);<br> Cell.Value := StringGrid.Cells[Col, Row];<br> end;<br>end;<br>procedure TOLEExcel.GetFixedRows(const StringGrid: TStringGrid; var Cell: Variant);<br>var<br>Col, Row: LongInt;<br>begin<br>for Row := 0 to StringGrid.FixedRows - 1 do<br> for Col := 0 to StringGrid.ColCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[Row + 1, Col + 1];<br> SetExcelTitleFont(Cell);<br> Cell.Value := StringGrid.Cells[Col, Row];<br> end;<br>end;<br>procedure TOLEExcel.GetStringGridBody(const StringGrid: TStringGrid; var Cell: Variant);<br>var<br>Col, Row, x, y: LongInt;<br>begin<br>Col := StringGrid.FixedCols;<br>Row := StringGrid.FixedRows;<br>for x := Row to StringGrid.RowCount - 1 do<br> for y := Col to StringGrid.ColCount - 1 do<br> begin<br> Cell := FWorkSheet.Cells[x + 1, y + 1];<br> SetExcelCellFont(Cell);<br> Cell.Value := StringGrid.Cells[y, x];<br> end;<br>end;<br>procedure TOLEExcel.StringGridToExcel(const StringGrid: TStringGrid);<br>var<br>Cell: Variant;<br>begin<br>if not FExcelCreated then exit;<br>GetFixedCols(StringGrid, Cell);<br>GetFixedRows(StringGrid, Cell);<br>GetStringGridBody(StringGrid, Cell);<br>end;<br>procedure TOLEExcel.SaveToExcel(const FileName: string);<br>begin<br>if not FExcelCreated then exit;<br>FWorkSheet.SaveAs(FileName);<br>end;<br>procedure Register;<br>begin<br>RegisterComponents('Tanglu', [TOLEExcel]);<br>end;<br>end.<br>----------------------------------------------