我准备用Excel做一个报表,涉及到合并Excel单元格的问题,怎么样通过输入左上和右下的单元格的编号把他们中间的格子合并为一个格子???

解决方案 »

  1.   

    在delphi里可以这样写,不知道是不是你想要的
    var ExcelApp: Variant;
    begin
    ExcelApp := CreateOleObject( 'Excel.Application' );
    数据填写完成之后
    ExcelApp.ActiveSheet.Range['A1','C5'].Merge(true);
    上面这句话的功能就是将A1到C5的区域合并起来end;
      

  2.   

    yanfei0891(黑暗)已经写的很详细了,不知道你是不是想要有关excel方面的例子。
      

  3.   

    try
      EXC:=CreateOleObject('Excel.Application');
      Exc.Visible:=False;
      Exc.Caption := '成绩查询系统调用 Microsoft Excel';
      Exc.workBooks.add;   //添加工作簿
      exc.WorkSheets[1].Activate;
      aSheet:=EXC.workSheets.item[1];
     except
      ShowMessage('启动EXCEL失败'+#13+#10+'可能是未安装Microsoft EXCEL!');
     end;
     exc2:=exc.activesheet.Range['A1','D1'];  //指定合并范围
     Exc2.MERGE;    //合并
     EXC2.value:='学生成绩表';  //合并单元格的内容
      

  4.   

    黑暗的做法大致相同![email protected]  QQ;15184311
      

  5.   

    http://community.csdn.net/Expert/topic/4634/4634208.xml?temp=.6999018
      

  6.   

    For Row:=1 to RowDepth-1 Do
     Begin
       CapStr:=trim(exApp.Cells[Row,1].value);
       Col1:=2;
       Col2:=FldCount;
       For Col1:=2 to Col2 Do
       begin
         NewCapStr:=trim(exApp.Cells[Row,Col1].value);
         if (NewCapStr=CapStr) then
         Begin
           Cell1:=exApp.Cells.Item[Row,Col1-1];
           Cell2:=exApp.Cells.Item[Row,Col1];
           exApp.Cells[Row,Col1].value:='';
           exApp.Range[Cell1,Cell2].Merge(True);
         end
         else
         begin
           CapStr:=NewCapStr;
         end;
       end;
     end;
      

  7.   

    使用OLE方法,可以做EXCEL表里的任何动作。
      

  8.   

    我前几天写的,供参考:function Excel_ColTag(ColNum: Integer): String;
    var
        num1,num2: Integer;
        ls_ColCaption: String;
    begin
      //取excel的列标签,用于建立range
      num1 := ColNum div 26;
      num2 := ColNum mod 26;
      if(num1 > 0) then
        ls_ColCaption := String(char(64 +num1))+String(char(64 +num2))
      else
        ls_ColCaption := String(char(64 +num2));  Result := ls_ColCaption;
    end;//---------------------------------------------------------------------------
    procedure T_App.ExportToExcel(CurDataGrid: TDBGridEh; ls_Title, ls_Select: String; Total: Boolean);
    var
        ExcelApp, WorkBook,Range: Variant;
        i, j: Integer;
        ls_Str: String;
        li_RowNo: Integer;    nRowCount,nColCount: Integer;
        nColWidth : Integer;
        ls_FileName,ls_TitleName,ls_FieldName,ls_Value: String;
    begin
        try
            ExcelApp := CreateOleObject('Excel.Application');
            WorkBook := CreateOleObject('Excel.Sheet');
        except
            //"运行Excel出错,请确认安装了Office"
            SetMDsuiStop(LoadStr(10009),LoadStr(10005),1);
            Exit;
        end;    try
            SaveDialog1.Filter := 'Excel files (*.xls)|*.xls';
            SaveDialog1.DefaultExt := '.xls';
            if (SaveDialog1.Execute) then
            begin
                ls_FileName := SaveDialog1.FileName;
                if (ExtractFileExt(ls_FileName) = '') then
                    ls_FileName := ls_FileName + SaveDialog1.DefaultExt;
                if (UpperCase(ExtractFileExt(ls_FileName)) <> '.XLS') then
                begin
                    //"导出文件选择错误!"
                    SetMDsuiStop(LoadStr(10010),LoadStr(10005),1);
                    suiMessageDialog1.ShowModal;
                    Exit;
                end;
            end
            else
            begin
                Exit;
            end;
            if (FileExists(ls_FileName)) then
            begin
                //"文件已存在,是否覆盖!","提示"
                SetMDsuiInformation(LoadStr(10011),LoadStr(10006),2);
                if (suiMessageDialog1.ShowModal <> mrOk) then
                begin
                    Exit;
                end;
            end;//if fileexists(sfilename) then        Application.ProcessMessages;
            ExcelApp.Visible := True;
            WorkBook := ExcelApp.WorkBooks.Add;
            ExcelApp.WorkSheets[1].Activate;        // 表格的行数
            nRowCount := CurDataGrid.DataSource.DataSet.RecordCount + 1;
            if nRowCount < 2 then
                nRowCount := 2;
            // 表格的列数
            nColCount := CurDataGrid.Columns.Count;
            if nColCount < 1 then
                nColCount := 1;        //写报表标题
            if(ls_Title <> '') then    //A1
            begin
                ExcelApp.Rows[1].RowHeight := 25;
                ExcelApp.Cells(1, 1) := ls_Title;
                ls_Str := 'A1:'+Excel_ColTag(nColCount)+'1';
                ExcelApp.Range[ls_Str].Merge(true);
                ExcelApp.Range[ls_Str].HorizontalAlignment := $FFFFEFF4;
                ExcelApp.Range[ls_Str].VerticalAlignment := $FFFFEFF4;
                ExcelApp.Rows[1].Font.Bold := true;
                ExcelApp.Rows[1].Font.Size := 20;            nRowCount := nRowCount + 1;
                li_RowNo := li_RowNo + 1;
            end;
            //加一个查询条件    A2
            if (ls_Select <> '') then
            begin
                for i :=0 to nColCount do
                begin
                    //字段名是可视的
                    if (CurDataGrid.Columns.Items[i].Visible) then
                    begin
                        ExcelApp.Cells(2, i+1) := ls_Select;
                        ls_Str := 'A2:'+Excel_ColTag(nColCount)+'2';
                        ExcelApp.Range[ls_Str].Merge(true);
                        Break;
                    end
                end;            nRowCount := nRowCount + 1;
                li_RowNo := li_RowNo + 1;
            end;        // 设置单元格的宽度
            for i := 0 to nColCount - 1 do
            begin
                //字段名是可视的
                if (CurDataGrid.Columns.Items[i].Visible) then
                begin
                    nColWidth := CurDataGrid.Columns.Items[i].Width;
                    //vExcelApp.OlePropertyGet('Columns', i + 1).OlePropertySet('ColumnWidth', (nColWidth div 7)+2);
                    ExcelApp.Columns[i+1].ColumnWidth := (nColWidth div 7)+2;
                end
                else
                begin
                    ExcelApp.Columns[i+1].ColumnWidth := 0;
                end
            end;        // 先将列名写入Excel表格
            for j := 0 to CurDataGrid.Columns.Count - 1 do
            begin
                if CurDataGrid.Columns.Items[j].Visible then
                begin
                    //设置标题行的字体
                    ExcelApp.Rows[li_RowNo+1].Font.Bold := true;
                    //设置字体居中
                    ExcelApp.Rows[li_RowNo+1].HorizontalAlignment := $FFFFEFF4;                ls_TitleName := CurDataGrid.Columns.Items[j].Title.Caption;
                    ExcelApp.Cells(li_RowNo+1, j+1) := ls_TitleName;
                end;
            end;        // 将DBGrid中的数据写入Excel表格
            CurDataGrid.DataSource.DataSet.First;
            for i := li_RowNo to nRowCount - 2 do
            begin
                // 普通数据行的行高16
                //vExcelApp.OlePropertyGet('Rows', i + 2).OlePropertySet('RowHeight', 16);
                // 63 63 72 75 6E 2E 63 6F 6D
                for j := 0 to CurDataGrid.Columns.Count - 1 do
                begin
                    //字段名是可视的
                    if (CurDataGrid.Columns.Items[j].Visible) then
                    begin
                        ls_FieldName := CurDataGrid.Columns.Items[j].FieldName;                    if (CurDataGrid.DataSource.DataSet.FieldByName(ls_FieldName).IsNull) then
                        begin
                            Continue;
                        end;                    ls_Value := CurDataGrid.DataSource.DataSet.FieldByName(ls_FieldName).Value;
                        ExcelApp.Cells(i+2, j+1) := ls_Value;
                    end;            end;
                CurDataGrid.DataSource.DataSet.Next;
            end;        //设置边框
            //ls_Str = "A3:"+Excel_ColTag(nColCount)+IntToStr(nRowCount);
            ls_Str := 'A'+IntToStr(li_RowNo+1)+':'+Excel_ColTag(nColCount)+IntToStr(nRowCount);
            ExcelApp.Range[ls_Str].Borders.LineStyle := 1;
            //ExcelApp.Range[ls_Str].Borders[2].Weight  := 3;        {
          if Total then
          begin
            Col := 1;
            for J := 0 to DBGrid.Columns.Count - 1 do
            begin
              S := Char(64 + ((J+1) mod 26));
              if (J+1) > 26 then
              begin
                S := Char(65+(((J+1)-26) div 26)) + S;
              end;
              if J = 0 then
              begin
                ExcelApp.Cells(Row, Col) := '合计';
              end
              else if DBGrid.Columns[J].Field.DataType in [ftInteger, ftSmallint, ftFloat, ftBCD] then
              begin
                FieldName := DBGrid.Columns[J].FieldName;
                ExcelApp.Cells(Row, Col) := '=SUM('+S+'4:'+S+IntToStr(Row-1)+')';
              end;
              Col := Col + 1;
            end;
          end;
          }
            try
                WorkBook.SaveAs(ls_FileName);
            except
                SetMDsuiStop('要保存的文件不能与已打开的文件重名',LoadStr(10005),1);
                suiMessageDialog1.ShowModal;
            end;
        except
            SetMDsuiStop(LoadStr(10012),LoadStr(10005),1);
            suiMessageDialog1.ShowModal();
        end;    ExcelApp := Unassigned;
        WorkBook := Unassigned;
    end;