现有系统中已经可以导出和导入xls文件类型,现在要求可以导出导入为WPS文档类型的。我看了下wps表格和xls表格格式都是差不多的。请高人指教一下,我现在是不是只需要在导出或者导入的时候增加WPS类型的选项就可以了?部分源代码如下:
//得到EXCEL文件的文件名及路径。
function TDbConnXLS.getXLSFileName: String;
var
openXLS:TOpenDialog;
begin
result:='';
openXLS:=TOpenDialog.Create(nil);
openXLS.Filter:='EXCEL(*.xls)|*.xls';
if openXLS.Execute then
result:=openXLS.FileName;
//else Cap_error('文件打开失败!');
end;//初始化
Function TDbConnXLS.init(cap_ShowHand:Tcap_ShowHandle):boolean;
var
resval:Rblstr;
begin
result:=false;
fileName:=getXLSFileName;
if fileName='' then exit;
if copy(fileName,length(fileName)-2,length(fileName))<>'xls' then
begin
cap_ShowHand.cap_error('您选中的文件不是XLS文件,请重新选择!');
exit;
end;
resval:=cap_ShowHand.Cap_Inbox('打开表','请输入EXCEL文件中的表名: ','Sheet1');
if resval.bool = false then exit; tableName:=resval.restr;
if tableName='' then
begin
cap_ShowHand.cap_error('表名不能为空!请输入表名!');
exit;
end; connstr:='';
connstr:='Provider=Microsoft.Jet.OLEDB.4.0;';
connstr:=connstr+'Data Source='+fileName+';';
connstr:=connstr+'Extended Properties=Excel 8.0;';
connstr:=connstr+'Persist Security Info=False;';
adoConn:=TADOConnection.Create(nil);
adoConn.ConnectionString := connstr;
adoConn.LoginPrompt:=false; adoQuery:=TADOQUery.Create(nil);
adoQuery.Connection:=adoConn;
result:=true;
end;
//title Excel表中的列名
//将listView的内容保存到Excel中
function TDbConnXLS.SavetoExcel(title: WideString; lv: TListView;cap_ShowHand:Tcap_ShowHandle): boolean;
var
i:integer;
SDG:TSaveDialog;
issave:boolean;
titlelist,templist:Tstrings;
begin
SDG:=TSaveDialog.Create(nil);
templist:=Tstringlist.Create;
titlelist:=Tstringlist.Create;
try
if lv.Items.Count = 0 then
begin
cap_ShowHand.cap_error('当前没有数据!');
exit;
end; if title = '' then //为空时导出所有列
for i:=0 to lv.Columns.Count-1 do //收集EXCEL表列名
titlelist.Add(lv.Columns[i].Caption)
else //不为空时,只导出指定列
titlelist.CommaText:=title; SDG.Filter:='EXCEL(*.xls)|*.xls';
SDG.FileName:='我的EXCEL文档.xls'; if SDG.Execute = true then
begin
isSave:=true;
if FileExists(SDG.FileName) then
issave:=cap_ShowHand.Cap_warn('文件已存在,是否替换!');
end
else exit;
if issave then
begin
templist:=(getListViewValue(titlelist,lv));
templist.SaveToFile(SDG.FileName);
cap_ShowHand.Cap_succeed('导出成功!');
end
else exit;
finally
templist.Free;
end;
end;还没贴完,等会回去精简一下。试问,我是不是就只需要在保存或者打开的时候增加wps类型?还有该怎么增加?还有上面有个链接xls文件的链接onnstr:='';
connstr:='Provider=Microsoft.Jet.OLEDB.4.0;';
connstr:=connstr+'Data Source='+fileName+';';
connstr:=connstr+'Extended Properties=Excel 8.0;';
connstr:=connstr+'Persist Security Info=False;';
这个玩意。如果我链接为wps 类型的我该怎么更改。等!!!急急急。。
//得到EXCEL文件的文件名及路径。
function TDbConnXLS.getXLSFileName: String;
var
openXLS:TOpenDialog;
begin
result:='';
openXLS:=TOpenDialog.Create(nil);
openXLS.Filter:='EXCEL(*.xls)|*.xls';
if openXLS.Execute then
result:=openXLS.FileName;
//else Cap_error('文件打开失败!');
end;//初始化
Function TDbConnXLS.init(cap_ShowHand:Tcap_ShowHandle):boolean;
var
resval:Rblstr;
begin
result:=false;
fileName:=getXLSFileName;
if fileName='' then exit;
if copy(fileName,length(fileName)-2,length(fileName))<>'xls' then
begin
cap_ShowHand.cap_error('您选中的文件不是XLS文件,请重新选择!');
exit;
end;
resval:=cap_ShowHand.Cap_Inbox('打开表','请输入EXCEL文件中的表名: ','Sheet1');
if resval.bool = false then exit; tableName:=resval.restr;
if tableName='' then
begin
cap_ShowHand.cap_error('表名不能为空!请输入表名!');
exit;
end; connstr:='';
connstr:='Provider=Microsoft.Jet.OLEDB.4.0;';
connstr:=connstr+'Data Source='+fileName+';';
connstr:=connstr+'Extended Properties=Excel 8.0;';
connstr:=connstr+'Persist Security Info=False;';
adoConn:=TADOConnection.Create(nil);
adoConn.ConnectionString := connstr;
adoConn.LoginPrompt:=false; adoQuery:=TADOQUery.Create(nil);
adoQuery.Connection:=adoConn;
result:=true;
end;
//title Excel表中的列名
//将listView的内容保存到Excel中
function TDbConnXLS.SavetoExcel(title: WideString; lv: TListView;cap_ShowHand:Tcap_ShowHandle): boolean;
var
i:integer;
SDG:TSaveDialog;
issave:boolean;
titlelist,templist:Tstrings;
begin
SDG:=TSaveDialog.Create(nil);
templist:=Tstringlist.Create;
titlelist:=Tstringlist.Create;
try
if lv.Items.Count = 0 then
begin
cap_ShowHand.cap_error('当前没有数据!');
exit;
end; if title = '' then //为空时导出所有列
for i:=0 to lv.Columns.Count-1 do //收集EXCEL表列名
titlelist.Add(lv.Columns[i].Caption)
else //不为空时,只导出指定列
titlelist.CommaText:=title; SDG.Filter:='EXCEL(*.xls)|*.xls';
SDG.FileName:='我的EXCEL文档.xls'; if SDG.Execute = true then
begin
isSave:=true;
if FileExists(SDG.FileName) then
issave:=cap_ShowHand.Cap_warn('文件已存在,是否替换!');
end
else exit;
if issave then
begin
templist:=(getListViewValue(titlelist,lv));
templist.SaveToFile(SDG.FileName);
cap_ShowHand.Cap_succeed('导出成功!');
end
else exit;
finally
templist.Free;
end;
end;还没贴完,等会回去精简一下。试问,我是不是就只需要在保存或者打开的时候增加wps类型?还有该怎么增加?还有上面有个链接xls文件的链接onnstr:='';
connstr:='Provider=Microsoft.Jet.OLEDB.4.0;';
connstr:=connstr+'Data Source='+fileName+';';
connstr:=connstr+'Extended Properties=Excel 8.0;';
connstr:=connstr+'Persist Security Info=False;';
这个玩意。如果我链接为wps 类型的我该怎么更改。等!!!急急急。。
只用导出为excel的格式的,用wps依然能打开。
业务部门说什么,我们就是狗一样的。。得照做。
他们机器装WPS,不一定装excel.我试过把导入导出代码格式换成.et是可以导入WPS的,但是现在两个都要支持,主要是下面代码
SDG.Filter:='EXCEL(*.xls)|*.xls';
SDG.FileName:='我的EXCEL文档.xls';
只有excel,怎么把WPS也添加进去????
SDG.Filter:='EXCEL(*.xls)|*.xls';
SDG.FileName:='我的EXCEL文档.xls';
这个怎么改?
Filter:string
作用:指定打开对话框,所有可用的过滤类型
格式:
a)描述|*.扩展名
b)指定多个过滤时,依次往后写:描述|*.扩展名|描述|*.扩展名
c)一个过滤器过滤多种格式时(用分号分割):描述|*.扩展名;*.扩展名
注意:|左右侧不要有空格;
我直接调用WPS打开,EXCEL文件。
一点问题都没有。