我的Delphi程序要从Excel中导入数据,
代码如下:
function ReadExcelToGrid(ExcelFileName:string;Cur_grid:TStringGrid;number_row,number_col,caption_row,caption_col,start_row,start_col:integer;caption_CH,caption_EN:string):integer;
var
  sheet: TSpreadSheet;
  i,j,cur_rowcount,result_val:integer;
  temp_val:string;
begin
    result_val:=0;
    with TMSExcel.Create(nil) do
    begin
      try
        try
        //showmessage(ExcelFileName);
        LoadFromFile(ExcelFileName);
        except
          showmessage('读入Excel文件出错');
          result_val:=-1;     //未完,还有英文对话框
          result:=-1;
        end;
        sheet := Sheets.SpreadSheet(0) ;
        if Assigned(sheet) then
        begin
          {temp_val:= getvstr(sheet.Cells.GetValue(caption_col-1, caption_row-1));
          showmessage(temp_val);  }          temp_val:=getvstr(sheet.Cells.GetValue(caption_col-1, caption_row-1));
          if not ((temp_val<>'') and (pos(caption_Ch,temp_val)>0) and (pos(caption_en,temp_val)>0)) then
          begin
            ////8-9 日增加一行提示
            //showmessage('显示文件题头的名字:  ' + temp_val );
            showmessage('您选择的文件不是'+caption_CH);
            result_val:=-1;
            result:=-1;     //未完,还有英文对话框
            exit;
          end
          else
          begin
            cur_rowcount:= start_row-1;
            for i := start_row to sheet.Cells.UsedRowCount do  //从excel文件的第二行开始读入,(第一行是标题)
            begin
                temp_val:=getvstr(sheet.Cells.GetValue((number_col-1), i));
                // if (temp_val='') and (i>number_row-1) then break;
                for j := start_col-1 to sheet.Cells.UsedColCount do
                begin
                  temp_val:=getvstr(sheet.Cells.GetValue(j, i));
                  Cur_grid.Cells[j,cur_rowcount] := getvstr(sheet.Cells.GetValue(j, i));
                end;
                cur_rowcount:=cur_rowcount+1;
                Cur_grid.RowCount:= Cur_grid.RowCount+1;
            end;
           end;
         end;
      finally
        Free;
      end;
    end;
    if  result_val=0 then
    begin
      cur_rowcount:=Cur_grid.RowCount-1  ;
      for i:= start_row-1 to Cur_grid.RowCount-1 do
      begin
        try
           temp_val:= getvstr(cur_grid.Cells[(number_col-1),i]);
           if (temp_val='') and (i>number_row-1) then
           begin
             Cur_grid.RowCount:= Cur_grid.RowCount - 1;
             cur_rowcount:=cur_rowcount-1;
           end;
         except
           break;
         end;
      end;
     end;
   result:=result_val;end;在中文版本的XP(控制面板- >日期、时间、语言和区域设置- >区域和语言选项- >区域语言页中   标准格式选择中文(中国),位置选择中国)系统下面是正常的没有问题,
能从Excel中单元格中读入数据,也可以正常的导入数据进行处理。
但是在系统设置为英文XP(控制面板- >日期、时间、语言和区域设置- >区域和语言选项- >区域语言页中   标准格式选择英语(美国),位置选择美国),这个是客户的系统,就出现了问题,上述的代码不能从单元格正常的读入数据,读到的都是空值,不管是哪一个单元格都读不到数据,实在不知道是什么原因引起的? 是字符集的问题,还是其它的问题呢? 那位解决国该问题的 恳请给出帮助 高分相送!
该项目的很急,再不搞定要被老板骂了!  呜呜呜

解决方案 »

  1.   

    把temp_val:string;改成temp_val:WideString;再试试
      

  2.   

    嗯,在英文操作系统下面, 把temp_val:string;改成temp_val:WideString 确实可以读入数据, 但是以 WideString 的格式往数据库中写数据的时候写进去的还是乱码的啊,
    我试着定义一个 string型的变量, 然后直接把 widestring型变量中的值(中英文混杂的字符串)付给 string 型的变量, 结果赋到 string 型变量中的所有的汉字都是?(一个汉字对应一个?),
    我看别人说widestring 到 string 直接赋值就可以了, 我的为什么不行呢 ?
    也就是说widestring 中的Unicode码转换为delphi 可以认识的 Ansistring 时候不认识, 该怎么搞呢 ?
    本人Delphi 用的也不是很熟的? 谁给点意见撒?  谢谢!
      

  3.   

    WideString与string只有在全是英文字符或是符合本机Ansi编码的环境下才能自动转换,否则一定乱码。
    写数据库的时候要注意两件事情:第一,数据库的字段声明是否是Unicode,SQL Server中是nchar和nvarchar;第二,Delphi中给TWideStringField赋值的时候,右值也需要是WideString。
    要写出适应国际化的程序,使用Delphi(尤其是老版本)的时候,要格外注意字符串的使用。
    读一下Delphi帮助中关于WideChar的相关主题,可以找到不少WideString与string(AnsiString)转换的内容。
      

  4.   

    问题已经解决, 用的是
    { Convert Variant to AnsiString }
    function WideStringToString(const AValue: Variant; codePage: Word): AnsiString;
    {Convert  AnsiString to WideStirng }
    function StringToWideString(const AValue: Variant; codePage: Word): WideString;相应的做一下转换即可!
      

  5.   

    但是我下面有一个查询程序,在英文系统下面查询出来的数据到 DBGrid 中的数据是正常的(没有乱码), 但是用 FastReport4.0生成报表时(报表的数据也是从数据库中读的),又是乱码了, 不知道是为什么?
    这个FastReport4.0控件面板没有对应的从 TntUnicodeControls 控件,  这个地方的乱码问题要怎么才能解决呢 ?