delphi处理word,excel表格的问题。

Richie2000 2004-01-09 10:38:53
最近遇到一个问题,如何处理一个表格
这个表格是word或是excel格式的文档
在每一个表格中,都存在一个字段,就是顺序的序号,如果从中间删掉一条记录(就是说,删掉表格中的一行),不知道大家有什么好的想法,还请赐教。如果能够提供源代码参考,愿意加倍给分。
...全文
99 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ly_liuyang 2004-01-12
  • 打赏
  • 举报
回复
呵呵
Word处理表格需要DOT模板的支持的

自己慢慢研究了
  • 打赏
  • 举报
回复
:)
luminrong 2004-01-11
  • 打赏
  • 举报
回复
up
Hedonism 2004-01-11
  • 打赏
  • 举报
回复
关注
ly_liuyang 2004-01-10
  • 打赏
  • 举报
回复
TWordApplication组件
TExeclApplication组件

//Sending Data to Excel
procedure TForm1.BitBtnToExcelClick(Sender: TObject);
var
WorkBk : _WorkBook; // Define a WorkBook
WorkSheet : _WorkSheet; // Define a WorkSheet
I, J, R, C : Integer;
IIndex : OleVariant;
TabGrid : Variant;
begin
if GenericStringGrid.Cells[0,0] <> '' then
begin
IIndex := 1;
R := GenericStringGrid.RowCount;
C := GenericStringGrid.ColCount;
// Create the Variant Array
TabGrid := VarArrayCreate([0,(R - 1),0,(C - 1)],VarOleStr);
I := 0;
// Define the loop for filling in the Variant
repeat
for J := 0 to (C - 1) do
TabGrid[I,J] := GenericStringGrid.Cells[J,I];
Inc(I,1);
until
I > (R - 1);
// Connect to the server TExcelApplication
ExcelApp.Connect;
// Add WorkBooks to the ExcelApplication
ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0);
// Select the first WorkBook
WorkBk := ExcelApp.WorkBooks.Item[IIndex];
// Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// Assign the Delphi Variant Matrix to the Variant associated with the WorkSheet
Worksheet.Range['A1',Worksheet.Cells.Item[R,C]].Value[EmptyParam] := TabGrid;
// Customise the WorkSheet
WorkSheet.Name := 'Customers';
Worksheet.Columns.Font.Bold := True;
Worksheet.Columns.HorizontalAlignment := xlRight;
WorkSheet.Columns.ColumnWidth := 14;
// Customise the first entire Column
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].Font.Color := clBlue;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].HorizontalAlignment := xlHAlignLeft;
WorkSheet.Range['A' + IntToStr(1),'A' + IntToStr(R)].ColumnWidth := 31;
// Show Excel
ExcelApp.Visible[0] := True;
// Disconnect the Server
ExcelApp.Disconnect;
// Unassign the Delphi Variant Matrix
TabGrid := Unassigned;
end;
end;

//Retrieving Data from Excel
procedure TForm1.BitBtnFromExcelClick(Sender: TObject);
var
WorkBk : _WorkBook;
WorkSheet : _WorkSheet;
K, R, X, Y : Integer;
IIndex : OleVariant;
RangeMatrix : Variant;
XLSFile : WideString;
begin
with TOpenDialog.Create(Self) do
begin
InitialDir:=ExtractFilePath(ParamStr(0));
Filter:='Excel document (*.xls)|*.xls';
Options:=[ofHideReadOnly, ofPathMustExist, ofFileMustExist];
if Execute then XLSFile:=FileName;
Free;
end;
if XLSFile='' then Exit;
IIndex := 1;
ExcelApp.Connect;
// Open the Excel File
ExcelApp.WorkBooks.Open(XLSFile,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
WorkBk := ExcelApp.WorkBooks.Item[IIndex];
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;
// In order to know the dimension of the WorkSheet, i.e the number of rows and the
// number of columns, we activate the last non-empty cell of it
WorkSheet.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
// Get the value of the last row
X := ExcelApp.ActiveCell.Row;
// Get the value of the last column
Y := ExcelApp.ActiveCell.Column;
// Define the number of the columns in the TStringGrid
GenericStringGrid.ColCount := Y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant Matrix
RangeMatrix := ExcelApp.Range['A1',ExcelApp.Cells.Item[X,Y]].Value[EmptyParam];
// Quit Excel and Disconnect the Server
ExcelApp.Quit;
ExcelApp.Disconnect;
// Define the loop for filling in the TStringGrid
K := 1;
repeat
for R := 1 to Y do
GenericStringGrid.Cells[(R - 1),(K - 1)] := RangeMatrix[K,R];
Inc(K,1);
GenericStringGrid.RowCount := K + 1;
until
K > X;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
end;

Word的
procedure TForm1.Button1Click(Sender: TObject);
var
templateName: OleVariant;
newTemplate: OleVariant;
ItemIndex: OleVariant;
vSelection: WordSelection;
vBookMark: BookMark;
vTable: Table;
I : Integer;
begin
// get dot file name
templateName := ExtractFilePath(Application.ExeName) + 'WordApp.dot';

newTemplate := False;

try
WordApp.Connect;
except
MessageDlg('Please Microsoft Office Word!', mtError, [mbOK], 0);
Abort;
end;

// create word document by specified dot
WordApp.Documents.AddOld(templateName, newTemplate);
WordApp.Caption := 'Word Application Demo Document';

vSelection := WordApp.Selection;

// get the first table
vTable := WordApp.ActiveDocument.Tables.Item(1);

vTable.Cell(1, 2).Range.Text := 'Liu Yang';
vTable.Cell(1, 4).Range.Text := 'LYSoft';
vTable.Cell(2, 2).Range.Text := 'http://lysoft.7u7.net';
vTable.Cell(3, 2).Range.Text := DateToStr(Now);
vTable.Cell(3, 4).Range.Text := TimeToStr(Now);

vTable.Cell(5, 1).Range.Text := 'LYSoft powered by Liu Yang';
vTable.Cell(7, 1).Range.Text := 'LY';
vTable.Cell(9, 2).Range.Text := 'Demo project';

for I := 1 to WordApp.ActiveDocument.Bookmarks.Count do
begin
ItemIndex := I;
// get the Index of specified bookmark
vBookMark := WordApp.ActiveDocument.Bookmarks.Item(ItemIndex);
//ShowMessage(vBookMark.Name+IntToStr(wordApp.ActiveDocument.Bookmarks.Count)+'Now='+IntToStr(i));
if CompareText(vBookMark.Name, 'bm_ID') = 0 then
begin
vBookMark.Select;
vSelection.InsertAfter(IntToStr(GetTickCount));
end;
end;

with WordApp do
begin
Visible := CheckBox1.Checked;
UserName := 'LY';
// minimize
WindowState := 2;
// maxmize
WindowState := 1;
// print preview
PrintPreview := CheckBox2.Checked;

// print now
if CheckBox3.Checked then PrintOutOld;

Disconnect;
end;
end;
caodayong 2004-01-10
  • 打赏
  • 举报
回复
你查一下ole内嵌的资料,以前用vba控制office系列的实在是很多,word和excel的控制方式也是大同小异的!
祝你成功!
Richie2000 2004-01-09
  • 打赏
  • 举报
回复
大家帮帮忙好吗??
补丁下载(1.15M): http://u.115.com/file/f8f8e7d9fa# Ver2.45.407 重大功能完善: 1.当计算公式出错导致生成报表失败,退出调用AC Report的程序时发生内存错误。 2.含有子报表且子报表的列数大于主报表,不能导出到Excel问题。 3.和Skin皮肤控件冲突的问题(主要是导出Excel后报错)。 4.进一步完善导出Excel的算法,将多个页面格式迥异的报表导出在一个Sheet上时,改进了算法,使得在Excel中所需列的数量和宽度计算精确度更高,真正做到无失真导出,容错性更好,对于任何复杂的报表,都可以正确导出 Ver2.45主要变动 1.修正了独立子报表(未嵌入到所属页面的子报表)预览后退出程序时,发生内存泄漏的bug。 2.图片单元格支持在公式中设置URL链接地址。 3.Cell()函数空值处理。 4.界面修改。 5.修正VCL控件和demo编译出错的问题 ACReport简介 Anycell Report(简称AC Report)是一款中国式报表组件,是国内最早的基于表格,支持图文混排、公式和脚本的中国式报表工具之一。就如Anycell Report的名称那样,灵活强大的表格功能一直是AC Report区别于其它软件或控件最显著的特征之一,AC Report 表格取消了传统表格概念中“列”的概念,每一行上的单元格数量可以不等,且可以自由活动,勿须上下对齐,在制作复杂的中国式报表时可以避免很多不必要的合并拆分操作,制作表格更加方便和随心所欲,并且省时省力。AC Report单元格支持多种丰富的形态,例如格式化文本、图片、图表、条码、OLE容器等。 AC Report的一些基本特点: 1.独具特色的表格,风格与Word表格相似,但可以做出比WordExcel更灵活的表格来。 2. 功能全面、专业的中国式报表设计器,中国用户更易于学习和接受。 3.支持多种单元格样式,可以打印图像、图表(直方图、折线图等)、Rich文本、 条形码、中式财务帐薄、支持在报表中嵌入WordExcel文档等。 4. 强大的计算和合计功能。内置表达式解析系统和函数库。 5. 可扩充性,可以在应用程序中给报表引擎扩充函数库、报表样式和单元格样式。 6.支持多种报表样式,如清单式、分组、交叉表、以及子报表等。 7. 支持多栏式报表。 8. 和应用程序完美结合,支持windows下所有的开发工具和程序语言(例如Delphi、C++、VB、PB、.NET、易语言),最终用户在设计器里可直接选择打印字段,生成表达式,报表设计器用户容易学习理解。 9. 支持脚本和窗体编程,报表设计人员可以编写脚本、在报表设计器里为报表添加窗体,为最终用户提供更丰富的交互功能。 10. 既可以使用应用程序中的数据集,也支持在报表中直接连接各种数据库,通过SQL直接获得报表需要的数据。 11.无失真导出为ExcelWord、Html格式的文档

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧