就是实现Excel中的n个单元格合并成一个单元,并将其内容垂直,水平方向居中.

解决方案 »

  1.   

    利用EXCEL文件的VBA:
        Range(Sheet1.Cells(IStart, Col), Sheet1.Cells(IEnd, Col)).Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .ShrinkToFit = False
            .MergeCells = True
        End With
      

  2.   

    if (you know how to deal with Excel in Delphi) then
    begin
        UseMacroInExcelToRecordTheOperation;
        TryToEmulateInDelphi;
    end
    else
        ReferToThisHomepage('http://votr.nease.net');
        FindSomeDocForReference;
    end;
      

  3.   

    我用下面代码为什么会报错:
    [Error] ztszdlg.pas(2654): Record, object or class type required
    [Error] ztszdlg.pas(2657): Undeclared identifier: 'ColumnWidth'
    [Error] ztszdlg.pas(2658): Undeclared identifier: 'MergeCells'
    。。
    var oexcel,WorkBook: OleVariant;
        nrow,ncol: integer;
        tabname,xlsFileName,dir_name:string;
    begin
    if stringgrid1.RowCount<=2 then exit;
    savedialog1.Filter:='Excel files(*.xls)|*.xls';
    dir_name:=ExtractFilePath(Application.ExeName)+'ExcelBook\';
    if not directoryexists(dir_name) then
      createdir(dir_name);
    savedialog1.InitialDir:=dir_name;
    savedialog1.FileName:=edit5.Text+gcbh;
    if savedialog1.Execute=true then
     begin
       xlsFileName:=savedialog1.FileName;
       try
       oexcel:=GetActiveOleObject('Excel.Application');
       except
         try
          oexcel:=CreateOleObject('Excel.Application');
         except
          MessageDlg('无法启动EXCEL程序。'+#13+'请确定该程序已正确安装!',mtInformation,[mbOK],0);
          exit;
          end;
        end;
       WorkBook:=CreateOleobject('Excel.Sheet');
       workBook:=oexcel.workBooks.Add;
       for nrow:=1 to stringgrid1.RowCount-1 do
         begin
          for ncol:=1 to stringgrid1.ColCount-1 do
            begin
              oexcel.Cells[nrow,ncol]:=stringgrid1.Cells[ncol,nrow];
            end;
         end;
      with  oexcel.Range[oexcel.Cells[1, 3], oexcel.cells[3, 3]]do
    begin
    ColumnWidth := 8; // 宽度
    MergeCells := True; //合并单元格
    WrapText := True; //自动换行
    HorizontalAlignment := xlCenter; //对齐方式 xlleft,xlright
    VerticalAlignment := xlCenter; //对齐方式 xltop,xlbottom
    font.Bold:=true; //字体加重
    font.Italic := True ; //斜体
    font.Colorindex:=6; // 颜色
    font.Name:='宋体'; //字体
    font.FontStyle := '常规' ;
    font.Size:= 22; //大小
    end;   oexcel.Visible:=true;
       WorkBook.SaveAs(xlsFilename);
     end;
      

  4.   

    呵呵,其實這個功能我前一個星期就搞過,涉及到很復雜的合並.
    合並的代碼我沒有寫在DELPHI裡面,我是在EXCEL的模板裡面用VBA做的.
    其實在EXCEL裡面寫代碼很容易,因為第一,幫助是全中文的,很好看懂.第二,就算你不知道在EXCEL裡面哪些操作具體用什麼代碼可以實現,你也可以讓EXCEL自動生成你需要的代碼做為參考.方法就是通過"宏(繁體的OFFICE叫'巨集')"來進行.先選擇EXCEL主菜單裡面的工具---->宏----->錄制宏 命令
    隨便輸入一個名字後確定,然後就進行你想用程序來實現的操作.完成後,停止錄制,最後,你進入Visual Basic編輯器,就可以看到EXCEL根據你剛才的操作過程自動生成的VB代碼.
    附:
    可以把代碼寫在EXCEL文件的Worksheet_Change事件裡面.隻要你通過DELPHI往EXCEL輸出數據,就能激活該事件.事件的參數是(ByVal Target As Range),你可以在excel裡面隨便找個用不上的單元格,往裡面寫參數,可以通過Target.value得到寫入的數據.