求大神给小弟我注释一下
求大神给我注释一下
var
Excel: OleVariant;
iRow,iCol : integer;
xlsFilename: string;
begin
OpenDialog1.Title := '请选择正确的excel文件';
OpenDialog1.Filter := 'Excel(*.xls)|*.xls';
if OpenDialog1.Execute then
begin
xlsFilename := OpenDialog1.FileName;
try
Excel := CreateOLEObject('Excel.Application');
except
Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
ADOQ1.Close;
ADOQ1.Open;
ADOQ1.Filtered := false;
Excel.Visible := false;
Excel.WorkBooks.Open(xlsFilename);
try
iRow := 2;
iCol := 1;
while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do
begin
if ADOQ1.Locate('years',trim(Excel.WorkSheets[1].Cells[iRow,iCol].value),[loCaseInsensitive]) then
begin
if not ADOQ1.FieldByName('audit').AsBoolean then
begin
ADOQ1.Edit;
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end
else
begin
ADOQ1.Insert;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end;
end
else
begin
ADOQ1.Insert;;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end;
iRow := iRow + 1;
end;
Excel.Quit;
MessageDlg('数据导入完成!',mtInformation,[mbOk],0);
except
Excel.Quit;
raise;
end;
ADOQ1.Close;
ADOQ1.Open;
end;
end;
------解决思路----------------------
简单写了一下
------解决思路----------------------
var
Excel: OleVariant;
iRow,iCol : integer;
xlsFilename: string;
begin
OpenDialog1.Title := '请选择正确的excel文件';
OpenDialog1.Filter := 'Excel(*.xls)|*.xls';
if OpenDialog1.Execute then
begin
xlsFilename := OpenDialog1.FileName;
try
Excel := CreateOLEObject('Excel.Application');
except
Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL);
Exit;
end;
ADOQ1.Close;
ADOQ1.Open;
ADOQ1.Filtered := false;
Excel.Visible := false;
Excel.WorkBooks.Open(xlsFilename);
try
iRow := 2;
iCol := 1;
while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do
begin
if ADOQ1.Locate('years',trim(Excel.WorkSheets[1].Cells[iRow,iCol].value),[loCaseInsensitive]) then
begin
if not ADOQ1.FieldByName('audit').AsBoolean then
begin
ADOQ1.Edit;
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end
else
begin
ADOQ1.Insert;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end;
end
else
begin
ADOQ1.Insert;;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
end;
iRow := iRow + 1;
end;
Excel.Quit;
MessageDlg('数据导入完成!',mtInformation,[mbOk],0);
except
Excel.Quit;
raise;
end;
ADOQ1.Close;
ADOQ1.Open;
end;
end;
------解决思路----------------------
简单写了一下
var
Excel: OleVariant; //创建OLE对象
iRow,iCol : integer; //所在行、列
xlsFilename: string; //excel文件名称
begin
OpenDialog1.Title := '请选择正确的excel文件'; //对话框标题
OpenDialog1.Filter := 'Excel(*.xls)
------解决思路----------------------
*.xls'; //打开文件类型 excel文件
if OpenDialog1.Execute then //如果选择文件
begin
xlsFilename := OpenDialog1.FileName; //将excel文件所在路径和名称赋值给变量xlsFilename
try
Excel := CreateOLEObject('Excel.Application'); //创建Excel对象
except
Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL); //异常处理代码
Exit;
end;
ADOQ1.Close; //ADO数据组件Tadoquery 关闭
ADOQ1.Open; //ADO数据组件Tadoquery 打开
ADOQ1.Filtered := false; //关闭过滤
Excel.Visible := false; // 隐藏excel对象
Excel.WorkBooks.Open(xlsFilename); //打开excel工作簿
try
iRow := 2;
iCol := 1;
//上面两句是定位到哪个单元格
while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do //判断单元格的数值不为空 循环语句
begin
if ADOQ1.Locate('years',trim(Excel.WorkSheets[1].Cells[iRow,iCol].value),[loCaseInsensitive]) then //定位字段years
begin
if not ADOQ1.FieldByName('audit').AsBoolean then //字段audit如果为真
begin
ADOQ1.Edit; //编辑字段
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value)); //给字段quantity赋值
ADOQ1.Post; //提交更改
end
else
begin //字段audit不为真
ADOQ1.Insert;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
// 向字段years,quantity插入数据(excel中的数据)
end;
end
else
begin //定位非字段years
ADOQ1.Insert;;
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value));
ADOQ1.Post;
// 出入数据同上
end;
iRow := iRow + 1; //行+1 ,循环体循环
end;
Excel.Quit;
MessageDlg('数据导入完成!',mtInformation,[mbOk],0);
except
Excel.Quit;
raise;
end;
ADOQ1.Close;
ADOQ1.Open;
end;
end;
------解决思路----------------------
这是EXCEL文件的数据读入TADOQuery表(数据库)的代码。注释花费时间,还要通俗。
var Excel: OleVariant; // Excel声明为OleVariant类型
iRow,iCol : integer;
xlsFilename: string;
begin
OpenDialog1.Title := '请选择正确的excel文件'; // OpenDialog的标题
OpenDialog1.Filter := 'Excel(*.xls)
------解决思路----------------------
*.xls'; // OpenDialog打开文件类型(指定文件后缀名)
if OpenDialog1.Execute then // OpenDialog成功打开文件
begin
xlsFilename := OpenDialog1.FileName; // xlsFilename指定被打开的文件名
try
Excel := CreateOLEObject('Excel.Application'); // 通过Delphi的Servets中的TExcelApplication打开EXCEL文件
except
Application.MessageBox('excel没有安装', '提示信息', MB_OK+MB_ICONASTERISK+MB_DEFBUTTON1+MB_APPLMODAL); // 系统未将office
Exit; // 退出
end;
ADOQ1.Close; // 关闭 TADOQuery
ADOQ1.Open; // 打开 TADOQuery
ADOQ1.Filtered := false; // TADOQuery 不过滤
Excel.Visible := false; // TADOQuery 不显示
Excel.WorkBooks.Open(xlsFilename) // 打开EXCEl文件
try
iRow := 2; // 单元行
iCol := 1; // 单元列
while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do // While循环语句 条件:当指定EXCEL的Book1的【行,列】值<>空格
begin
// 数据集中查询特定的记录,并定位该记录为当前记录 loCaseInsensitive是字符匹配选项:不分大小写查询数据
if ADOQ1.Locate('years',trim(Excel.WorkSheets[1].Cells[iRow,iCol].value),[loCaseInsensitive]) then
begin
if not ADOQ1.FieldByName('audit').AsBoolean then // 字段audit值为真
begin
ADOQ1.Edit; // 编辑该行数据
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value)); // 赋新值
ADOQ1.Post; // 确定编辑数据
end
else begin
ADOQ1.Insert; // 插入一行 并Edit
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value); // 写入数据
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value)); // 写入数据
ADOQ1.Post; // 确定传输数据
end;
end
else begin
ADOQ1.Insert;; // 插入一行 并Edit
ADOQ1.FieldByName('years').Value := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value); // 写入数据
ADOQ1.FieldByName('quantity').Value := StrToFloat(trim(Excel.WorkSheets[1].Cells[iRow,iCol+6].value)); // 写入数据
ADOQ1.Post; // 确定传输数据
end;
iRow := iRow + 1; // 向下一行
end;
Excel.Quit; // Excel退出
MessageDlg('数据导入完成!',mtInformation,[mbOk],0); // 打开信息提示对话框
except // 出现错误执行下列代码
Excel.Quit; // Excel退出
raise; // 抛出异常
end;
ADOQ1.Close;
ADOQ1.Open;
end;
end;