CnPack Forum


 
Subject: CnXlsWriter更新一个bug
solokey
新警察
Rank: 1



UID 30253
Digest Posts 0
Credits 28
Posts 12
点点分 28
Reading Access 10
Registered 2007-11-2
Status Offline
Post at 2010-6-2 15:10  Profile | Blog | P.M. 
CnXlsWriter更新一个bug

没找到老刘,所以就发在这里了.管理员看到了更新一下吧.

修改这个函数即可.以前的代码会搞错int64和extended,因为variant里貌似没这个类型.现在把用string写入varriant的整型和浮点型,都在前面加个',防止被科学计数...

procedure TCnXlsWriter.SetCells(const ACol: Byte; const ARow: Word; const Value: Variant);
var
  aStr: string;
  aInt: Integer;
  aFloat: Double;
  aCode: Integer;
begin
  case VarType(Value) of
    varSmallint, varInteger, varByte:
      XlsWriteCellRk(FStream, ACol, ARow, Value);
    varSingle, varDouble, varCurrency:
      XlsWriteCellNumber(FStream, ACol, ARow, Value);
    varString, {$IFDEF DELPHI2009_UP} varUString, {$ENDIF} varOleStr:
      begin
        Val(aStr, aInt, aCode);
        if aCode = 0 then
        begin
          XlsWriteCellLabel(FStream, ACol, ARow, '''' + aStr);
          Exit;
        end;
        Val(aStr, aFloat, aCode);
        if aCode = 0 then
        begin
          XlsWriteCellLabel(FStream, ACol, ARow, '''' + aStr);
          Exit;
        end;
        
        XlsWriteCellLabel(FStream, ACol, ARow, Value);
      end;
    varDate:
      XlsWriteCellLabel(FStream, ACol, ARow, DateTimeToStr(Value));
  else
    XlsWriteCellBlank(FStream, ACol, ARow);
  end;
end;

Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2010-6-16 20:43  Profile | Blog | P.M. 
好的,非常感谢。已经提交入SVN。
Top
solokey
新警察
Rank: 1



UID 30253
Digest Posts 0
Credits 28
Posts 12
点点分 28
Reading Access 10
Registered 2007-11-2
Status Offline
Post at 2010-8-9 00:24  Profile | Blog | P.M. 
貌似还是得更新一个,2010下的问题.这种文件格式貌似不支持Unicode的双字节字符串.

更新一个函数声明:

    procedure XlsWriteCellLabel(XlsStream: TStream; const ACol: Byte; const ARow: Word;
      const AValue: AnsiString);

再继续更新那个函数:

procedure TCnXlsWriter.SetCells(const ACol: Byte; const ARow: Word; const Value: Variant);
var
  aStr: AnsiString;
  aUStr: WideString;
  aInt: Integer;
  aFloat: Double;
  aCode: Integer;
begin
  case VarType(Value) of
    varSmallint, varInteger, varByte:
      XlsWriteCellRk(FStream, ACol, ARow, Value);
    varSingle, varDouble, varCurrency:
      XlsWriteCellNumber(FStream, ACol, ARow, Value);
    varString, varOleStr:
      begin
        aStr := VarToStr(Value);
        Val(aStr, aInt, aCode);
        if aCode = 0 then
        begin
          XlsWriteCellLabel(FStream, ACol, ARow, '''' + aStr);
          Exit;
        end;
        Val(aStr, aFloat, aCode);
        if aCode = 0 then
        begin
          XlsWriteCellLabel(FStream, ACol, ARow, '''' + aStr);
          Exit;
        end;
        XlsWriteCellLabel(FStream, ACol, ARow, Value);
      end;
    {$IFDEF DELPHI2009_UP}
    varUString:
      begin
        aUStr := VarToWideStr(Value);
        aStr := WideCharToString(PWideChar(aUStr));
        XlsWriteCellLabel(FStream, ACol, ARow, aStr);
      end;
    {$ENDIF}
    varDate:
      XlsWriteCellLabel(FStream, ACol, ARow, DateTimeToStr(Value));
  else
    XlsWriteCellBlank(FStream, ACol, ARow);
  end;
end;

另外,这样写的话,d2010会报Warning,因为我手头没这个高科技的2010...谁来试下?
目前的代码双字节的问题是解决了..就是会报Warning,不爽啊.
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2010-8-9 07:43  Profile | Blog | P.M. 
提交了。感谢。

Warning的问题,加上AnsiString强制类型转换就行。具体代码可在SVN上看看?
Top
 




All times are GMT++8, the time now is 2024-11-24 02:21

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.009935 second(s), 9 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP