Two methods, one is to connect with ADO, the problem is the content of the Excel file to rule, and the second is open with OLE, but the operation is not like
Operation of the database is so convenient.
First, connect with ADO:
Set Property connetionstring
Select Microsoft Jet 4.0 OLE DB Provider
Select or enter a datasorce name, choose the Excel file you want to open
User name default is Admin password default is empty, can ignore
Extended properties set to: Excel 8.0
SQL statement SELECT * FROM [Yourtablename] (note to have [])
Second, open with OLE (here is an example, commented out code is also useful statement, note to uses Extctrls,comobj unit):
var excelapp:variant;
Begin
Excelapp:=createoleobject (' Excel.Application ');
Excelapp.visible:=true;
excelapp.caption:= ' application calls Microsoft Excel ';
EXCELAPP.WORKBOOKS.ADD; New Workbook
ExcelApp.workBooks.Open (' C:\My documents\ca09lin1.xls '); Open a workbook that already exists
Excelapp.worksheets[2].activate; Open a 2nd worksheet
Excelapp.worksheets[' The fourth chapter '].activate; Open a worksheet called the fourth chapter
excelapp.cells[1,4]. Value:= ' first row fourth column ';
excelapp.cells[1,5]. Value:= ' first row fifth column ';
EXCELAPP.ACTIVESHEET.COLUMNS[4]. columnwidth:=15;
EXCELAPP.ACTIVESHEET.ROWS[1]. rowheight:=15;
EXCELAPP.WORKSHEETS[1]. ROWS[8]. Pagebreak:=1; Set page breaks, but appears to be invalid
EXCELAPP.ACTIVESHEET.ROWS[8]. Pagebreak:=1; Ditto
excelapp.activesheet.range[' B3:d4 '. BORDERS[2]. weight:=3;
excelapp.activesheet.range[' B3:d4 '. BORDERS[1]. weight:=3;
excelapp.activesheet.range[' B3:d4 '. BORDERS[3]. weight:=3;
excelapp.activesheet.range[' B3:d4 '. BORDERS[4]. weight:=3;
excelapp.activesheet.range[' B3:d4 '. BORDERS[5]. weight:=3; A slash is added directly within the range of cells |
excelapp.activesheet.range[' B3:d4 '. BORDERS[6]. weight:=3; Similar to the previous sentence
Bordrs:1-left 2-right 3-top 4-bottom 5-oblique (\) 6-oblique (/)
excelapp.cells[3,2]. Value:= ' Three rows two columns ';
excelapp.cells[3,3]. Value:= ' Three rows three columns ';
excelapp.cells[3,4]. Value:= ' Three rows four columns ';
excelapp.cells[4,2]. Value:= ' Four rows two columns ';
excelapp.cells[4,3]. Value:= ' Four rows three columns ';
excelapp.cells[4,4]. Value:= ' Four rows four columns ';
excelapp.activesheet.range[' B3:d4 '. Value.copytoclipboard;
excelapp.activesheet.cells[1,4]. clearcontents; Clears the contents of a row of four columns, ActiveSheet can omit
Excelapp.rows[3].font. name:= ' official script '; ActiveSheet is omitted here, but only for the current sheet and not for the entire workbook.
Excelapp.rows[3].font. Color:=clblue;
EXCELAPP.ROWS[3]. Font.bold:=true;
EXCELAPP.ROWS[3]. Font.underline:=true;
excelapp.range[' B3:d4 '. Copy;
Richedit1.pastefromclipboard;
excelapp.activesheet.pagesetup.centerfooter:= ' page $p ';
All page setups (PageSetup properties) are not allowed, and I don't know why
ExcelApp.ActiveSheet.PrintPreview; Print Preview
ExcelApp.ActiveSheet.PrintOut; Direct Print output
If not ExcelApp.ActiveWorkBook.Saved then//worksheet Save:
ExcelApp.ActiveSheet.PrintPreview;
Excelapp.saveas (' C:\Excel\Demo1.xls '); Save Worksheet as
ExcelApp.ActiveWorkBook.Saved: = True; Discard disk
ExcelApp.WorkBooks.Close; Close Workbook
Excelapp.quit; Exit Excel
excelapp:=unassigned;//releasing the Excel process
End
Other:
To get the number of rows and columns of Excel:
MAXC: =exlapp.worksheets[1]. UsedRange.Columns.Count;
MAXR: =exlapp.worksheets[1]. UsedRange.Rows.Count;
Get column widths
A:=createoleobject (' Excel.Application ');
A.workbooks.add;
a.activecell.columnwidth:=10;
ShowMessage (IntToStr (a.activecell.columnwidth));
Delphi Read and write Excel