SaveAs()保存excel的文件类型问题

天空的云2020 2011-10-09 02:28:41

try
Excel.ActiveSheet.Rows[3].Delete;
Excel.ActiveSheet.Columns[5].Delete;
Excel.ActiveSheet.Columns[6].Delete;
Excel.Cells[1,1].Value := 'CARRIER';
Excel.Cells[1,2].Value := 'CI';
Excel.Cells[1,5].Value := 'changeType';
Excel.Cells[1,6].Value := 'success';
WorkBook.Worksheets[1].Name := 'Sheet1';
Excel.DisplayAlerts := true;
finally
Excel.Quit;
Excel := Unassigned;
end;

我打开一个EXCEL对它进行上面的操作然后退出EXCEL它提示是否对所做的修改进行保存,这时我手动的选择保存为97-2003工作薄。
这种做法没有任何问题!
但是我用
excel.WorkBooks[1].SaveAs('c:\Sheet1.xls');
保存得来的文件,打开时会提示你尝试打开的文件XX的格式与文件扩展名指定的格式不一致。

这个问题怎么解决?

我找个网上的两种方法都不行,一中是修改注册表里弄DefaultFormat的值为0x38(56),还有一种是saveas函数的一个参数指定保存格式也不行。那个参数有好多EXCEL的格式。看不懂
XlFileFormat

Constant Value
xlExcel2 16
xlExcel2FarEast 27
xlExcel3 29
xlExcel4 33
xlExcel4Workbook 35
xlExcel5 39

我的EXCEL是2007版。2003版也试了还是不行。

...全文
1282 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
天空的云2020 2011-10-09
  • 打赏
  • 举报
回复
多谢。查了半天资料才找到FileFormat参数的用法
excel.WorkBooks[1].SaveAs('c:\Sheet1.xls',39);
这样就OK了

浩南_哥 2011-10-09
  • 打赏
  • 举报
回复
不太清楚你的filename指向的excel是什么样的内容,你的代码我试了几个简单的Excel没什么问题。
SaveAs函数参数含义:
http://www.feiesoft.com/vba/excel/xlmthSaveAs.htm
FileFormat参数MSDN解释:

xlAddIn Microsoft Office Excel Add-In.
xlCSV Comma separated value.
xlCSVMac Comma separated value.
xlCSVMSDOS Comma separated value.
xlCSVWindows Comma separated value.
xlCurrentPlatformText Specifies a type of text format
xlDBF2 Dbase 2 format.
xlDBF3 Dbase 3 format.
xlDBF4 Dbase 4 format.
xlDIF Data Interchange format.
xlExcel2 Excel version 2.0.
xlExcel2FarEast Excel version 2.0 far east.
xlExcel3 Excel version 3.0.
xlExcel4 Excel version 4.0.
xlExcel4Workbook Excel version 4.0. Workbook format.
xlExcel5 Excel version 5.0.
xlExcel7 Excel 95.
xlExcel9795 Excel version 95 and 97.
xlHtml Web page format.
xlIntlAddIn Microsoft Office Excel Add-In international format.
xlIntlMacro Deprecated format.
xlSYLK Symbolic link format.
xlTemplate Excel template format.
xlTextMac Specifies a type of text format.
xlTextMSDOS Specifies a type of text format.
xlTextPrinter Specifies a type of text format.
xlTextWindows Specifies a type of text format.
xlUnicodeText Specifies a type of text format.
xlWebArchive MHT format.
xlWJ2WD1 Deprecated format.
xlWJ3 Deprecated format.
xlWJ3FJ3 Deprecated format.
xlWK1 Lotus 1-2-3 format.
xlWK1ALL Lotus 1-2-3 format.
xlWK1FMT Lotus 1-2-3 format.
xlWK3 Lotus 1-2-3 format.
xlWK3FM3 Lotus 1-2-3 format.
xlWK4 Lotus 1-2-3 format.
xlWKS Lotus 1-2-3 format.
xlWorkbookNormal Excel workbook format.
xlWorks2FarEast Microsoft Works 2.0 format
xlWQ1 Quattro Pro format.
xlXMLSpreadsheet Excel Spreadsheet format.

天空的云2020 2011-10-09
  • 打赏
  • 举报
回复

unit u_formatXls;

interface

uses
Variants, ComObj;
//格式化小区切换表xls
function formatChange(fileName:string):Boolean;

implementation

function formatChange(fileName:string):Boolean;
var
Excel,WorkBook: OleVariant;
Sheet: Variant;
i:integer;
begin
try
excel := CreateOleObject('Excel.Application');
WorkBook := excel.Workbooks.Add(fileName);
Sheet := WorkBook.Worksheets[1];
except
result := false ;
EXIT;
end;

try
//excel.Visible:=true; //显示EXCEL
Excel.ActiveSheet.Rows[3].Delete;
Excel.ActiveSheet.Rows[2].Delete;
Excel.ActiveSheet.Rows[1].Delete;
Excel.ActiveSheet.Columns[21].Delete;
Excel.ActiveSheet.Columns[20].Delete;
Excel.ActiveSheet.Columns[19].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[1].Delete;
Excel.ActiveSheet.Columns[3].Delete;
Excel.ActiveSheet.Columns[5].Delete;
Excel.ActiveSheet.Columns[6].Delete;
Excel.Cells[1,1].Value := 'CARRIER';
Excel.Cells[1,2].Value := 'CI';
Excel.Cells[1,3].Value := 'NCARRIER';
Excel.Cells[1,4].Value := 'NCI';
Excel.Cells[1,5].Value := 'changeType';
Excel.Cells[1,6].Value := 'success';
Excel.Cells[1,7].Value := 'invalidPilotFail';
Excel.Cells[1,8].Value := 'blockFail';
Excel.Cells[1,9].Value := 'otherFail';
WorkBook.Worksheets[1].Name := 'Sheet1';
excel.WorkBooks[1].SaveAs('c:\Sheet1.xls',excel.XlFileFormat.xlExcel5);
finally
Excel.Quit;
Excel := Unassigned;
end;

result := true ;
end;

end.
天空的云2020 2011-10-09
  • 打赏
  • 举报
回复
saveas()函数的XlFileFormat参数有下面这些值: xlAddIn xlCSV xlCSVMac xlCSVMSDOS xlCSVWindows xlCurrentPlatformText xlDBF2 xlDBF3 xlDBF4 xlDIF xlExcel2 xlExcel2FarEast xlExcel3 xlExcel4 xlExcel4Workbook xlExcel5 xlExcel7 xlExcel9795 xlHTML xlIntlAddIn xlIntlMacro xlSYLK xlTemplate xlTextMac xlTextMSDOS xlTextPrinter xlTextWindows xlUnicodeText xlWJ2WD1 xlWK1 xlWK1ALL xlWK1FMT xlWK3 xlWK4 xlWK3FM3 xlWKS xlWorkbookNormal xlWorks2FarEast xlWQ1 xlWJ3 xlWJ3FJ3
哪个是97-2003工作簿类型?还有这个参数怎么用?

excel.WorkBooks[1].SaveAs('c:\Sheet1.xls',excel.XlFileFormat.xlExcel5)
提示project project1.exe raised exception class EOleError with message 'Method'XlFileFormat' not supported by automation object'.Process stopped.Use Step or Run to continue
浩南_哥 2011-10-09
  • 打赏
  • 举报
回复
把你的代码贴全

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧