Excel文件复制粘贴

  1. use ComObj;
  2. procedure TFm_main.BitBtn1Click(Sender: TObject);
  3. var SourceFile: string;
  4.   Sourcexlapp, Sourcexlsheet, xlapp, xlsheet: variant;
  5.   RowNum, RowJ, Cols, i: integer;
  6. begin
  7.   try
  8.     Tbutton(sender).Enabled := false;
  9.     if trim(edit1.Text) = '' then
  10.     begin
  11.       if OpenDialog1.Execute then
  12.         edit1.Text := OpenDialog1.FileName;
  13.     end;
  14.     RowNum := strtoint(trim(MaskEdit1.Text));
  15.     Cols := strtoint(trim(MaskEdit3.Text));
  16.     screen.Cursor := crHourGlass;
  17.     SourceFile := trim(edit1.Text);
  18.     try
  19.       Sourcexlapp := createoleobject('excel.application');
  20.     except
  21.       showmessage('not found excel in your system, so can not create file!');
  22.       exit;
  23.     end;
  24.     Sourcexlapp.Visible := false;
  25.     Sourcexlapp.workbooks.open(SourceFile);
  26.     Sourcexlsheet := Sourcexlapp.ACTIVESHEET;
  27.     try
  28.       xlapp := createoleobject('excel.application');
  29.     except
  30.       showmessage('not found excel in your system, so can not create file!');
  31.       exit;
  32.     end;
  33.     xlapp.workbooks.add; //添加新工作簿
  34.     xlapp.visible := true;
  35.     xlsheet := xlapp.activesheet;
  36.     for i := 1 to Cols do
  37.     begin
  38.       xlsheet.columns[i].columnwidth := Sourcexlsheet.columns[i].columnwidth;
  39.     end;
  40.     RowJ := 1; i := RowNum + 1;
  41.     while i <= 2000 do
  42.     begin
  43.       if length(trim(Sourcexlsheet.cells[i, 1])) = 0 then break;
  44.     //copy title
  45.       Sourcexlapp.Range[Sourcexlsheet.cells[11], Sourcexlsheet.cells[RowNum, Cols]].select;
  46.       Sourcexlapp.selection.copy;
  47.       xlapp.Range[xlsheet.cells[Rowj, 1], xlsheet.cells[Rowj, 1]].select;
  48.       xlsheet.Paste;
  49.     //copy data
  50.       inc(Rowj, RowNum);
  51.       Sourcexlapp.Range[Sourcexlsheet.cells[i, 1], Sourcexlsheet.cells[i, Cols]].select;
  52.       Sourcexlapp.selection.copy;
  53.       xlapp.Range[xlsheet.cells[Rowj, 1], xlsheet.cells[Rowj, 1]].select;
  54.       xlsheet.Paste;
  55.       inc(Rowj, 2);
  56.       inc(i);
  57.     end;
  58.     try
  59.       Sourcexlapp.Quit;
  60.     except
  61.     end;
  62.     VarClear(Sourcexlapp);
  63.     VarClear(Sourcexlsheet);
  64.     VarClear(xlapp);
  65.     VarClear(xlsheet);
  66.   finally
  67.     screen.Cursor := crDefault;
  68.     Tbutton(sender).Enabled := true;
  69.   end;
  70. end;
  71. Word文档  use ComObj,WordXP;
  72. procedure Pub_TChartToWord(F_TChart: TChart);
  73. var i: integer;
  74.   wordapp: variant;
  75. begin
  76.   if F_TChart = nil then exit;
  77.   try
  78.     try
  79.       wordapp := createoleobject('word.application');
  80.     except
  81.       showmessage('not found word in your system, so can not create file!');
  82.       exit;
  83.     end;
  84.     wordapp.visible := true;
  85.     wordapp.Documents.Add(DocumentType := wdNewBlankDocument);
  86.     F_TChart.CopyToClipboardBitmap;
  87.     wordapp.Selection.Paste;
  88.     for i := 0 to F_TChart.Series[0].Count - 1 do
  89.     begin
  90.       wordapp.Selection.TypeParagraph;
  91.       wordapp.Selection.TypeText(F_TChart.Series[0].XLabel[i] + '  ' + FloatToStr(F_TChart.Series[0].YValue[i]));
  92.     end;
  93.     wordapp.Selection.TypeParagraph;
  94.     wordapp.Selection.TypeText('打印时间: ' + DateTimeToStr(now));
  95.   finally
  96.     varclear(wordapp);
  97.   end;
  98. end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值