delphi ole excel容器,保存文件

时间:2012-01-30 16:19:16

标签: delphi excel delphi-2009 ole

Delphi中这个c++ question的语法是什么?

使用oleContainer后,尝试执行无效的SaveDocumentAs。我认为这可能是一个很好的选择。

更新:感谢您的翻译,但是在调用

后,excel文件会被解决
var
  ExcelOle: TOleContainer;
begin
  ExcelOLE.CreateObjectFromFile(FileName, False);
  ExcelOle.OleObject.application.workbooks[1].save;
end;

1 个答案:

答案 0 :(得分:11)

只需创建一个Ole对象:

uses ComObj, Excel;

var Excel: OleVariant;
Excel := CreateOleObject('EXCEL.Application');
Excel.Application.Workbooks.Add;
Excel.Application.Workbooks[1].SaveAs('c:\test.xlsx', xlWorkbookDefault); // or xlOpenXMLWorkbook (51)
Excel.Application.Quit;

Excel.pas

unit Excel;

interface

uses Windows, Activex;

// originally from Excel97.pas
// XlFileFormat constants
type
  XlFileFormat = TOleEnum;
const
  xlAddIn = $00000012;
  xlCSV = $00000006;
  xlCSVMac = $00000016;
  xlCSVMSDOS = $00000018;
  xlCSVWindows = $00000017;
  xlDBF2 = $00000007;
  xlDBF3 = $00000008;
  xlDBF4 = $0000000B;
  xlDIF = $00000009;
  xlExcel2 = $00000010;
  xlExcel2FarEast = $0000001B;
  xlExcel3 = $0000001D;
  xlExcel4 = $00000021;
  xlExcel5 = $00000027;
  xlExcel7 = $00000027;
  xlExcel9795 = $0000002B;
  xlExcel4Workbook = $00000023;
  xlIntlAddIn = $0000001A;
  xlIntlMacro = $00000019;
  xlWorkbookNormal = $FFFFEFD1;
  xlSYLK = $00000002;
  xlTemplate = $00000011;
  xlCurrentPlatformText = $FFFFEFC2;
  xlTextMac = $00000013;
  xlTextMSDOS = $00000015;
  xlTextPrinter = $00000024;
  xlTextWindows = $00000014;
  xlWJ2WD1 = $0000000E;
  xlWK1 = $00000005;
  xlWK1ALL = $0000001F;
  xlWK1FMT = $0000001E;
  xlWK3 = $0000000F;
  xlWK4 = $00000026;
  xlWK3FM3 = $00000020;
  xlWKS = $00000004;
  xlWorks2FarEast = $0000001C;
  xlWQ1 = $00000022;
  xlWJ3 = $00000028;
  xlWJ3FJ3 = $00000029;
  xlExcel12 =50;
  xlExcel8 = 56;
  xlHtml = 44;
  xlOpenXMLAddIn = 55;
  xlOpenXMLTemplate = 54;
  xlOpenXMLTemplateMacroEnabled = 53;
  xlOpenXMLWorkbook = 51;
  xlOpenXMLWorkbookMacroEnabled = 52;
  xlTemplate8 = 17;
  xlUnicodeText = 42;
  xlWebArchive = 45;
  xlWorkbookDefault = 51;
  xlXMLSpreadsheet = 46;

implementation

end.