这几天做一个项目,要求显示word及excel文件(仅仅显示),查了n多的资料,鼓捣出以下动态链接库,现无偿提供给大家,这可是我几天的心血啊!顺便请你们帮我测试一下,并提提意见,没问题吧?
程序在winxp,office2000 Premium下测试通过library OfficeToHtml;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Classes,
  Windows,
  Messages,
  Variants,
  Dialogs,
  Word2000,
  Excel2000,
  OleServer,
  OleCtnrs,
  ComObj;//StdCtrls,OleCtrls,SHDocVw,Graphics,Controls, Forms,{$R *.res}function DocToHtml(SourceFilename:PChar;DesFileName:PChar):bool;stdcall;
var
    Template, TempFalse, ItemIndex, Filename,
    FileFormat, Filename2,
    SaveChanges, OriginalFormat, RouteDocument: OleVariant;
    readonly:OleVariant;
    Wordapplication:TWordApplication;
    WordDocument : TWordDocument;
    resultValue : boolean;
    //temp1 : pChar;
    //temp2 : PChar;
begin
     Template := EmptyParam;
     TempFalse := false;
     readonly := true;
     ItemIndex := 1;
     Wordapplication := TWordApplication.Create(nil);
     WordDocument := TWordDocument.Create(nil);
     //getMem(temp1,50);
     //getmem(temp2,50);
     //temp1 := strcopy(temp1,SourceFileName);
     //temp2 := strCopy(temp2,desfilename);
     try
        Wordapplication.Connect;
     except
        ShowMessage('无法启动Word。请检查是否已安装。');
        Wordapplication.Free;
        WordDocument.Free;
        result := false;
        exit;
     end;
     //FileName := StrPas(temp1);
     //filename2 := strPas(temp2);
     //freemem(temp1);
     //freemem(temp2);
     FileName := StrPas(SourceFileName);
     filename2 := strPas(desfilename);
     fileformat := wdFormatHtml;
     Wordapplication.Visible := false;
     {Open a document}
     WordApplication.Options.CheckSpellingAsYouType := False; //拼写检查
     WordApplication.Options.CheckGrammarAsYouType := False;
     WordApplication.Documents.Open(Filename,Template,readonly,TempFalse,Template,Template,Template,Template,Template,Template,Template,Template);
     {Assign WordDocument component}
     WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
     // ****************** Save As ***********************
     try
        WordDocument.SaveAs(Filename2, FileFormat);
     except
        ShowMessage('无法保存文档。请检查WORD是否被关闭。');
        Wordapplication.Free;
        WordDocument.Free;
        result := false;
        exit;
     end;
     // ****************** Close Document ***********************
     resultValue := true;
     SaveChanges := WdDoNotSaveChanges;
     OriginalFormat := UnAssigned;
     RouteDocument := UnAssigned;
     try
        Wordapplication.Quit(SaveChanges, OriginalFormat, RouteDocument);
        WordDocument.Disconnect;
        Wordapplication.Disconnect;
        Wordapplication.Free;
        WordDocument.Free;
     except
        ShowMessage('无法关闭文档。请检查WORD是否异常。');
     end;
     result := resultValue;
end;
function XlsToHtml(SourceFilename:PChar;DesFileName:PChar):bool;stdcall;
var
    ItemIndex, Filename,
    FileFormat, Filename2,
    readonly:OleVariant;
    ExcelWorkSheet : TExcelWorkSheet;
    ExcelWorkBook: TExcelWorkBook;
    ExcelApplication : TExcelApplication;
begin
     readonly := true;
     ItemIndex := 1;
     ExcelApplication := TExcelApplication.Create(nil);
     ExcelWorkBook := TExcelWorkBook.Create(nil);
     ExcelWorkSheet := TExcelWorkSheet.Create(nil);
     try
        ExcelApplication.Connect;
     except
        ShowMessage('无法启动Excel。请检查是否已安装。');
        ExcelWorkSheet.Free;
        ExcelWorkBook.Free;
        ExcelApplication.Free;
        result := false;
        exit;
     end;
     FileName := strPas(SourceFileName);
     filename2 := strPas(DesFileName);
     fileformat := xlHtml;
     ExcelApplication.Visible[0] := false;
     //Open a document
     ExcelApplication.Workbooks.Open(Filename,EmptyParam,readonly,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
     //assign WordDocument component
     ExcelWorkBook.ConnectTo(ExcelApplication.Workbooks.Item[ItemIndex]);
     ExcelWorkSheet.ConnectTo(ExcelWorkBook.Activesheet as _Worksheet);
     // ****************** Save As ***********************
     try
        if fileexists(filename2) then
            deleteFile(DesFileName);
        ExcelWorkSheet.SaveAs(Filename2, FileFormat);
     except
        ShowMessage('无法保存文档。请检查Excel是否被关闭。');
        ExcelWorkSheet.Free;
        ExcelWorkBook.Free;
        ExcelApplication.Free;
        result := false;
        exit;
     end;
     // ****************** Close Document ***********************
     try
        ExcelApplication.Quit();
        ExcelWorkSheet.Disconnect;
        ExcelWorkBook.Disconnect;
        ExcelApplication.Disconnect;
     finally
        ExcelWorkSheet.Free;
        ExcelWorkBook.Free;
        ExcelApplication.Free;
     end;
     result := true;
end;exports
XlsToHtml,DocToHtml;
begin
end.在C++ builder中的调用(在delphi中的调用自己写啦):
头文件:typedef (__stdcall *DocToHtml)(char * SourceFilename,char * DesFileName);
typedef (__stdcall *XlsToHtml)(char * SourceFilename,char * DesFileName);
bool ConvertDocToHtml(AnsiString sourceFileName,AnsiString DesFileName);
bool ConvertXlsToHtml(AnsiString sourceFileName,AnsiString DesFileName);cpp文件中:bool ConvertDocToHtml(AnsiString sourceFileName,AnsiString DesFileName)
{
   DocToHtml  pFunc;
   HINSTANCE Module;
   AnsiString strFileName;   PChar  txt1;
   PChar  txt2;
   Module = LoadLibrary((ExtractFilePath(ParamStr(0))+"\\dll\\OfficeToHtml.dll").c_str());
   if (Module == NULL)
   {
       data->showConstMessage("找不到动态链接库");
       return false;
   }
   txt1 = PChar(sourceFileName.c_str());
   txt2 = PChar(DesFileName.c_str());
   Screen->Cursor = crHourGlass;
   (void __stdcall *)pFunc = GetProcAddress(Module, "DocToHtml");
   if( pFunc == NULL)
   {
       data->showConstMessage("取动态连接库中函数错误");
       return false;
   }
   pFunc(txt1,txt2);
   FreeLibrary(Module);
   Screen->Cursor = crDefault;
   return true;
}bool ConvertXlsToHtml(AnsiString sourceFileName,AnsiString DesFileName)
{
   DocToHtml  pFunc;
   HINSTANCE Module;
   AnsiString strFileName;   PChar  txt1;
   PChar  txt2;
   Module = LoadLibrary((ExtractFilePath(ParamStr(0))+"\\dll\\OfficeToHtml.dll").c_str());
   if (Module == NULL)
   {
       data->showConstMessage("找不到动态链接库");
       return false;
   }
   txt1 = PChar(sourceFileName.c_str());
   txt2 = PChar(DesFileName.c_str());
   Screen->Cursor = crHourGlass;
   (void __stdcall *)pFunc = GetProcAddress(Module, "XlsToHtml");
   if( pFunc == NULL)
   {
       data->showConstMessage("取动态连接库中函数错误");
       return false;
   }
   pFunc(txt1,txt2);
   FreeLibrary(Module);
   Screen->Cursor = crDefault;
   return true;
}