- use ComObj;
- procedure TFm_main.BitBtn1Click(Sender: TObject);
- var SourceFile: string;
- Sourcexlapp, Sourcexlsheet, xlapp, xlsheet: variant;
- RowNum, RowJ, Cols, i: integer;
- begin
- try
- Tbutton(sender).Enabled := false;
- if trim(edit1.Text) = '' then
- begin
- if OpenDialog1.Execute then
- edit1.Text := OpenDialog1.FileName;
- end;
- RowNum := strtoint(trim(MaskEdit1.Text));
- Cols := strtoint(trim(MaskEdit3.Text));
- screen.Cursor := crHourGlass;
- SourceFile := trim(edit1.Text);
- try
- Sourcexlapp := createoleobject('excel.application');
- except
- showmessage('not found excel in your system, so can not create file!');
- exit;
- end;
- Sourcexlapp.Visible := false;
- Sourcexlapp.workbooks.open(SourceFile);
- Sourcexlsheet := Sourcexlapp.ACTIVESHEET;
- try
- xlapp := createoleobject('excel.application');
- except
- showmessage('not found excel in your system, so can not create file!');
- exit;
- end;
- xlapp.workbooks.add; //添加新工作簿
- xlapp.visible := true;
- xlsheet := xlapp.activesheet;
- for i := 1 to Cols do
- begin
- xlsheet.columns[i].columnwidth := Sourcexlsheet.columns[i].columnwidth;
- end;
- RowJ := 1; i := RowNum + 1;
- while i <= 2000 do
- begin
- if length(trim(Sourcexlsheet.cells[i, 1])) = 0 then break;
- //copy title
- Sourcexlapp.Range[Sourcexlsheet.cells[1, 1], Sourcexlsheet.cells[RowNum, Cols]].select;
- Sourcexlapp.selection.copy;
- xlapp.Range[xlsheet.cells[Rowj, 1], xlsheet.cells[Rowj, 1]].select;
- xlsheet.Paste;
- //copy data
- inc(Rowj, RowNum);
- Sourcexlapp.Range[Sourcexlsheet.cells[i, 1], Sourcexlsheet.cells[i, Cols]].select;
- Sourcexlapp.selection.copy;
- xlapp.Range[xlsheet.cells[Rowj, 1], xlsheet.cells[Rowj, 1]].select;
- xlsheet.Paste;
- inc(Rowj, 2);
- inc(i);
- end;
- try
- Sourcexlapp.Quit;
- except
- end;
- VarClear(Sourcexlapp);
- VarClear(Sourcexlsheet);
- VarClear(xlapp);
- VarClear(xlsheet);
- finally
- screen.Cursor := crDefault;
- Tbutton(sender).Enabled := true;
- end;
- end;
- Word文档 use ComObj,WordXP;
- procedure Pub_TChartToWord(F_TChart: TChart);
- var i: integer;
- wordapp: variant;
- begin
- if F_TChart = nil then exit;
- try
- try
- wordapp := createoleobject('word.application');
- except
- showmessage('not found word in your system, so can not create file!');
- exit;
- end;
- wordapp.visible := true;
- wordapp.Documents.Add(DocumentType := wdNewBlankDocument);
- F_TChart.CopyToClipboardBitmap;
- wordapp.Selection.Paste;
- for i := 0 to F_TChart.Series[0].Count - 1 do
- begin
- wordapp.Selection.TypeParagraph;
- wordapp.Selection.TypeText(F_TChart.Series[0].XLabel[i] + ' ' + FloatToStr(F_TChart.Series[0].YValue[i]));
- end;
- wordapp.Selection.TypeParagraph;
- wordapp.Selection.TypeText('打印时间: ' + DateTimeToStr(now));
- finally
- varclear(wordapp);
- end;
- end;