作者:乔山办公网日期:
返回目录:excel表格制作
方法1:将查询结果写入本地的csv文件(copyexcel能直接处理csv文件)
方法2:将查询结果写入本地foxpro2.5格式的dbf文件(excel能直接处理dbf)
方法3:利用zhidaodelphi的office组件
方法4:利用delphi的 comobj 单元(其实就是ole)
如果是提取多个工作簿的Sheet1的A1单元格的数据。在A列分别输入多个工作簿的名称,然后在B2单元格输入以下公式,并向下填充公式=INDIRECT("'["&A2&"]Sheet1'!A1")
直接做一个导出功能即可,实现流程如下:
1、添加一个“7a686964616fe4b893e5b19e330导出”按钮;
2、点击按钮,双击进入代码编辑区,先定义局部变量XlAPP(var XlAPP: Variant;);
3、再添加实现导出的代码,如下:
try
if ActiveControl.ClassType.ClassName = 'TwwDBGrid' then
begin
// 将游标变为漏斗
Screen.Cursor := crHourglass;
if not Db_data.DataSource.DataSet.Active then exit;
if Db_data.DataSource.DataSet.RecordCount < 1 then exit;
try
XlApp := createoleobject('Excel.Application');
XLApp.Visible := false;
// excelcount := XLApp.Workbooks.count;
XLApp.Workbooks.Add(xlWBatWorkSheet);
Sheet1 := XLApp.Workbooks[1].WorkSheets['sheet1'];
except
showmessage('未能正常导出Excel,请与IT部同事联系!');
exit;
end;
// 把游标变为预设值
Screen.Cursor := crDefault;
end
else
application.MessageBox('请选择需要导出的内容!','温馨提示', 0);
finally
end;
可以自己做个函数
//=======================================================================
//========================Excel数据导入Sql表函数======================
//=======================================================================
Function ExcelToSQL(ExcelFileName:String;SQLTableName:String;InputField:String;Gauge:TGauge):String;
Var ExcelApp,WorkBook,Sheet:OleVariant;
i,j:integer;
ExcelRowCount:Integer;
TableColCount:Integer;
InsertValue,InsertSql:String;
CellValue:String;
begin
//检测计算机中是否安装7a686964616fe59b9ee7ad94365了Excel,是否可以建立对象
Try
ExcelApp:=CreateOleObject('Excel.Application');
Except
Begin
Application.MessageBox('你的计算机中没有安装Excel,无法导出到Excel!','警告',0);
Exit;
end;
end;
ExcelApp.Visible:=False;
WorkBook:=ExcelApp.WorkBooks.Open(ExcelFileName);
Sheet:=WorkBook.WorkSheets[1];
ExcelRowCount:=Sheet.UsedRange.Rows.Count; //Excel表行数
DM.ADOQ_Find.Close;
DM.ADOQ_Find.SQL.Clear;
DM.ADOQ_Find.SQL.Add('Select '+InputField+' From '+SQLTableName);
DM.ADOQ_Find.Open;
TableColCount:=DM.ADOQ_Find.FieldCount; //SQL表中列数
Gauge.MaxValue:=ExcelRowCount;
Gauge.Progress:=0;
For i:=2 to ExcelRowCount do
Begin
InsertValue:='';
For j:=1 to TableColCount do
Begin
CellValue:=Sheet.Cells[i,j];
If InsertValue='' then //利用StringReplace 替换掉字段中的空格
InsertValue:=InsertValue+''''+StringReplace(CellValue,' ','',[rfReplaceAll, rfIgnoreCase])+''''
Else
InsertValue:=InsertValue+','+''''+StringReplace(CellValue,' ','',[rfReplaceAll, rfIgnoreCase])+'''' ;
end;
InsertSql:='Insert Into '+SQLTableName+'('+InputField+')'+' Values('+InsertValue+')';
//ShowMessage(insertsql);
Try
With DM.ADOQ_Find DO
Begin
Close;
Sql.Clear;
Sql.Add(InsertSql);
ExecSql;
end;
except
Application.MessageBox('导入失败,请检查Excel文件或数据格式!','操作提示',0);
Exit;
end;
Gauge.Progress:=i;
end;
Gauge.Progress:=0;
ExcelApp.Quit;
//Application.MessageBox('导入成功!','操作提示',0);
end;
调用时
ExcelToSQL(lEdit_DicFile.Text,TableName,InputField,Gauge);
lEdit_DicFile.Text, 是文件名
TableName 表名
InputField 字段名
Gauge 进度条