Delphi - 操作Excel数据公式的实现
procedure TF_SMP_FT_NEW.RzBitBtn_StartToChangeClick(Sender: TObject);
var
i, j, ni, nj, iRows, iCol, iCol_flag: Integer;
T_DATE, T_LC, T_CP, T_FN, T_Row_Flag, T_sl, Data_Flag, New_Path_Flag, Warning_Flag: string;
ApplicationSheet: variant;
nArr: array[..] of Integer;
mArr: array[..] of Integer;
begin
if RzOpenDialog1.FileName <> '' then
begin
RzEdit_Status.Text := '文件正在被转换,请稍后!';
if RzEdit_FileName.Text <> '' then
begin
try
ApplicationSheet := Createoleobject('Excel.Application'); //创建工作表
ApplicationSheet.Visible := false; //Excel不可见
ApplicationSheet.Workbooks.Open(RzOpenDialog1.FileName); //打开Excel并且导入文件
ApplicationSheet.WorkSheets[].activate; //设置第一页当前页
New_Path_Flag := 'C:\Temp\' +'EX_'+ RzEdit_FileName.Text;
Warning_Flag := '文件已保存到:C:\temp\'+'EX_'+ RzEdit_FileName.Text +#+ '请注意查看!谢谢';
//Operating the sheet.
T_DATE := ApplicationSheet.Cells[, ].Value; //缓存日期
for i := to do
begin
if ApplicationSheet.Cells[, i].Value = '' then
begin
if ApplicationSheet.Cells[, i - ].Value <> '' then
begin
T_Row_Flag := ApplicationSheet.Cells[, i - ].Value; //列数标记
end;
end;
end;
T_sl := StringReplace(T_Row_Flag, 'T', '', [rfReplaceAll]);
iCol_flag := StrToInt(T_sl) + ; //列数
j := StrToInt(T_sl) + ;
for i := to j do
begin
ApplicationSheet.ActiveSheet.Columns[i].Delete; //把空白的列删除掉
end;
//asheet.Cells[16, 1].Value := 'Limit'; //先把格式规范化
ApplicationSheet.Cells[, ].Value := 'Bias1'; //先把格式规范化
ApplicationSheet.Cells[, ].Value := 'Bias2';
ApplicationSheet.Cells[, ].Value := 'Bias3'; ApplicationSheet.Cells[, ].Value := 'Min Limit';
ApplicationSheet.Cells[, ].Value := 'Max Limit';
ApplicationSheet.Cells[, ].Value := 'Average';
ApplicationSheet.Cells[, ].Value := 'STDEV';
ApplicationSheet.Cells[, ].Value := 'Min Data';
ApplicationSheet.Cells[, ].Value := 'Max Data'; ApplicationSheet.ActiveSheet.Rows[].Delete; //行的处理:从下向上
for i := to do
begin
ApplicationSheet.ActiveSheet.Rows[].Delete;
end;
ApplicationSheet.Cells[, ].Value := 'Serial#';
ApplicationSheet.Cells[, ].Value := 'Bin#';
ApplicationSheet.ActiveSheet.Rows[].Delete;
ApplicationSheet.ActiveSheet.Rows[].Delete;
ApplicationSheet.ActiveSheet.Rows[].Delete;
ApplicationSheet.ActiveSheet.Rows[].Delete;
ApplicationSheet.ActiveSheet.Rows[].Delete;
for i := to do
begin
ApplicationSheet.ActiveSheet.Rows[].Delete;
end;
ApplicationSheet.Cells[, ].Value := 'LotCode';
ApplicationSheet.Cells[, ].Value := '';
ApplicationSheet.Cells[, ].Value := 'CUSTOMER_Production';
ApplicationSheet.Cells[, ].Value := 'TestFileName';
ApplicationSheet.Cells[, ].Value := 'Parameter';
ApplicationSheet.Cells[, ].Value := '';
ApplicationSheet.Cells[, ].Value := 'Unit';
ApplicationSheet.Cells[, ].Value := '';
ApplicationSheet.ActiveSheet.Range['A2:E4'].Value := '';
ApplicationSheet.ActiveSheet.Range['A16:E16'].Value := '';
T_FN := RzEdit_FileName.Text;
T_CP := Copy(T_FN, Pos('_', T_FN) + , );
T_CP := Copy(T_CP, , Pos('_', T_CP) - ); //产品名称
T_LC := Copy(T_FN, , Pos('_', T_FN) - ); //周记
ApplicationSheet.Cells[, ].Value := T_LC;
ApplicationSheet.Cells[, ].Value := T_CP;
ApplicationSheet.Cells[, ].Value := RzEdit_FileName.Text;
OraQuery_Tmp.Close;
OraQuery_Tmp.Open;
Oraquery_Tmp.First;
i := ;
while not Oraquery_Tmp.Eof do
begin
ApplicationSheet.Cells[, i].Value := Oraquery_Tmp.FieldByName('CGCS').AsString;
ApplicationSheet.Cells[, i].Value := Oraquery_Tmp.FieldByName('CGDW').AsString;
ApplicationSheet.Cells[, i].Value := Oraquery_Tmp.FieldByName('MinLimit').AsString;
ApplicationSheet.Cells[, i].Value := Oraquery_Tmp.FieldByName('MaxLimit').AsString;
i := i + ;
Oraquery_Tmp.Next;
end;
//修改时间:20140924
//增加功能:单位进一步转换达到测试要求
iRows := ; //从100行开始
Data_Flag := ApplicationSheet.Cells[iRows, ].Value;
while Data_Flag <> '' do
begin
iRows := iRows + ;
Data_Flag := ApplicationSheet.Cells[iRows, ].Value;
end;
iRows := iRows - ; //计算行数 OraQuery_Tmp.Close;
OraQuery_Tmp.Open;
Oraquery_Tmp.First;
i := ;
while not Oraquery_Tmp.Eof do
begin
mArr[i] := Oraquery_Tmp.FieldByName('ID').AsInteger + ;
i := i + ;
Oraquery_Tmp.Next;
end;
ni := i - ; for iCol := to ni do
begin
for i := to iRows do
begin
Data_Flag := ApplicationSheet.Cells[i, mArr[iCol]].Value;
if Data_Flag = 'Untested' then
begin
Continue;
end
else
begin
ApplicationSheet.Cells[i, mArr[iCol]].Value := ApplicationSheet.Cells[i, mArr[iCol]].Value * ;
end;
end;
end;
OraQuery_Tmp.Close;
OraQuery_Tmp.Open;
Oraquery_Tmp.First;
j := ;
while not Oraquery_Tmp.Eof do
begin
nArr[j] := Oraquery_Tmp.FieldByName('ID').AsInteger + ;
j := j + ;
Oraquery_Tmp.Next;
end;
nj := j - ; for iCol := to nj do
begin
for i := to iRows do
begin
Data_Flag := ApplicationSheet.Cells[i, nArr[iCol]].Value;
if Data_Flag = 'Untested' then
begin
Continue;
end
else
begin
ApplicationSheet.Cells[i, nArr[iCol]].Value := ApplicationSheet.Cells[i, nArr[iCol]].Value * ;
end;
end;
end;
//修改时间:2014-12-16
//公式的实现
for j := iCol_flag downto do
begin
ApplicationSheet.Cells[, j].Value := '=AVERAGE(' + INDEX(, j) + ':' + INDEX(iRows, j) + ')'; //Average
ApplicationSheet.Cells[, j].Value := '=STDEVP(' + INDEX(, j) + ':' + INDEX(iRows, j) + ')'; //STDEVP
ApplicationSheet.Cells[, j].Value := '=MIN(' + INDEX(, j) + ':' + INDEX(iRows, j) + ')'; //MinDate
ApplicationSheet.Cells[, j].Value := '=MAX(' + INDEX(, j) + ':' + INDEX(iRows, j) + ')'; //MaxDate
end; //修改时间:2014-10-24
//删除相同栏位,满足客户要求
//测试程序
for i := iCol_flag downto do
begin
Data_Flag := ApplicationSheet.Cells[, i].Value;
if Data_Flag = 'N/A' then
begin
ApplicationSheet.ActiveSheet.Columns[i].Delete;
end;
end; ApplicationSheet.Cells[, ].Value := 'DATE';
ApplicationSheet.Cells[, ].Value := 'TEST TIME';
ApplicationSheet.WorkSheets[].Columns.NumberFormatLocal := 'G/通用格式'; //格式设置
ApplicationSheet.Cells[, ].Value := T_DATE;
ApplicationSheet.Cells.Item[, ].NumberFormatLocal := 'YYYYMMDD'; ApplicationSheet.DisplayAlerts := False; //不显示保存替换对话框
ApplicationSheet.WorkBooks[].SaveAs(New_Path_Flag);
ApplicationSheet.activeWorkBook.saved := True; //忽略保存
except
ApplicationSheet.Workbooks[].close(True);
end;
ApplicationSheet.Workbooks[].close(True);
RzEdit_Status.Text := '文件已转换完成!';
Application.MessageBox('文件已转换完成!', '提示', + mb_iconinformation);
ShowMessage(Warning_Flag);
end;
end
else
begin
Application.MessageBox(' 请您先打开要操作的文件!', '提示', + mb_iconinformation);
Abort;
end;
end; end.
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
Delphi - 操作Excel数据公式的实现的更多相关文章
- Delphi操作Excel大全
Delphi操作Excel大全 DELPHI操作excel(转)(一) 使用动态创建的方法 首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp ...
- 用DELPHI操作EXCEL Word
用DELPHI操作EXCEL 在DELPHI中显示EXCEL文件,可用以下简单代码做到.但要实用,则需进一步完善. var Form1: TForm1; EApp:variant;implemen ...
- VBS 移除excel数据公式,只保留值
如果将excel数据公式移除,只保留计算之后的值,将大大减少excel文件. 因为有上篇移除excel外部数据链接的经验,进行excel数据公式移除将快的多,方法如下. 首先我们得明白怎么手动移除ex ...
- C#操作Excel数据增删改查(转)
C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文件,并添加两张工作表. 工作表1: UserInfo表,字段:UserId.UserName.Age.Address.CreateT ...
- C#操作Excel数据增删改查示例
Excel数据增删改查我们可以使用c#进行操作,首先创建ExcelDB.xlsx文件,并添加两张工作表,接下按照下面的操作步骤即可 C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文 ...
- C# 操作Excel数据透视表
一.概述 数据透视表(Pivot Table)是一种交互式的表,可以进行某些计算,如求和与计数等,可动态地改变透视表版面布置,也可以重新安排行号.列标和页字段.当改变版面布置时,数据透视表也会按照新的 ...
- python中操作excel数据
python操作excel,python有提供库 本文介绍openpyxl,他只支持新型的excell( xlsx)格式,读取速度还可以 1.安装 pip install openpyxl 2.使用 ...
- 一个使用xlwings操作excel数据优化60倍处理效率的案例
☞ ░ 前往老猿Python博文目录 ░ 一.引言 老猿在将自己的博文数据(包括url地址.标题和阅读数量)从博客中获取后,使用xlwings保存到excel对象时发现,不同的处理方法性能相差非常大. ...
- delphi 控制 EXCEL 数据透视表
虽说报表多又难做,做报表相当容易. 做报表也可以偷懒的,超级实用又省事.只需要做一个报表,这个报表里面包括几乎所有的数据字段,然后将查询到的数据导出到 excel中,利用excel自带的“数据透视”功 ...
随机推荐
- springMVC校验器(validator)
springmvc使用的是Hibernate Validator(和Hibernate的ORM无关)来完成校验功能 一.普通校验 1.导入jar包 2.编写校验错误配置文件 3.配置校验错误信息文件 ...
- 调用大漠插件发送QQ和微信消息 C#版
大漠插件:3.1233 找图,找色,识字,找字,窗口,按鼠标,按键盘 0.注册dm.dll: regsvr32 xxx\dm.dll 1.添加com引用: 2.dmsoft各种调用: 原理: 查找窗口 ...
- xml解析-jaxp修改结点
jaxp修改结点 / 修改第一个p1下面的sex内容是nan * 1.创建解析器工厂 * 2.根据解析器工厂创建解析器 * 3.解析xml返回document * 4.得到sex item方法 * 5 ...
- C++ message queue 消息队列入门
说明:当我们有多个线程以不同的速度运行并且我们想要以特定的顺序从一个线程向另一个线程发送信息时,消息队列可能会有用. 这个想法是,发送线程将消息推送到队列中,而接收线程将消息按自己的步调弹出. 只要发 ...
- Android集成极光推送
要说学习极光推送,个人感觉官方文档就非常好啦,但是没法,人太懒啦,为了下次能够快速的将极光推送集成到项目中,故结合之前开发的项目和官方文档记录下简单的Android集成极光推送,在这之前,先上一张简单 ...
- Android MediaRecorder录制播放音频
1.请求录制音频权限 <user-permission android:name="android.permission.RECORD_AUDIO"/> RECORD_ ...
- man -k, man -f : nothing appropriate ; 更新 whatis 数据库
man 有两个选项: -f, –whatis Equivalent to whatis. Display a ) for details. -k, –apropos Equivalent to apr ...
- Verilog语言框架
一.常用关键字
- react界面跳转,滚动到顶部
在使用react-router-dom时,我们经常会遇到路由切换时滚动到浏览器顶部的问题. 滚动到顶部 Scroll to top 很多时候我们需要的是滚动到顶部“Scroll to top”,因为发 ...
- Jenkins之插件Publish HTML reports的使用
前提: 下载插件HTML Publisher plugin 一.安装 安装好HTML Publisher plugin之后,会在新建或者编辑项目时,在[增加构建后操作步骤]出现[Publish HTM ...