Delphi 和 Excel 关联?(30分)

  • 主题发起人 主题发起人 apple058
  • 开始时间 开始时间
A

apple058

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
我希望把 Delphi 中的数据 导入到 Excel 中去。导进去后没有表格形式,
我就问别人怎样添加,别人就告诉我用“ 宏 ”,
我就拷贝了下面一段 宏 代码是用来添加表格用的。
Range("A1:F17").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End Sub
我捉摸了半天也没把它翻译到 Delphi 中去,
我只翻译了第一句:ExcelApplication1.Range['A1','F17'].Select;
请问各位高手,以后的代码怎样改写到 Delphi中去能执行,并且生成表格呢?
谢谢!!!
 
用SERVER里面的EXCEL控件就可以了!
 
TO 游向明;
我是用的server的控件 已经能导出数据,但是就是没有网格,所以要通过加入“宏”来处理!
 
在Delphi 可以直接调用Excel 中的宏,别自己写了。
 
TO yuqiong:
请问怎样用 Delphi 调用 “宏”,譬如上面一段!
 
希望学习!
 
下面是一个delphi 操纵Excel的例子,可以参考一下
procedure TfrmStratumInfo.BitBtn4Click(Sender: TObject);
var
eclApp,WorkBook,ColumnRange:Variant;//声明为OLE Automation 对象
xlsFileName:string;
i: integer;
begin
Screen.Cursor := crHorse;
if SaveDialog1.Execute then
xlsFileName := SaveDialog1.FileName
else
begin
Screen.Cursor := crDefault;
exit;
end;
try
//创建OLE对象Excel Application与 WorkBook
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;

try
workBook:=eclApp.workBooks.Add;
eclApp.WorkSheets[1].Activate;
//eclApp.ActiveSheet.Columns[1].ColumnsWidth := 10;
//eclApp.ActiveSheet.Columns[2].ColumnsWidth := 100;
//eclApp.ActiveSheet.Columns[2].Align := alLeft;
for i := 0 to dxDBInspector1.TotalRowCount-1do
begin
eclApp.Cells[i+1 , 1] := dxDBInspector1.Rows.Caption;
eclApp.Cells[i+1, 2] := dxDBInspector1.Rows.EditText;
end;

WorkBook.saveas(xlsFileName);
WorkBook.close;
finally
eclApp.Quit;
//释放VARIANT变量
eclApp:=Unassigned;
Screen.Cursor := crDefault;
end;
end;
 

Similar threads

I
回复
0
查看
544
import
I
I
回复
0
查看
697
import
I
I
回复
0
查看
610
import
I
I
回复
0
查看
490
import
I
I
回复
0
查看
746
import
I
后退
顶部