高手请进,200分问题 (200分)

  • 主题发起人 主题发起人 xiaoa2002
  • 开始时间 开始时间
X

xiaoa2002

Unregistered / Unconfirmed
GUEST, unregistred user!
把word表格(6,6),最后一行的后五个单元格合并,需要源码,谢谢
 
xlgrid
可以用这个控件
可以去www.51delphi.com
看看
 
不用控件的,谢谢
 
放个Edit在上面,盖住后五个格不就行了,在OnChange、OnReSize事件中写些控制的代码就行了[:D]
 
不好意思,没说清,是word表格
 
用 WORD 的 宏跟踪,就知道代码怎么写了。
 
我虽然不懂,但对这个问题挺感兴趣的:)
查了点资料,看看下面的方法可不可行
如果那几个格是固定的
你在word里录制一个宏(合并单元格的宏)
然后在delphi中调用这个宏
{下面是查到的一段代码不可能不能用,我没试验}
var AFileName:OleVariant;

begin
AFileName:='d:/test.doc';
//打开外部Word文档
WordApplication1.Documents.Open(AFileName,EmptyParam,EmptyParam,EmptyParam
,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam );
WordApplication1.Visible:=true;//是否显示文件编辑
WordApplication1.Run('test'); //运行test这个宏
end;
 
都不行啊
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Word2000, OleCtnrs, OleServer;

type

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FWordApp: TWordApplication;
FWordDoc: TWordDocument;
FNewDocument: _Document;
FTable: Table;
FFileName: string;
FVisible: Boolean;
FInsPos: Range;
FStarPos: Range;
FWdPaperSize: LongWord;
InsertPosBm: Bookmark;
public
{ Public declarations }
function AddTables(ANumRows, ANumColumns: Integer): Boolean;
function NewDocument(const DocName: string; TempName: string = ''):
Boolean;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.NewDocument(const DocName: string; TempName: string = ''):
Boolean;
//创建Word新文档;
var
_Template, _NewTemplate, _DocumentType, _Visible: OleVariant;
begin
_Template := TempName;
_NewTemplate := False;
_DocumentType := wdNewBlankDocument;
_Visible := True;

FNewDocument := FWordApp.Documents.Add(_Template, _NewTemplate,
_DocumentType, _Visible);
if FNewDocument = nil then
begin
Result := False;
end
else
begin
FWordDoc.Disconnect;
FWordDoc.ConnectTo(FNewDocument);
FNewDocument.Activate;
FFileName := DocName;
//设置字体大小;
if FWdPaperSize <> $00000029 then
begin
FWordDoc.PageSetup.PaperSize := FWdPaperSize;
FWordDoc.PageSetup.LeftMargin := FWordApp.CentimetersToPoints(2);
FWordDoc.PageSetup.RightMargin := FWordApp.CentimetersToPoints(2);
FWordDoc.PageSetup.TopMargin := FWordApp.CentimetersToPoints(2);
FWordDoc.PageSetup.BottomMargin :=FWordApp.CentimetersToPoints(2);
FWordDoc.PageSetup.FooterDistance :=FWordApp.CentimetersToPoints(1.6);
end;
FWordDoc.Range.Font.size := 9;
FWordDoc.Range.Text :='';
//设置文档的行间据;
with FWordDoc.Range.ParagraphFormat do
begin
LineSpacingRule := wdLineSpaceAtLeast;
LineSpacing := 12;
LineUnitBefore := 0;
LineUnitAfter := 0;
end;
Result := True;
end;

end;

function TForm1.AddTables(ANumRows, ANumColumns: Integer): Boolean;
var
_Range: Range;
_DefaultTableBehavior, _AutoFitBehavior: OleVariant;
_Unit, _Count, _Extend: OleVariant;
_KeepWithNext: OleVariant;
begin
Result := True;
//插入表格到Word文档中;
_Range := FWordApp.Selection.Range;
_DefaultTableBehavior := wdWord9TableBehavior;
_AutoFitBehavior := wdAutoFitWindow;
try
FTable := FWordDoc.Tables.Add(_Range, ANumRows, ANumColumns,
_DefaultTableBehavior, _AutoFitBehavior);
//使整个表格保持在同一页当中;
_KeepWithNext := True;
FTable.Range.ParagraphFormat.KeepWithNext := _KeepWithNext;
except
Result := False;
end;

//将光标移动到表格之后;
_Unit := wdLine;
_Count := 5;
_Extend := wdMove;
FWordApp.Selection.MoveDown(_Unit, _Count, _Extend);

_Unit := wdCharacter;
_Count := 1;
_Extend := wdMove;
FWordApp.Selection.MoveRight(_Unit, _Count, _Extend);

_Unit := wdCharacter;
_Count := 5;
_Extend := wdExtend;
FWordApp.Selection.MoveRight(_Unit, _Count, _Extend);

FWordApp.Selection.Cells.Merge;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FWdPaperSize := 41;

FWordApp := TWordApplication.Create(nil);
FWordDoc := TWordDocument.Create(nil);
FWordApp.AutoQuit := True;
FWordApp.ConnectKind := ckNewInstance; //建立新实例

//关闭word的错误检查功能
with FWordApp.Options do
begin
CheckSpellingAsYouType := False;
CheckGrammarAsYouType := False;
SuggestSpellingCorrections := False;
SuggestFromMainDictionaryOnly := False;
CheckGrammarWithSpelling := False;
ShowReadabilityStatistics := False;
IgnoreUppercase := True;
IgnoreMixedDigits := True;
IgnoreInternetAndFileAddresses := True;
AllowCombinedAuxiliaryForms := True;
EnableMisusedWordsDictionary := True;
AllowCompoundNounProcessing := True;
UseGermanSpellingReform := False;
end;
FWordApp.Connect;
FWordApp.Visible := Visible;
NewDocument('D:/test.doc');
AddTables(6,6);

end;

end.
 
RaisingRain老兄,你只要简单告诉我怎样把光标定位在表格(6,2),然后合并最后一行的后五个单元格就行了,我先研究一下你的代码,十分感谢[:)]
 
说明,我的这个表格第一行一列,第二行三列,其他每行六列(举例的,是个活动表格)
 
_Unit := wdLine; //行移
_Count := 5; //位置
_Extend := wdMove; //移动
FWordApp.Selection.MoveDown(_Unit, _Count, _Extend);
上述代码表示把光标移动到第六行,

_Unit := wdCharacter; //字符移动
_Count := 1; //位置
_Extend := wdMove; //移动
FWordApp.Selection.MoveRight(_Unit, _Count, _Extend);
上述代码表示把光标移动到第二个字符处。

_Unit := wdCharacter; //字符移动
_Count := 5; //位置
_Extend := wdExtend; //移动选择对象
FWordApp.Selection.MoveRight(_Unit, _Count, _Extend);
上述代码表示选择第六行第二个字符起选择五个字符。

FWordApp.Selection.Cells.Merge; //合并选择对象



 
老兄,看看我的代码,如何合并最后一行的后9个单元格,辛苦了,谢谢[:)]

unit Unit3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, comobj, word2000, wordxp;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
NewTable,myRange,Word, Doc:OleVariant; //, Fields
i:integer;
Align,ShowFirst:olevariant;
begin
try
Word:=CreateOleObject('Word.Application');
except
application.MessageBox('请安装Microsoft Word 2000 或更高版本!','警告!',mb_ok+mb_iconwarning);
abort;
end;
Word.Visible:=True;
//创建空文档;
Word.Documents.Add();
//将页面改为“横向”;
Word.ActiveDocument.PageSetup.Orientation := wdOrientLandscape;
Word.ActiveDocument.PageSetup.LineNumbering.Active:=False;
Word.ActiveDocument.PageSetup.Orientation := wdOrientLandscape;

Word.ActiveDocument.PageSetup.TopMargin := 2/0.035;
Word.ActiveDocument.PageSetup.BottomMargin := 2/0.035;
Word.ActiveDocument.PageSetup.LeftMargin := 2/0.035;
Word.ActiveDocument.PageSetup.RightMargin := 2/0.035;
Word.ActiveDocument.PageSetup.Gutter := 0;
Word.ActiveDocument.PageSetup.HeaderDistance := 1.2/0.035;
Word.ActiveDocument.PageSetup.FooterDistance := 1.5/0.035;
Word.ActiveDocument.PageSetup.PageWidth := 29.7/0.035;
Word.ActiveDocument.PageSetup.PageHeight := 21/0.035;

Word.ActiveDocument.PageSetup.FirstPageTray := wdPrinterDefaultBin;
Word.ActiveDocument.PageSetup.OtherPagesTray := wdPrinterDefaultBin;
Word.ActiveDocument.PageSetup.SectionStart := wdSectionNewPage;
Word.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter := False;
Word.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter := False;
Word.ActiveDocument.PageSetup.VerticalAlignment := wdAlignVerticalTop;
Word.ActiveDocument.PageSetup.SuppressEndnotes := False;
Word.ActiveDocument.PageSetup.MirrorMargins := False;
Word.ActiveDocument.PageSetup.TwoPagesOnOne := False;
Word.ActiveDocument.PageSetup.GutterPos := wdGutterPosLeft;
Word.ActiveDocument.PageSetup.LayoutMode := wdLayoutModeLineGrid;

Doc := Word.ActiveDocument;
//写入页眉页脚
//写入页眉页脚
If word.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
begin
word.ActiveWindow.Panes(2).Close;
End;
If (word.ActiveWindow.ActivePane.View.Type=wdNormalView)
Or (word.ActiveWindow.ActivePane.View.Type=wdOutlineView) Then
begin
word.ActiveWindow.ActivePane.View.Type:=wdPrintView;
End;

word.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
Word.Selection.Font.NameFarEast:='宋体';
Word.Selection.Font.NameAscii:='Times New Roman';
Word.Selection.Font.NameOther:='Times New Roman';
Word.Selection.Font.Name:='Times New Roman';
Word.Selection.Font.Size:=9;
Word.Selection.Font.Bold:=false;
Word.Selection.Font.Italic:=wdToggle;
Word.Selection.TypeText(Text:='<< 强脑工程业务训练系统 >>');
word.Application.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;

Align:=wdAlignPageNumberCenter;
ShowFirst:=True;
Word.ActiveDocument.Sections.Item(1).Footers.Item(1).PageNumbers.Add(Align,ShowFirst);

Word.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;

//写入标题;
Word.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
Word.Selection.Font.NameFarEast:='宋体';
Word.Selection.Font.NameAscii:='Times New Roman';
Word.Selection.Font.NameOther:='Times New Roman';
Word.Selection.Font.Name:='Times New Roman';
Word.Selection.Font.Size:=22;
Word.Selection.Font.Bold := true;
Word.Selection.TypeText(Text:='训练计划');

Word.Selection.TypeParagraph;
Word.Selection.ParagraphFormat.Alignment:= wdAlignParagraphJustify;
Word.Selection.Font.NameFarEast:='宋体';
Word.Selection.Font.NameAscii:='Times New Roman';
Word.Selection.Font.NameOther:='Times New Roman';
Word.Selection.Font.Name:='Times New Roman';
Word.Selection.Font.Size:=9;
Word.Selection.Font.Bold := false;
//Word.Selection.Font.Bold:=wdToggle;
Word.Selection.TypeParagraph;
//画出空表格;
Word.ActiveDocument.Tables.Add(Range:=Word.Selection.Range,
NumRows:=10, //ClientDataSet_tmp.RecordCount+1,
NumColumns:=10,
DefaultTableBehavior:=0,
AutoFitBehavior:=0);
//合并表格
word.Selection.MoveDown(wdLine,1,wdExtend);
word.Selection.Cells.Merge;

word.Selection.MoveRight(1,1,wdMove);
word.Selection.MoveRight(1,5,wdExtend);
word.Selection.Cells.Merge;

word.Selection.MoveRight(2,1,wdMove);
word.Selection.MoveDown(wdLine,1,wdExtend);
word.Selection.Cells.Merge;
word.Selection.MoveRight(3,1,wdMove);
word.Selection.MoveDown(wdLine,1,wdExtend);
word.Selection.Cells.Merge;
word.Selection.MoveRight(3,1,wdMove);
word.Selection.MoveDown(wdLine,1,wdExtend);
word.Selection.Cells.Merge;
word.Selection.MoveRight(3,1,wdMove);
word.Selection.MoveDown(wdLine,1,wdExtend);
word.Selection.Cells.Merge;
end;

end.
 
Word.Selection.MoveDown(wdLine,8,wdMove);
Word.Selection.MoveLeft(wdCharacter,8,wdExtend);
Word.Selection.Cells.Merge;
在最后加三行

 
谢谢老兄和其他兄弟们
 
后退
顶部