excel表导入数据库代码: uses :comobj,db; Procedure TForm1.Excel_2_db(str :string); var eclApp,WorkBook :variant ; xlsFileName :string; a_FiledCount:integer; //数据库表中的列数 b_filedCount:integer; //excel 文件中的 列数 b_row :integer; // excel 文件的行熟 i,j :integer; a_flag :boolean; begin Form1.OpenDialog1.Title :='Excel文件 导入到数据库'+str+'表'; Form1.OpenDialog1.InitialDir :=ExtractFilePath(Application.ExeName ); if (Form1.OpenDialog1.Execute ) then xlsFileName :=ExtractFileName(Form1.OpenDialog1.FileName) else exit; try eclApp := CreateOleObject('Excel.Application'); WorkBook :=CreateOleObject('Excel.Sheet'); except showmessage('您系统未安装MS-EXCEL'); exit; end; try workBook :=eclApp.WorkBooks.add ; eclApp.workBooks.open(Form1.OpenDialog1.FileName ); except on EOleException do begin WorkBook.close; eclApp.quit; eclApp:=Unassigned; exit; end; end; eclApp.visible :=false; try //try ..finally try //try ..except With Data_Mod.DataModule1.kcinfo_Tab do begin close ; TableName :=str; active :=true; a_FiledCount :=FieldCount; end; b_filedCount :=eclApp.ActiveSheet.UsedRange.columns.Count;//返回excel 表中的列数 b_row :=eclApp.activesheet.UsedRange.rows.count; //返回excel 表中的行数 if (a_FiledCount <>b_FiledCount) //当数据 表和导入的excel表中的列数不一样,说明导入的excel文件不是正确的 then begin showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择'); WorkBook.close; eclApp.quit; eclApp:=Unassigned; exit; end else begin //列数正确,但是还要继续判断每列的字段名是否一致 for i :=1 to b_filedCount do begin //showmessage(eclApp.activesheet.cells.item[1,i].value); //showmessage(DataMod.ADO_basic.Fields.Fields[i-1].FieldName ); if eclApp.activesheet.cells.item[2,i].value<>Data_Mod.DataModule1.kcinfo_Tab.Fields[i-1].FieldName //判断字段名是否相等 //if eclApp.activesheet.cells.item[1,i].value<>DataMod.ADO_basic.Fields.Fields[i-1].FieldName //判断中文title.caption 是否相等 then begin showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择'); WorkBook.close; eclApp.quit; eclApp:=Unassigned; Data_Mod.DataModule1.kcinfo_Tab.Close ; exit; end; end; //for i:=..... end; //end with else for i :=3 to b_row do //行 begin a_flag :=Data_Mod.DataModule1.kcinfo_Tab.Locate(eclApp.activesheet.cells.item[2,1],eclApp.activesheet.cells.item[i,1],[loCaseInsensitive]); if (a_flag =true) then begin showmessage('该记录已经存在'); Data_Mod.DataModule1.kcinfo_Tab.Next ; continue; end; With Data_Mod.DataModule1.kcinfo_Tab do begin close ; TableName :=str; active :=true; Append; end; For j :=1 to b_filedCount do //列 begin //开始导入数据库 //showmessage(eclApp.activesheet.cells.item[1,j]);//.Value); ////showmessage(eclApp.activesheet.cells.item[i,j].value); //showmessage(eclApp.activesheet.cells[i,j].value); Data_Mod.DataModule1.kcinfo_Tab.FieldByName(eclApp.activesheet.cells.item[2,j]).Value :=eclApp.activesheet.cells[i,j].value; end ; //end with For j :=1 to b_filedCount do Data_Mod.DataModule1.kcinfo_Tab.Post ; Data_Mod.DataModule1.kcinfo_Tab.Refresh ; end; showmessage('导入数据成功'); except WorkBook.close; eclApp.quit; eclApp:=Unassigned; Data_Mod.DataModule1.kcinfo_Tab.Close ; end; //end try except finally //操作错误,退出 WorkBook.close; eclApp.quit; eclApp:=Unassigned; Data_Mod.DataModule1.kcinfo_Tab.Close ; end; end;
数据库导出到Excel:sqlText=sqlText + " case when ((select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ; sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) is null ) then 0 else "; sqlText=sqlText + " (select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ; sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) end as startowe,"; sqlText=sqlText + " owe=(case when ((select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ; sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) is null ) then 0 else "; sqlText=sqlText + " (select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ; sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) end as startowe +sum(owecurtotal) )";
uses :comobj,db;
Procedure TForm1.Excel_2_db(str :string);
var eclApp,WorkBook :variant ;
xlsFileName :string;
a_FiledCount:integer; //数据库表中的列数
b_filedCount:integer; //excel 文件中的 列数
b_row :integer; // excel 文件的行熟
i,j :integer;
a_flag :boolean;
begin
Form1.OpenDialog1.Title :='Excel文件 导入到数据库'+str+'表';
Form1.OpenDialog1.InitialDir :=ExtractFilePath(Application.ExeName );
if (Form1.OpenDialog1.Execute )
then xlsFileName :=ExtractFileName(Form1.OpenDialog1.FileName)
else exit; try
eclApp := CreateOleObject('Excel.Application');
WorkBook :=CreateOleObject('Excel.Sheet');
except
showmessage('您系统未安装MS-EXCEL');
exit;
end;
try
workBook :=eclApp.WorkBooks.add ;
eclApp.workBooks.open(Form1.OpenDialog1.FileName );
except
on EOleException do
begin
WorkBook.close;
eclApp.quit;
eclApp:=Unassigned;
exit;
end;
end;
eclApp.visible :=false;
try //try ..finally
try //try ..except
With Data_Mod.DataModule1.kcinfo_Tab do
begin
close ;
TableName :=str;
active :=true;
a_FiledCount :=FieldCount; end; b_filedCount :=eclApp.ActiveSheet.UsedRange.columns.Count;//返回excel 表中的列数
b_row :=eclApp.activesheet.UsedRange.rows.count; //返回excel 表中的行数 if (a_FiledCount <>b_FiledCount) //当数据 表和导入的excel表中的列数不一样,说明导入的excel文件不是正确的
then begin
showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择');
WorkBook.close;
eclApp.quit;
eclApp:=Unassigned;
exit;
end
else begin //列数正确,但是还要继续判断每列的字段名是否一致
for i :=1 to b_filedCount do
begin
//showmessage(eclApp.activesheet.cells.item[1,i].value);
//showmessage(DataMod.ADO_basic.Fields.Fields[i-1].FieldName ); if eclApp.activesheet.cells.item[2,i].value<>Data_Mod.DataModule1.kcinfo_Tab.Fields[i-1].FieldName //判断字段名是否相等
//if eclApp.activesheet.cells.item[1,i].value<>DataMod.ADO_basic.Fields.Fields[i-1].FieldName //判断中文title.caption 是否相等
then begin
showmessage('您选择导入的excel文件错误'+#13+#10+'请您重新选择');
WorkBook.close;
eclApp.quit;
eclApp:=Unassigned;
Data_Mod.DataModule1.kcinfo_Tab.Close ;
exit;
end;
end; //for i:=.....
end; //end with else for i :=3 to b_row do //行
begin
a_flag :=Data_Mod.DataModule1.kcinfo_Tab.Locate(eclApp.activesheet.cells.item[2,1],eclApp.activesheet.cells.item[i,1],[loCaseInsensitive]);
if (a_flag =true)
then begin
showmessage('该记录已经存在');
Data_Mod.DataModule1.kcinfo_Tab.Next ;
continue;
end; With Data_Mod.DataModule1.kcinfo_Tab do
begin
close ;
TableName :=str;
active :=true;
Append;
end;
For j :=1 to b_filedCount do //列
begin //开始导入数据库
//showmessage(eclApp.activesheet.cells.item[1,j]);//.Value);
////showmessage(eclApp.activesheet.cells.item[i,j].value);
//showmessage(eclApp.activesheet.cells[i,j].value); Data_Mod.DataModule1.kcinfo_Tab.FieldByName(eclApp.activesheet.cells.item[2,j]).Value :=eclApp.activesheet.cells[i,j].value; end ; //end with For j :=1 to b_filedCount do
Data_Mod.DataModule1.kcinfo_Tab.Post ;
Data_Mod.DataModule1.kcinfo_Tab.Refresh ;
end;
showmessage('导入数据成功');
except
WorkBook.close;
eclApp.quit;
eclApp:=Unassigned;
Data_Mod.DataModule1.kcinfo_Tab.Close ;
end; //end try except
finally //操作错误,退出
WorkBook.close;
eclApp.quit;
eclApp:=Unassigned;
Data_Mod.DataModule1.kcinfo_Tab.Close ;
end;
end;
sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) is null ) then 0 else ";
sqlText=sqlText + " (select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ;
sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) end as startowe,";
sqlText=sqlText + " owe=(case when ((select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ;
sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) is null ) then 0 else ";
sqlText=sqlText + " (select aaowe= sum(owecurtotal) from tblowedetail where tblowedetail.insuranceid in (select insuranceid from tblinsurancedetail where "+ strTemp1 +" ) and tblowedetail.orgid in (select orgid from tblorgdetail where "+ strTemp2 +" ) and tblowedetail.orgid in (select orgid from tblZoneManagerDetail where "+ strTemp3 +" ) and (datepart(yyyy,oweMonth)*12+datepart(m,oweMonth)) < " + FromMonth ;
sqlText=sqlText + " group by tblowedetail.orgid ,tblowedetail.insuranceid ) end as startowe +sum(owecurtotal) )";
void DataSetToExcel(TDataSet* DataSet)
{
Variant excel_app; //excel applicatian对象
Variant excel_book; //book
Variant excel_sheet; //工作簿
Variant excel_range; //范围
Variant my_worksheet; //当前页
Variant my_range; //当前范围
///***************输出****************
excel_app=Variant::CreateObject("excel.application"); //创建app对象
excel_book=excel_app.OlePropertyGet("workbooks"); //创建book
excel_app.OlePropertySet("SheetsInNewWorkbook",(Variant)1);
excel_sheet=excel_book.OleFunction("add"); //添加一页
my_worksheet=excel_sheet.OlePropertyGet("ActiveSheet");
excel_app.OlePropertySet("Visible",(Variant)true); //可见
//int j(1);
PropertyGet Range("Range");
PropertySet SetValue("Value");
PropertySet SetFormula("Formula");
PropertyGet GetValue("Value");
PropertyGet GetFormula("Formula");
AnsiString mytmp;
for(int j=1;j<=DataSet->FieldCount;++j)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=String(char(64+j))+IntToStr(1);
//Range.ClearArgs();
// SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << mytmp );
my_range.Exec(SetValue << DataSet->Fields->Fields[j-1]->FieldName);
Range.ClearArgs();
}
DataSet->Bof;
for (int i=2;i<=DataSet->RecordCount+1;++i)
{ for(int j=1;j<=DataSet->FieldCount;++j)
{
Range.ClearArgs();
SetValue.ClearArgs();
mytmp=String(char(64+j))+IntToStr(i);
//Range.ClearArgs();
//SetValue.ClearArgs();
my_range = my_worksheet.Exec(Range << mytmp );
my_range.Exec(SetValue << DataSet->Fields->Fields[j-1]->AsString);
Range.ClearArgs();
}
DataSet->Next();
}
}
一直不知道怎么处理!