怎么把excel数据导入到delphi中来

landland 2003-08-25 04:05:05
怎么把excel数据导入到delphi中来
...全文
131 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mib3000 2003-10-21
  • 打赏
  • 举报
回复
mark
ming709 2003-09-07
  • 打赏
  • 举报
回复
up
liushiboy 2003-08-26
  • 打赏
  • 举报
回复
用ADO或者OLE都行..
ADO中的话,查询语句为:
select * from [sheet1$]
连接字符串为:
DRIVER='{Microsoft EXCEL Driver (*.xls)};DBQ='+FileName+';Extended Properties=EXCEL 8.0'
ld9702 2003-08-26
  • 打赏
  • 举报
回复
uses OleServer,Excel2000,shellapi,comobj
procedure TReport_Frm.S_openClick(Sender: TObject);
var
filename:string;
xl,ExcelApp,WorkBook,Sheet: oleVariant;
x,y:integer;
begin
inherited;
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName)+'报表管理';
OpenDialog1.Execute;
filename:=OpenDialog1.FileName;
if length(filename)=0 then
begin
Mymessagebox(' 没有选择历史统计文件的存储位置,不能导入历史统计文件的数据! ',1,2);
exit;
end;
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
ExcelApp.WorkBooks.Open(filename);
ExcelApp.WorkSheets[1].Activate;
except
Mymessagebox(' 您的机器里未安装Microsoft Excel没有该文件,不能导入Excel报表! ',1,2);
exit;
end;
if ExcelApp.Cells[1,1].Value='流量表' then
begin
label1.Caption:=ExcelApp.Cells[2,1].Value;
sa_grid.RowCount:=30;
for y:=1 to sa_grid.RowCount-1 do
begin
for x:=1 to 7 do
begin
sa_grid.Cells[x,y]:=ExcelApp.Cells[y+3,x].Value;
end;
end;
end else begin
Mymessagebox(' 该Excel文件不是本系统导出的统计文件,不能导入本系统! ',1,2);
exit;
end;
WorkBook.Close;
ExcelApp.Quit;
end;
IORILI 2003-08-25
  • 打赏
  • 举报
回复
MyExcel:=createoleobject('Excel.Application');
MyWorkbook:=MyExcel.Workbooks.Open(datamodule2.OpenDialog1.FileName);
MyExcel.Visible:=false;
myworksheet1:=myexcel.activesheet;
for bom_cell:=1 to myworksheet1.usedrange.columns.Count do
begin
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='类别' then
fuxiang_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='产品编码' then
type_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='型号' then
name_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='出口国家' then
guige_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='周计划' then //材料
cailiao_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='上次欠产' then
danwei_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='单位' then
danhao_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='当日' then
gongxu_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='累计' then
plat_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='备注' then
danhao_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='欠产' then
gongxu_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='分厂名称' then
plat_field:=bom_cell;
if varastype(myworksheet1.cells.item[1,bom_cell],varstring)='数据日期' then
plat_field:=bom_cell;
end;
//执行存储过程
dm.adoq1.Close;
dm.adoq1.SQL.Clear;
dm.adoq1.SQL.add('');
dm.adoq1.ExecSQL;
try For bom_cell:=2 To myworksheet1.usedrange.Rows.Count do
begin
dm.ADOQ1.Close;
dm.ADOQ1.SQL.Clear;
上面说明了怎么取excel中的数据
上海老李 2003-08-25
  • 打赏
  • 举报
回复
unit toexcel;

interface


uses
Windows, Variants, Classes, controls,DB,DBTables, OleServer,Excel2000;

procedure ToExce(table1:Tdataset);

implementation

procedure ToExce(table1:Tdataset);
var
WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
XLapp:TexcelApplication;
begin
XLapp:=TexcelApplication.create(nil);
IIndex := 1;
R := table1.RecordCount;
C := table1.FieldCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);

I := 0;
// Define the loop for filling in the Variant
table1.First;
while not table1.Eof do
begin
for J := 0 to (C - 1) do
TabGrid[I,J] := table1.Fields[J].Asstring ;
Inc(I,1);
table1.Next ;
end;

// Connect to the server TExcelApplication
XLApp.Connect;
// Add WorkBooks to the ExcelApplication
XLApp.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := XLApp.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value := TabGrid;
// Customise the WorkSheet
WorkSheet.Name := 'Customers';
Worksheet.Columns.Font.Bold := True;
Worksheet.Columns.HorizontalAlignment := xlRight;
WorkSheet.Columns.ColumnWidth := 14;
// Customise the first entire Column
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
//WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
// Show Excel
XLApp.Visible[0] := True;
// Disconnect the Server
XLApp.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end;
end.

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧