将格式从excel工作表复制到Delphi中的另一个工作簿工作表

时间:2015-05-04 23:04:23

标签: excel delphi copy range cells

  

在工作表中有:

This is a header

我想取消合并B2:C2格式并将其格式复制到A1处的另一张(在另一张工作簿中),然后将值(在C3中)复制到另一张的B1单元格,D2到A2,E2到B2等...... 我该怎么办?

This is a detail

procedure Something;
const root = 'C:\vial\$$652791.xls';
     filename = 'C:\vial\abarajame.xls';
var xlsH, xlsD: TExcelApplication;
    sheetH,sheetD: _Worksheet;
    DestRange: OleVariant;
    format: string;
begin
    xlsH := TExcelApplication.Create(Self);
    xlsD := TExcelApplication.Create(Self);
    xlsH.Workbooks.Open(root, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0 );
    xlsD.Workbooks.Open(filename, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0 );
    sheetH := xlsH.Worksheets[1] as _Worksheet;
    sheetD := xlsD.Worksheets[1] as _Worksheet;
    //Insert 5 empty rows
    sheetD.Rows.Range['A1','A5'].EntireRow.Insert(xlShiftDown, Null);
    //1st. col.
    sheetH.Cells.Range['B2','I2'].MergeCells := False;
    DestRange := sheetH.Cells.Range['B2','I2'];
    format    := sheetH.Cells.Range['B2','I2'].NumberFormat;
    sheetD.Cells.Range['A1','A1'].Copy(DestRange);
    sheetD.Cells.Range['A1','A1'].NumberFormat := format;
    DestRange := sheetH.Range['C3','C3'];
    format    := sheetH.Range['C3','C3'].NumberFormat;
    sheetD.Range['A2','A2'].Copy(DestRange);
    sheetD.Range['A2','A2'].NumberFormat := format;
    //2nd. col.
    DestRange := xlsH.Range['D2','D2'];
    xlsD.Range['B1','B1'].Copy(DestRange);
    DestRange := xlsH.Range['C4','C4'];
    hojaD.Range['B2','B2'].Copy(DestRange);
    xlsd.SaveWorkspace;
    xlsH.Workbooks.Close(0);
    xlsD.Workbooks.Close(0);
    FreeAndNil(xlsH);
    FreeAndNil(xlsD);
end;

我已尝试过不同的方法,Cells.Range,Sheet from Sheet,来自ExcelApplication的Cells,但没有做什么!!

结果必须是:

That I want to do

1 个答案:

答案 0 :(得分:0)

这是模式:

 sheetD.Rows.Range['A1','A5'].EntireRow.Insert(xlShiftDown, Null);
  //1st. col.
  sheetH.Range['B2','B2'].Copy(sheetD.Range['A1','A1']);
  sheetH.Range['C3','C3'].Copy(sheetD.Range['B1','B1']);
  //2nd. col.
  sheetH.Range['D2','D2'].Copy(sheetD.Range['A2','A2']);
  sheetH.Range['D3','D3'].Copy(sheetD.Range['B2','B2']);
  //3rd. col.
  sheetH.Range['E2','E2'].Copy(sheetD.Range['A3','A3']);
  sheetH.Range['E3','E3'].Copy(sheetD.Range['B3','B3']);
  //4th.col
  sheetH.Range['F2','F2'].Copy(sheetD.Range['A4','A4']);
  sheetH.Range['F3','F3'].Copy(sheetD.Range['B4','B4']);
  // col.1
  sheetH.Range['G2','G2'].Copy(sheetD.Range['C1','C1']);
  sheetH.Range['G3','G3'].Copy(sheetD.Range['D1','D1']);
  // col.2
  sheetH.Range['H2','H2'].Copy(sheetD.Range['C2','C2']);
  sheetH.Range['H3','H3'].Copy(sheetD.Range['D2','D2']);
  // col.3
  sheetH.Range['I2','I2'].Copy(sheetD.Range['C3','C3']);
  sheetH.Range['I3','I3'].Copy(sheetD.Range['D3','D3']);