zoukankan      html  css  js  c++  java
  • 转 XlsReadWriteII 的博文(自留参考)

    如何使用XlsReadWriteII在Delphi中读取Excel文件


    XLSReadWriteII v5.20.01a for Delphi XE5 x32下载地址:

    http://download.csdn.net/detail/wozengcong/6878409

    XLSReadWriteII v5.10.25 Cracked for XE2-XE4 (Win32)下载地址:
    http://download.csdn.net/detail/wozengcong/6886915


    XLSReadWriteII_v5.20.25〖D7,XE-XE6〗下载地址:

    http://download.csdn.net/detail/wozengcong/7689003


    安装步骤:
    1、在Embarcadero RAD Studio XE2主菜单中依次点击"Component->Install Packages"添加(Add)XLSRWII5_DXE版本号.bpl
    2、在Embarcadero RAD Studio XE4主菜单中依次点击“Toosl->Options->Delphi Options->Library->Library Path”分别添加Library Path:Obj和XLSReadWriteII的完整路径

    安装步骤:
    1、在Embarcadero RAD Studio XE2主菜单中依次点击"Component->Install Packages"添加(Add)XLSRWII5_DXE2.bpl
    2、在Embarcadero RAD Studio XE4主菜单中依次点击“Toosl->Options->Delphi Options->Library->Library Path”分别添加Library Path:
    Obj和XLSReadWriteII的完整路径
    在Delphi中读取Excel文件,使用CreateOleObject的方式挺讨厌的,一直搞不定,输出了文件之后,总会在系统中打开一个Excel,就算Quit也不行,一个程序中使用的多了,还不定搞出什么事情来。狠狠心找个其它的东西来代替,于是发现了XlsReadWriteII。

    使用之后发现这个东西真不错,简单好用。不管是读还是写均轻松搞定。下面是官方的Demo中的代码。

    写文件代码,包括对格式的定制:

    [delphi] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    XLS.Filename := 'FormatSample.xls';
    XLS.Clear;
    // Add format #0
    with XLS.Formats.Add do begin
    FontIndex := XLS.Fonts.AddIndex;
    XLS.Fonts[FontIndex].Name := 'Courier new';
    XLS.Fonts[FontIndex].Size := 14;
    XLS.Fonts[FontIndex].Color := xcRed;
    end;
    // Add format #1
    with XLS.Formats.Add do begin
    FontIndex := XLS.Fonts.AddIndex;
    XLS.Fonts[FontIndex].AssignTFont(Font);
    end;
    // Add format #2
    with XLS.Formats.Add do begin
    FillPatternForeColor := xcLilac;
    end;
    // Add format #3
    with XLS.Formats.Add do begin
    BorderTopColor := xcBlue;
    BorderBottomColor := xcBlue;
    BorderTopStyle := cbsThin;
    BorderBottomStyle := cbsThick;
    end;
    // Add format #4
    // ShortDateFormat is a Delphi global variable for the local date format.
    with XLS.Formats.Add do begin
    NumberFormat := ShortDateFormat;
    end;
    XLS.Sheets[0].WriteString(1,2,0,'Format #0');
    XLS.Sheets[0].WriteString(1,3,1,'Format #1');
    XLS.Sheets[0].WriteString(1,4,2,'Format #2');
    XLS.Sheets[0].WriteString(1,5,3,'Format #3');
    XLS.Sheets[0].WriteNumber(1,6,4,Date);
    XLS.Write; //如果需要另存为一个文件,改FileName之后再写一次,如需更改工作簿的名称,设置Sheets[0].Name就可以了。

    读取文件代码:

    [delphi] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    var
    Col,Row: integer;
    Sum: double;
    begin
    if edFilename.Text = '' then begin
    ShowMessage('Filename is missing');
    Exit;
    end;
    Sum := 0;
    XLS.Filename := edFilename.Text;
    XLS.Read; //此行不能省,否则读取不到数据
    XLS.Sheets[0].LastCol := 255;
    XLS.Sheets[0].LastRow := 65535;
    for Col := XLS.Sheets[0].FirstCol to XLS.Sheets[0].LastCol do begin
    for Row := XLS.Sheets[0].FirstRow to XLS.Sheets[0].LastRow do begin
    if (Row < Grid.RowCount) and (Col < Grid.ColCount) then
    Grid.Cells[Col + 1,Row + 1] := XLS.Sheets[0].AsFmtString[Col,Row];
    if XLS.Sheets[0].CellType[Col,Row] = ctFloat then
    Sum := Sum + XLS.Sheets[0].AsFloat[Col,Row];
    end;
    end;
    lblSum.Caption := Format('The sum of all numeric cells are: %.2f',[Sum]);
    Grid.Invalidate;
    end;

    温馨提示:

    XE5报错解决方案:

    1、[dcc32 Error] Unit1.pas(51): E2003 Undeclared identifier: 'ctFloat'

    [delphi] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    uses XLSSheetData5, XLSReadWriteII5, Xc12Utils5;//增加引用:Xc12Utils5
    ctFloat修改为xctFloat


    2、[dcc32 Error] Unit1.pas(42): E2064 Left side cannot be assigned to

    [delphi] view plaincopyprint?在CODE上查看代码片派生到我的代码片
    XLS.Sheets[0].LastCol := 255;
    XLS.Sheets[0].LastRow := 65535;
    删除或者注释掉即可

    3、别名说明:

    XLS是XLSReadWriteII5组件的Name,Grid是StringGrid组件的Name

  • 相关阅读:
    [leetcode] Delete Operation for Two Strings
    [leetcode] Minimum ASCII Delete Sum for Two Strings
    [leetcode] Palindromic Substrings
    [leetcode] Student Attendance Record I
    [leetcode] Reverse String II
    [leetcode] Diameter of Binary Tree
    [leetcode] Climbing Stairs
    [leetcode] Range Sum Query
    Codeforces 1294A Collecting Coins
    团体程序设计天梯赛 L2-021 点赞狂魔 (25分)
  • 原文地址:https://www.cnblogs.com/happyhills/p/3933038.html
Copyright © 2011-2022 走看看