电竞比分网-中国电竞赛事及体育赛事平台

分享

通過Excel控件進(jìn)行數(shù)據(jù)的打印或者預(yù)覽

 quasiceo 2013-02-25
如何把Excel預(yù)覽窗口嵌入我的程序做報(bào)表? 瀏覽:157
加入我的收藏
我用Excel做報(bào)表,在預(yù)覽的時(shí)候希望窗口和我的Form集成。如下圖,請(qǐng)教如何實(shí)現(xiàn):
按此在新窗口瀏覽圖片

別說用其它報(bào)表工具,因?yàn)镋xcel的通用性極好,函數(shù)豐富。生成的圖表能二次編輯,隨著數(shù)據(jù)列的變化而變化。外發(fā)也不用培訓(xùn),過濾,排序,篩選到其它位置等都很容易實(shí)現(xiàn)了,這是其它工具做不到的。
----------------------------------------------
-


男 feverkim2k3 (無賴飛豬) ▲▲▲▲△ -
盒子活躍會(huì)員
2013-2-24 16:02:43
報(bào)表區(qū)域用一個(gè)OLEContainer來代替就是了。然后打開報(bào)表調(diào)用
OleContainer1.CreateObjectFromFile('d:\Format.xls',False); 
這樣就會(huì)自動(dòng)打開EXCEL文件。
此帖子包含附件:
JPEG 圖像
大小:85.5K
----------------------------------------------
-
男 sbzldlb (邊緣人) ▲▲▲▲△ -
盒子活躍會(huì)員
2013-2-24 19:16:41
ole是可以的
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('請(qǐng)安裝EXCEL再打?。?, '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2;  //頁面水平居中
//      ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2;   //頁面垂直居中
      //ExcelApp.Cells.Select;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //設(shè)為自動(dòng)列寬,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
 
 
      
      ExcelApp.ActiveSheet.PrintPreview;
      //ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
end;

通過Excel控件進(jìn)行數(shù)據(jù)的打印或者預(yù)覽

 下面的單元是利用Excel,進(jìn)行數(shù)據(jù)的打印功能。
函數(shù)包括了PrintView、PrintOut
參數(shù)含義:
FileName:需要打印的Excel文件路徑
AUtoFile:是否列寬字段適應(yīng)
Range:指定某一個(gè)格子選擇 默認(rèn)是空
 
具體是否頁面垂直或者水平居中,看實(shí)際的情況
--------------------------------------------------------------------------------------------------
 
unit uExcelPrint;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ComObj;
 
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
 
implementation
 
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('請(qǐng)安裝EXCEL再打??!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2;  //頁面水平居中
//      ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2;   //頁面垂直居中
      //ExcelApp.Cells.Select;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //設(shè)為自動(dòng)列寬,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
 
 
      
      ExcelApp.ActiveSheet.PrintPreview;
      //ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
end;
 
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('請(qǐng)安裝EXCEL再打??!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      //ExcelApp.ActiveSheet.PrintPreview;
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //設(shè)為自動(dòng)列寬,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
      ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
 
end;
 
end.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多