用ole读excel:会读了后,你自己处理一下要读的数据就行了
var
I, J: Integer;
MaxRow, MaxCol: Integer;
List, Strs: TStringList;
ExcelApp, Sheet: Variant;
OldTime: TDateTime;
begin
List := TStringList.Create;
Strs := TStringList.Create;
// 创建一个excel的ole对象
ExcelApp := CreateOleObject( "Excel.Application ");
try
// 打开一个excel文件
ExcelApp.WorkBooks.Open(Edit1.Text);
List.BeginUpdate;
try
// 设置工作区
ExcelApp.WorkSheets[1].Activate;
Sheet := ExcelApp.WorkSheets[1];
// 有数据的区域的行数和列数
MaxRow := Sheet.UsedRange.Rows.count - 1;
MaxCol := Sheet.UsedRange.Columns.count;
for I := 2 to MaxRow do
begin
Strs.Clear;
for J := 1 to MaxCol do
begin
// 获得excel的数据第i行,第j列单元格内的数据
Strs.Add(Sheet.Cells[i, j].Value);
end;
List.Add(Strs.CommaText);
end;
finally
// 关闭工作区
ExcelApp.WorkBooks.Close;
List.EndUpdate;
end;
finally
// 释放ole对象
ExcelApp.Quit;
List.Free;
Strs.Free;
end;
end;
Delphi控制Excel的方法
1 创建Excel文件
要在Delphi中控制Excel,就必须用到OLE自动化。现在一般采用OLE2来创建OLE对象,当激活一个OLE对象时,服务器程序仅在容器程序内部激活,这就是所谓的“就地激活”(in-place activation)。
创建Excel文件时,先创建一个OLE对象,然后在对象中建立工作表worksheet,如函数createExcel所示:
function createExcel:variant; |
2 数据表格控制
Excel表格的控制,主要包括数据的导入、修改;单元格的合并、边框的控制;表格的复制、粘贴等。当报表格式一定的情况下,表格的复制、粘贴显得尤为重要,这样,可以先制作一个文件模板,然后按照实际需要输出多页报表即可。
(1)数据的导入(importData)
procedure importData; |
(2)单元格的合并、边框的控制(lineStylecontrol)
单元格的合并,是在选定合并范围的情况下进行的。边框控制可以操作边框线条的是否显示。其他方式的控制,可以仿照下面过程进行。
procedure lineStylecontrol; |
(3)表格的复制与粘贴(copyandPaste)
procedure copyandPaste; |
3 文件的保存
文件保存是在创建文件的基础上进行的,过程saveFile说明了文件保存过程中应该注意的问题:
procedure saveFile; |
Uses ExtCtrls,ComObj单元
var ExcelApp:Variant;
begin
ExcelApp:=CreateOleObject('Excel.Application');
//ExcelApp.visible:=true;
ExcelApp.Caption:='应用程序调用 Microsoft Excel';
ExcelApp.WorkBooks.Add; //新增工作簿
//ExcelApp.workBooks.Open('C:\My Documents\Ca09lin1.xls'); //打开已存在工作簿
ExcelApp.Worksheets[2].activate; //打开第2个工作表
//ExcelApp.WorkSheets['第四章'].activate; //打开名为第四章的工作表
ExcelApp.Cells[1,4].Value:='第一行第四列';
ExcelApp.Cells[1,5].Value:='第一行第五列';
ExcelApp.ActiveSheet.Columns[4].ColumnWidth:=15;
ExcelApp.ActiveSheet.Rows[1].RowHeight:=15;
//ExcelApp.WorkSheets[1].Rows[8].PageBreak:=1; //设置分页符,但似无效
//Excelapp.ActiveSheet.Rows[8].PageBreak:=1; //同上
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; //会直接在范围内的各Cell内加上斜杠|
//ExcelApp.ActiveSheet.Range['B3:D4'].Borders[6].Weight:=3; //与上句类似
//Bordrs:1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
ExcelApp.Cells[3,2].Value:='三行二列';
ExcelApp.Cells[3,3].Value:='三行三列';
ExcelApp.Cells[3,4].Value:='三行四列';
ExcelApp.Cells[4,2].Value:='四行二列';
ExcelApp.Cells[4,3].Value:='四行三列';
ExcelApp.Cells[4,4].Value:='四行四列';
//ExcelApp.ActiveSheet.Range['B3:D4'].Value.CopyToClipBoard;
ExcelApp.activeSheet.Cells[1,4].ClearContents; //清除一行四列的内容,activeSheet可以省略
Excelapp.Rows[3].font.Name:='隶书'; //这里Rows前省略了activeSheet,但针对也只是当前工作表而非整个工作簿
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:='第$P页';
//所有页面设置(PageSetup的属性)都不能进行,不知为何
//ExcelApp.ActiveSheet.PrintPreview; //打印预览
//ExcelApp.ActiveSheet.PrintOut; //直接打印输出
//if not ExcelApp.ActiveWorkBook.Saved then //工作表保存:
// ExcelApp.ActiveSheet.PrintPreview;
//ExcelApp.SaveAs( 'C:\Excel\Demo1.xls' ); //工作表另存为
ExcelApp.ActiveWorkBook.Saved := True; // 放弃存盘
ExcelApp.WorkBooks.Close; //关闭工作簿
ExcelApp.Quit; //退出 Excel
ExcelApp:=Unassigned;//释放excel进程
end;
另:
得到excel的行数、列数:
Maxc :=ExlApp.WorkSheets[1].UsedRange.Columns.Count;
Maxr :=ExlApp.WorkSheets[1].UsedRange.Rows.Count;
得到列宽
a:=createoleobject('excel.application');
a.workbooks.add;
a.activecell.columnwidth:=10;
showmessage(inttostr(a.activecell.columnwidth));
procedure TForm1.Button1Click(Sender: TObject);
var excelx,excely : string;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定
//excel档路径
ExcelApp.Visible := false; ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
for i := 1 to excelrowcount 1 do
begin
excelx := excelapp.Cells[i,1].Value;
excely := excelapp.Cells[i,2].Value;
if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据
exit
else
with query1 do
begin
close;
sql.clear;
sql.add(insert into test(name,address) values(:name,:address));
parambyname('name').asstring := excelx;//excel档的第一列插入到test表的 name栏位;
parambyname('address').asstring := excely;//excel档的第二列插入到test表的 address 栏位;
execsql;
end;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;
end;
end;