首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

小弟,请问怎么把 DBGrid 中的数据倒入到EXCEL 中

2012-05-16 
小弟,请教如何把 DBGrid 中的数据倒入到EXCEL 中GGJJ:刚学习 Delphi7.0,怎么把把 DBGrid 中的数据倒入到EX

小弟,请教如何把 DBGrid 中的数据倒入到EXCEL 中

  GGJJ:
   
  刚学习 Delphi 7.0,怎么把把 DBGrid 中的数据倒入到EXCEL 中?

  请指教。

  用到哪些控件。


  谢谢。

  本人 QQ:125974110 注明:delphi

[解决办法]
7、控制EXCEL
全面控制 Excel 
首先创建 Excel 对象,使用ComObj:
var ExcelID: Variant;
ExcelID := CreateOleObject( 'Excel.Application' );
1) 显示当前窗口:
ExcelID.Visible := True;
2) 更改 Excel 标题栏:
ExcelID.Caption := '应用程序调用 Microsoft Excel';
3) 添加新工作簿:
ExcelID.WorkBooks.Add;
4) 打开已存在的工作簿:
ExcelID.WorkBooks.Open( 'C:\Excel\Demo.xls' );
5) 设置第2个工作表为活动工作表:
ExcelID.WorkSheets[2].Activate;
或 
ExcelID.WorksSheets[ 'Sheet2' ].Activate;
6) 给单元格赋值:
ExcelID.Cells[1,4].Value := '第一行第四列';
7) 设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelID.ActiveSheet.Columns[1].ColumnsWidth := 5;
8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
ExcelID.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米
9) 在第8行之前插入分页符:
ExcelID.WorkSheets[1].Rows.PageBreak := 1;
10) 在第8列之前删除分页符:
ExcelID.ActiveSheet.Columns[4].PageBreak := 0;
11) 指定边框线宽度:
ExcelID.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;
1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
12) 清除第一行第四列单元格公式:
ExcelID.ActiveSheet.Cells[1,4].ClearContents;
13) 设置第一行字体属性:
ExcelID.ActiveSheet.Rows[1].Font.Name := '隶书';
ExcelID.ActiveSheet.Rows[1].Font.Color := clBlue;
ExcelID.ActiveSheet.Rows[1].Font.Bold := True;
ExcelID.ActiveSheet.Rows[1].Font.UnderLine := True;
14) 进行页面设置:
a.页眉:
ExcelID.ActiveSheet.PageSetup.CenterHeader := '报表演示';
b.页脚:
ExcelID.ActiveSheet.PageSetup.CenterFooter := '第&P页';
c.页眉到顶端边距2cm:
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
d.页脚到底端边距3cm:
ExcelID.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;
e.顶边距2cm:
ExcelID.ActiveSheet.PageSetup.TopMargin := 2/0.035;
f.底边距2cm:
ExcelID.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
g.左边距2cm:
ExcelID.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
h.右边距2cm:
ExcelID.ActiveSheet.PageSetup.RightMargin := 2/0.035;
i.页面水平居中:
ExcelID.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
j.页面垂直居中:
ExcelID.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
k.打印单元格网线:
ExcelID.ActiveSheet.PageSetup.PrintGridLines := True;
15) 拷贝操作:
a.拷贝整个工作表:
ExcelID.ActiveSheet.Used.Range.Copy;
b.拷贝指定区域:
ExcelID.ActiveSheet.Range[ 'A1:E2' ].Copy;
c.从A1位置开始粘贴:
ExcelID.ActiveSheet.Range.[ 'A1' ].PasteSpecial;
d.从文件尾部开始粘贴:
ExcelID.ActiveSheet.Range.PasteSpecial;
16) 插入一行或一列:
a. ExcelID.ActiveSheet.Rows[2].Insert;
b. ExcelID.ActiveSheet.Columns[1].Insert;
17) 删除一行或一列:
a. ExcelID.ActiveSheet.Rows[2].Delete;
b. ExcelID.ActiveSheet.Columns[1].Delete;
18) 打印预览工作表:
ExcelID.ActiveSheet.PrintPreview;
19) 打印输出工作表:
ExcelID.ActiveSheet.PrintOut;
20) 工作表保存:
if not ExcelID.ActiveWorkBook.Saved then
ExcelID.ActiveSheet.PrintPreview;
21) 工作表另存为:
ExcelID.SaveAs( 'C:\Excel\Demo1.xls' );
22) 放弃存盘:
ExcelID.ActiveWorkBook.Saved := True;
23) 关闭工作簿:
ExcelID.WorkBooks.Close;
24) 退出 Excel:
ExcelID.Quit;

[解决办法]
利用Excel实现数据录入及打印预览利用Excel实现数据录入及打印预览(原创) - 浏览592次

  Excel强大的数据录入功能极打印预览功能是其他大多数程序望尘莫及的,能不能在我们自己编的程序中调用Excel的数据录入功能及预览功能呢。实际上,使用Delphi你能完全控制Excel或Word的所有功能。虽然来自Excel对象层次内部非常复杂,但我们通过使用Variant(在OLE自动化中使用的Delphi的安全数组版本)通常可以简洁,容易的实现对Excel的控制。
  下面,我将我实际运用的一些方法介绍给大家:

1.启动Excel

  在Uses中加入Comobj;

var
 v:Variant;
begin
 v:=CreateOleobject(‘Excel.Application’);


 v.Visible:=true;
end;

  其中,Comobj中包含了用来检索OLE的对象和向其传发命令的例程,第一行代码启动Excel,执行后Excel会在内存中出现,因此第二行代码使之可见。

2.关闭Excel

  在创建了Excel Application对象后,调用它的Quit方法完成关闭事件。

If not varIsEmpty(v) then v.quit;

  如果需要在关闭前确定是否存盘,加入:

v.DiaplayAlert:=true; //确定存盘
v.DiaplayAlert:=false;//不存盘,直接退出

3.加入工作表及对其表格内容的操作

var
 Sheet: Variant;
begin
 v.workbook.add;
 v.Workbooks.Add;
 v.Workbooks[1].WorkSheets[1].Name := '数据录入';
 Sheet:= v.Workbooks[1].WorkSheets['数据录入'];
 Sheet.Cells[1,1] :='试验'; Label1.Caption:=Sheet.Cells[1,1];
End;

  Sheet对象是很多页的集合,其他的Workbooks是工作薄的集合,WorkSheets对象是工作表的集合,Charts对象是图表的集合。

4.格式的设置

var
 Range: Variant;
begin
 Range := v.Workbooks[1].WorkSheets['数据录入'].Range['A2:M2’]; //单元格从A2到M2 
Range.Merge; //合并单元格
 Range.Rows.RowHeight := 20; //设置行高
 Range.Borders.LineStyle := 1; //加边框
 Range.Columns[2].ColumnWidth := 12; // 设置列宽
 Range.FormulaR1C1 := '标题';
 Range.HorizontalAlignment := xlCenter; //水平对齐方式 
 Range.VerticalAlignment := xlCenter; //垂直对齐方式
 Range.Characters.Font.Name := '宋体'; //字体
 Range.Characters.Font.FontStyle := '加粗';
 Range.Characters.Font.Size := 12;
 Range.Characters.Font.OutlineFont := False; //是否有下划线
 Range.Characters.Font.ColorIndex := xlAutomatic; //颜色
end;

  其他的属性可以察看MSDN或者其他资料。

5. 打印的设置

var
 Sheet: Variant;
begin
 Sheet := XLApp1.Workbooks[1].WorkSheets['数据录入'];
 Sheet.PageSetup.PrintTitleRows :='$1:$3'; //页眉
 Sheet.PageSetup.PrintTitleColumns := '';
 Sheet.PageSetup.LeftFooter := ' 注:页脚'+' 总共&N页'+'--第&P页'; //页脚
 Sheet.PageSetup.LeftMargin := 30; //设置边距
 Sheet.PageSetup.RightMargin := 30;
 Sheet.PageSetup.TopMargin := 30;
 Sheet.PageSetup.BottomMargin := 50;
 Sheet.PageSetup.PrintQuality := 400; //分辨率
 Sheet.PageSetup.CenterHorizontally := True;//是否水平居中
 Sheet.PageSetup.CenterVertically := True; //是否垂直居中
 Sheet.PageSetup.Orientation := 2; //横向打印
 Sheet.PageSetup.Draft := False; //非草稿模式
 Sheet.PageSetup.FirstPageNumber := xlAutomatic;
 Sheet.PageSetup.BlackAndWhite := True; //黑白稿
 Sheet.PageSetup.Zoom := 100; //缩放
 sheet.PrintPreView; //打印预览
end;

  好了,到这,我已经介绍了大部分常用的控制方法,试试吧。提醒你,你的机子装了Excel没有…… 
  (演示代码及程序下载 - 183k)

  作者:万重(转载请保持完整)
  2000.6.9

孜孜原创 【你的看法】 【关闭窗口】 


热点排行