unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, XLSReadWriteII5, Grids, IniFiles, Xc12Utils5,
XLSSheetData5, XPMan;

type TDoubleArray = array of double;

type
TfrmMain = class(TForm)
Panel1: TPanel;
btnRead: TButton;
btnWrite: TButton;
edReadFilename: TEdit;
edWriteFilename: TEdit;
btnDlgOpen: TButton;
btnDlgSave: TButton;
dlgSave: TSaveDialog;
dlgOpen: TOpenDialog;
Button1: TButton;
Grid: TStringGrid;
btnAddCells: TButton;
XLS: TXLSReadWriteII5;
XPManifest1: TXPManifest;
procedure btnCloseClick(Sender: TObject);
procedure btnReadClick(Sender: TObject);
procedure btnWriteClick(Sender: TObject);
procedure btnDlgOpenClick(Sender: TObject);
procedure btnDlgSaveClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnAddCellsClick(Sender: TObject);
private
procedure AddCells;

procedure ReadCells;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
Close;
end;

procedure TfrmMain.btnReadClick(Sender: TObject);
begin
XLS.Filename := edReadFilename.Text;
XLS.Read;

ReadCells;
end;

procedure TfrmMain.btnWriteClick(Sender: TObject);
begin
XLS.Filename := edWriteFilename.Text;
XLS.Write;
end;

procedure TfrmMain.btnDlgOpenClick(Sender: TObject);
begin
dlgOpen.FileName := edReadFilename.Text;
if dlgOpen.Execute then
edReadFilename.Text := dlgOpen.FileName;
end;

procedure TfrmMain.btnDlgSaveClick(Sender: TObject);
begin
dlgSave.FileName := edWriteFilename.Text;
if dlgSave.Execute then
edWriteFilename.Text := dlgSave.FileName;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
Close;
end;

procedure TfrmMain.ReadCells;
var
C,R: integer;
Ref: string;
Cnt: integer;
vError: TXc12CellError;
CellType: TXLSCellType;
begin
Cnt := 0;
XLS[0].CalcDimensions;
for R := XLS[0].FirstRow to XLS[0].LastRow do begin
for C := XLS[0].FirstCol to XLS[0].LastCol do begin
CellType := XLS[0].CellType[C,R];
if CellType <> xctNone then begin
Ref := ColRowToRefStr(C,R);

Grid.Cells[0,Cnt + 1] := Ref;

case CellType of
xctBlank : begin
Grid.Cells[1,Cnt + 1] := ‘Blank‘;
end;
xctBoolean : begin
Grid.Cells[1,Cnt + 1] := ‘Boolean‘;
if XLS[0].AsBoolean[C,R] then
Grid.Cells[2,Cnt + 1] := ‘TRUE‘
else
Grid.Cells[2,Cnt + 1] := ‘FALSE‘;
end;
xctError : begin
Grid.Cells[1,Cnt + 1] := ‘Error‘;
vError := XLS[0].AsError[C,R];
Grid.Cells[2,Cnt + 1] := Xc12CellErrorNames[vError];
end;
xctString : begin
Grid.Cells[1,Cnt + 1] := ‘String‘;
Grid.Cells[2,Cnt + 1] := XLS[0].AsString[C,R];
end;
xctFloat : begin
Grid.Cells[1,Cnt + 1] := ‘Float‘;
Grid.Cells[2,Cnt + 1] := FloatToStr(XLS[0].AsFloat[C,R]);
end;
xctFloatFormula : begin
Grid.Cells[1,Cnt + 1] := ‘Formula, float‘;
Grid.Cells[2,Cnt + 1] := FloatToStr(XLS[0].AsFloat[C,R]);
Grid.Cells[3,Cnt + 1] := XLS[0].AsFormula[C,R]
end;
xctStringFormula : begin
Grid.Cells[1,Cnt + 1] := ‘Formula, string‘;
Grid.Cells[2,Cnt + 1] := XLS[0].AsString[C,R];
Grid.Cells[3,Cnt + 1] := XLS[0].AsFormula[C,R]
end;
xctBooleanFormula: begin
Grid.Cells[1,Cnt + 1] := ‘Formula, boolean‘;
if XLS[0].AsBoolean[C,R] then
Grid.Cells[2,Cnt + 1] := ‘TRUE‘
else
Grid.Cells[2,Cnt + 1] := ‘FALSE‘;
Grid.Cells[3,Cnt + 1] := XLS[0].AsFormula[C,R]
end;
xctErrorFormula : begin
Grid.Cells[1,Cnt + 1] := ‘Formula, error‘;
vError := XLS[0].AsError[C,R];
Grid.Cells[2,Cnt + 1] := Xc12CellErrorNames[vError];
Grid.Cells[3,Cnt + 1] := XLS[0].AsFormula[C,R]
end;
end;

Inc(Cnt);
if Cnt > Grid.RowCount then
Exit;
end;
end;
end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
S: string;
Ini: TIniFile;
begin
S := ChangeFileExt(Application.ExeName,‘.ini‘);
Ini := TIniFile.Create(S);
try
edReadFilename.Text := Ini.ReadString(‘Files‘,‘Read‘,‘‘);
edWriteFilename.Text := Ini.ReadString(‘Files‘,‘Write‘,‘‘);
finally
Ini.Free;
end;

Grid.Cells[0,0] := ‘Refrence‘;
Grid.Cells[1,0] := ‘Cell type‘;
Grid.Cells[2,0] := ‘Value‘;
Grid.Cells[3,0] := ‘Formula‘;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
var
S: string;
Ini: TIniFile;
begin
S := ChangeFileExt(Application.ExeName,‘.ini‘);
Ini := TIniFile.Create(S);
try
Ini.WriteString(‘Files‘,‘Read‘,edReadFilename.Text);
Ini.WriteString(‘Files‘,‘Write‘,edWriteFilename.Text);
finally
Ini.Free;
end;
end;

procedure TfrmMain.btnAddCellsClick(Sender: TObject);
begin
AddCells;

ReadCells;
end;

procedure TfrmMain.AddCells;
begin
// Column and row references are zero-relative. Cell A1 have column 0 and row 0.

// Add a float value in cell A1
XLS[0].AsFloat[0,0] := 125;
// Cell references can also be given as a string when using the AsXXXRef properties.
XLS[0].AsFloatRef[‘A2‘] := 250;

// Inserting a float values in column B, starting at row 4 and continuing to row 6.
// The array argument can be of any size.
XLS[0].InsertFloatColValues(0,3,[10,20,30]);

XLS[0].AsString[1,0] := ‘Hello, world‘;
XLS[0].AsStringRef[‘B2‘] := ‘Hello again‘;

XLS[0].InsertStringColValues(1,3,[‘One‘,‘Two‘,‘Three‘]);

XLS[0].AsBoolean[2,0] := True;
XLS[0].AsBooleanRef[‘C2‘] := False;

// Adding Excel error values.
XLS[0].AsError[2,0] := errDiv0;
XLS[0].AsErrorRef[‘C2‘] := errNA;

// Adding values as variants.
XLS[0].AsVariant[3,0] := ‘Oink!‘;
XLS[0].AsVariantRef[‘D2‘] := 750.25;

// Inserting a float values in Row 11, starting at col A and continuing to col C.
// The array argument can be of any size.
XLS[0].InsertFloatRowValues(0,10,[100,200,300]);

XLS[0].InsertFloatRowValues(5,10,[1000,2000,3000]);

// Inserting values as a variant array. The values can be numeric, string or boolean.
// Error values are not possible as delphi will translate the error type to
// an integer value.
XLS[0].InsertRowValues(0,11,[1,‘Two‘,True]);

XLS[0].InsertColValues(6,0,[1,‘Two‘,True]);

// Add formulas. Formuas are entered as text strings in the same syntax as
// in Excel.
XLS[0].AsFormula[4,0] := ‘SUM(A10:H11)‘;
// Set the result of the formula (this value is wrong).
XLS[0].AsNumFormulaValue[4,0] := 5000;
XLS[0].AsFormulaRef[‘E2‘] := ‘MAX(A10:H11)*100‘;

// Calculate the workbook. This will replace the above wrong formula result
// with the correct value.
// This os only of importance when working with the file in the component,
// or possible if the file is exported to another software that don‘t calculate
// formulas.
// When the file is opened in Excel, the formulas will be recalculated.
XLS.Calculate;

XLS[0].CalcDimensions;
end;

end.
例子1、

TXLSReadWriteII2版本导出Excel文件:

procedure TForm1.N1Click(Sender: TObject);

var

i: Integer;

aSaveDialog: TSaveDialog;

aFileName, aStampTime: AnsiString;

aXlsObj: TXLSReadWriteII2;

p: PDataRec;

begin

aSaveDialog := TSaveDialog.Create(Self);

try

aSaveDialog.InitialDir := ExtractFilePath(ParamStr(0));

aSaveDialog.DefaultExt := ‘xls‘;

aSaveDialog.Filter := ‘Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*‘;

aStampTime := FormatDateTime(‘yyyymmddhhnnss‘, Now);

aSaveDialog.FileName := aStampTime;

if not aSaveDialog.Execute then

Exit;

aFileName := aSaveDialog.FileName;

if aFileName = ‘‘ then

Exit;

finally

aSaveDialog.Free;

end;

aXlsObj := TXLSReadWriteII2.Create(nil);

try

aXlsObj.Sheets[0].AsWideString[0, 0] := ‘id‘;

aXlsObj.Sheets[0].AsWideString[1, 0] := ‘table‘;

aXlsObj.Sheets[0].AsWideString[2, 0] := ‘kind‘;

aXlsObj.Sheets[0].AsWideString[3, 0] := ‘rows‘;

aXlsObj.Sheets[0].AsWideString[4, 0] := ‘times‘;

aXlsObj.Sheets[0].AsWideString[5, 0] := ‘desc‘;

for i:=1 to FDataHash.Count - 1 do

begin

p := FDataHash[i];

aXlsObj.Sheets[0].AsWideString[0, i] := Format(‘%d‘, [p.id]);

aXlsObj.Sheets[0].AsWideString[1, i] := p.table;

aXlsObj.Sheets[0].AsWideString[2, i] := p.kind;

aXlsObj.Sheets[0].AsWideString[3, i] := Format(‘%d‘, [p.rows]);

aXlsObj.Sheets[0].AsWideString[4, i] := Format(‘%d‘, [p.times]);

aXlsObj.Sheets[0].AsWideString[5, i] := p.desc;

end;

aXlsObj.Filename := aFileName;

aXlsObj.Write;

ShowMessage(Format(‘导出文件‘+#13#10+‘%s‘+#13#10 +‘成功!‘, [aFileName]));

finally

aXlsObj.Free;

end;

end;

TXLSReadWriteII5 单元格读写的更多相关文章

  1. 【原创】.NET读写Excel工具Spire.Xls使用(3)单元格控制

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

  2. NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel

    NPOI.dll 用法.单元格,样式,字体,颜色,行高,宽度.读写excel 转载:http://yuncode.net/code/c_531e679b3896495 view source prin ...

  3. C#读写EXCEL单元格的问题

    最近, 我在用C#开发一个EXCEL Add-In的时候,发现了一些害人不浅的坑,特来总结列举如下: 这里我读写EXCEL引用的是using Excel = Microsoft.Office.Inte ...

  4. Python 实现 Excel 里单元格的读写与清空操作

    #coding=utf-8 # coding=utf-8 作用是声明python代码的文本格式是utf-8,python按照utf-8的方式来读取程序. # 如果不加这个声明,无论代码中还是注释中有中 ...

  5. 转载 NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel

    我用的版本是1.25的.每个版本用法有一点不同 using System; using System.Collections.Generic; using System.ComponentModel; ...

  6. 利用NPOI开源的读写Excel、WORD等微软OLE2组件读写execl,控制样式或单元格

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  7. POI读写Excel-操作包含合并单元格操作

    在上篇博客中写到关于Excel操作解析成相关的类,下面将写入一种Excel对Excel表格读取和写入. 对于Excel表格操作,最重要的是创建workBook.其操作顺序是: 1.获得WorkBook ...

  8. ASP.NET 导出gridview中的数据到Excel表中,并对指定单元格换行操作

    1. 使用NPOI读取及生成excel表. (1)导出Click事件: 获取DataTable; 给文件加文件名: string xlsxName = "xxx_" + DateT ...

  9. C#操作Excel的技巧与方法 设置单元格等

    C#操作Excel可以分为客户端和插件版本,区别就是是否需要Excel环境,功能实现一样 一.通用操作与处理(有点乱有时间再整理) 1:工程对excel类库的导入,如: c:\program file ...

随机推荐

  1. Locust 集合点

    直接编写接口事务脚本对后台接口进行测试:有时测试需要让所有并发用户完成初始化后再进行压力测试,这就需要类似于LoadRunner中的集合点的概念,由于框架本身没有直接封装,有如下办法实现: from ...

  2. Cannot find ./catalina.sh The file is absent or does not have execute permission This file is nee

    从tomcat官网上下载了apache-tomcat-5.5.36.zip,在window xp系统里面解压以后,直接放在了linux服务器上. 进入tomcat/bin目录,执行启动的时候出现如下错 ...

  3. XXS level8

    (1)查看PHP源代码 <?php ini_set("display_errors", 0); $str = strtolower($_GET["keyword&q ...

  4. 解决jquery库和base库的冲突

    jquery库引用在base库之前,$的所有权就是base库的:而jquery库引用在base库之前后的话,$的所有权就是jquery库的.解决这种库之间的冲突可用以下方法解决: 情况一,jquery ...

  5. 基于ajax 验证表单是否被占用----------------附:10.25日总结

    总得来说,今天的主要工作是注册页面的处理, 1.判断 用户名与密码是否为空值 ,两次密码框输入的值是否相同.判断邮箱过程中,有使用到正则表达式 2.用户名是否使用过,有用到了json与ajax的知识. ...

  6. BEAM188简单应用

    目录 BEAM188简介 APDL应用实例 显示梁三维图 BEAM188简介 BEAM188-3D线性有限应变梁 Beam188 单元适合于分析从细长到中等粗短的梁结构,该单元基于铁木辛哥梁结构理论, ...

  7. C# 线程安全集合

    转载 对于并行任务,与其相关紧密的就是对一些共享资源,数据结构的并行访问.经常要做的就是对一些队列进行加锁-解锁,然后执行类似插入,删除等等互斥操作. .NetFramework 4.0 中提供了一些 ...

  8. PythonStudy——运算符优先级 Operator precedence

    运算符优先级 以下所列优先级顺序按照从低到高优先级的顺序:同行为相同优先级. 1 Lambda #运算优先级最低 2 逻辑运算符: or 3 逻辑运算符: and 4 逻辑运算符:not 5 成员测试 ...

  9. java_basic_基础

    变量 类型 运算符 条件 循环 调试 字符串 数组 从键盘输入数据 switch的用法 从变量接收值 从键盘接收值 输出到一个文件 基本类型的赋值与引用类型的赋值是不一样的 是将引用类型的地址 每一个 ...

  10. find查找文件的时间问题

    很多细节方面的东西没有到真正用的时候,是觉察不出来的,因为这个时间的问题出了问题,现在好好理一下,这个find的时间很容易就搞混了,一段时间不用,也忘了,也反映出来了自己的基础知识不是很牢固啊   f ...