我的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(控制面板- >日期、时间、语言和区域设置- >区域和语言选项- >区域语言页中 标准格式选择英语(美国),位置选择美国),这个是客户的系统,就出现了问题,上述的代码不能从单元格正常的读入数据,读到的都是空值,不管是哪一个单元格都读不到数据,实在不知道是什么原因引起的? 是字符集的问题,还是其它的问题呢? 那位解决国该问题的 恳请给出帮助 高分相送!
该项目的很急,再不搞定要被老板骂了! 呜呜呜
代码如下:
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(控制面板- >日期、时间、语言和区域设置- >区域和语言选项- >区域语言页中 标准格式选择英语(美国),位置选择美国),这个是客户的系统,就出现了问题,上述的代码不能从单元格正常的读入数据,读到的都是空值,不管是哪一个单元格都读不到数据,实在不知道是什么原因引起的? 是字符集的问题,还是其它的问题呢? 那位解决国该问题的 恳请给出帮助 高分相送!
该项目的很急,再不搞定要被老板骂了! 呜呜呜
解决方案 »
- delphi中使用统计函数问题
- 在这临近年关的时候,我不祝福大家新年快乐,请大家进来缅怀已经离去1周年的sysu死树前辈!
- DBLookupComboBox如何使用
- 大家帮小弟一下
- 争shell 打开explorer 的程序(在线等
- 请问如何判断一个函数的执行时间?
- 跪求!!!!!!!高分相送还!!!!!!!
- 有什么简单的办法把字符串分离出来
- 我想把网站上的论坛内容打包供别人下载,离线浏览,所以想做一个类似行舟做的CSDN的离线浏览器,大家出出主意!
- 一个数组的问题,不用for循环怎么把其中的每个字符读出来哦?
- 小弟刚到,分不多少散点分求解:treeview怎么用?
- 刚接手一个项目,是通过串口接单片机通讯方面的.有点问题想和大家切磋一下!!!!
我试着定义一个 string型的变量, 然后直接把 widestring型变量中的值(中英文混杂的字符串)付给 string 型的变量, 结果赋到 string 型变量中的所有的汉字都是?(一个汉字对应一个?),
我看别人说widestring 到 string 直接赋值就可以了, 我的为什么不行呢 ?
也就是说widestring 中的Unicode码转换为delphi 可以认识的 Ansistring 时候不认识, 该怎么搞呢 ?
本人Delphi 用的也不是很熟的? 谁给点意见撒? 谢谢!
写数据库的时候要注意两件事情:第一,数据库的字段声明是否是Unicode,SQL Server中是nchar和nvarchar;第二,Delphi中给TWideStringField赋值的时候,右值也需要是WideString。
要写出适应国际化的程序,使用Delphi(尤其是老版本)的时候,要格外注意字符串的使用。
读一下Delphi帮助中关于WideChar的相关主题,可以找到不少WideString与string(AnsiString)转换的内容。
{ Convert Variant to AnsiString }
function WideStringToString(const AValue: Variant; codePage: Word): AnsiString;
{Convert AnsiString to WideStirng }
function StringToWideString(const AValue: Variant; codePage: Word): WideString;相应的做一下转换即可!
这个FastReport4.0控件面板没有对应的从 TntUnicodeControls 控件, 这个地方的乱码问题要怎么才能解决呢 ?