主题:求助:数据导出到excel问题
ttkl
[专家分:0] 发布于 2007-04-11 10:44:00
使用控件excelapplication,excelworkbook,excelworksheet 来操作数据导出到excel。
在 语句excelapplication1.workbooks.add(emptyparam,0);
出错 UNDECLARED IDENTIFIER:'EMPTYPARAM '
请指教。谢谢
回复列表 (共16个回复)
11 楼
newstudents [专家分:0] 发布于 2007-04-18 11:02:00
filename1:='D:\新建文件夹\产品信息表.xls';//路径
try
excelapplication1.Connect;
except
end;
excelapplication1.Visible[0]:=true;
//excelworkbook1.ConnectTo(excelapplication1.Workbooks.add(emptyparam,0));
excelworkbook1.ConnectTo(excelapplication1.Workbooks.open(filename1,
emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,
emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,emptyparam,
emptyparam,emptyparam,0));//连接打开的空的EXCEL
excelworkbook1.Activate;//激活
//excelworksheet1.ConnectTo(excelworkbook1.Worksheets.add(emptyparam,emptyparam,
//emptyparam,emptyparam,0) as _worksheet);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets.item[1] as _worksheet);
excelworksheet1.Activate;
excelworksheet1.Name:='产品信息表';//表名
excelworksheet1.UsedRange[0].ClearContents;//清空
excelworksheet1.UsedRange[0].Borders.linestyle:=xllinestylenone;//表边框的类型
//excelworksheet1.Cells.Font.Size:=18;//设置字体
excelworksheet1.Cells.Item[1,3]:='产品信息表:';
dbgrid1.DataSource.DataSet.Open;//把dbgrid1中查询到的数据发送到excel中,以便打印;
excelworksheet1.Cells.Item[2,1]:=dbgrid1.Fields[0].FieldName;
excelworksheet1.Cells.Item[2,2]:=dbgrid1.Fields[1].FieldName;
excelworksheet1.Cells.Item[2,3]:=dbgrid1.Fields[2].FieldName;
excelworksheet1.Cells.Item[2,4]:=dbgrid1.Fields[3].FieldName;
excelworksheet1.Cells.Item[2,5]:=dbgrid1.Fields[4].FieldName;
excelworksheet1.Cells.Item[2,6]:=dbgrid1.Fields[5].FieldName;
//excelworksheet1.Cells.Item[2,1]:=table1.Fields[0].FieldName;
//excelworksheet1.Cells.Item[2,2]:=table1.Fields[1].FieldName;
//excelworksheet1.Cells.Item[2,3]:=table1.Fields[2].FieldName;
//excelworksheet1.Cells.Item[2,4]:=table1.Fields[3].FieldName;
//excelworksheet1.Cells.Item[2,5]:=table1.Fields[4].FieldName;
//excelworksheet1.Cells.Item[2,6]:=table1.Fields[5].FieldName;
excelworksheet1.Cells.Item[2,7]:='备注';
//table1.First;
dbgrid1.DataSource.DataSet.First;
i:=3;
while not dbgrid1.DataSource.DataSet.Eof do
begin
excelworksheet1.Cells.Item[i,1]:=dbgrid1.Fields[0].AsString;
excelworksheet1.Cells.Item[i,2]:=dbgrid1.Fields[1].AsString;
excelworksheet1.Cells.Item[i,3]:=dbgrid1.Fields[2].AsString;
excelworksheet1.Cells.Item[i,4]:=dbgrid1.Fields[3].AsString;
excelworksheet1.Cells.Item[i,5]:=dbgrid1.Fields[4].AsString;
excelworksheet1.Cells.Item[i,6]:=dbgrid1.Fields[5].AsString;
//excelworksheet1.Cells.Item[i,1]:=table1.Fields[0].AsString;
//excelworksheet1.Cells.Item[i,2]:=table1.Fields[1].AsString;
//excelworksheet1.Cells.Item[i,3]:=table1.Fields[2].AsString;
//excelworksheet1.Cells.Item[i,4]:=table1.Fields[3].AsString;
//excelworksheet1.Cells.Item[i,5]:=table1.Fields[4].AsString;
//excelworksheet1.Cells.Item[i,6]:=table1.Fields[5].AsString;
dbgrid1.DataSource.DataSet.Next;
inc(i);
end;
dbgrid1.DataSource.DataSet.Close;//绘制表的边框
rangel:=excelworksheet1.Range['a2','g'+inttostr(i-1)];
rangel.borders.linestyle:=xlcontinuous;
rangel.borderaround(xldouble,xlthick);
//try
//excelworkbook1.save;
//except
//end;
12 楼
lubby [专家分:310] 发布于 2007-04-18 15:21:00
当然可以的,如果直接是使用代码,只需要对excel表格的单元格赋值就行,不过excel单元格是列在前,行在后,如:第1行第3列是[3,1],代码可以参考上面的
13 楼
xinxi99206 [专家分:380] 发布于 2007-04-18 23:52:00
procedure TFzongjihua.Button3Click(Sender: TObject);
var i,j,k:integer;
begin
xlapp := CreateOleObject('excel.application');
xlApp.DisplayAlerts := true ;//是否提示存盘
//新建EXCEL文件
xlapp.workbooks.add; // 如果要需要打开本身存在的EXCEL文件,则用xalpp.Workbooks.Open(OpenDialog1.FileName);代替xlapp.Workbooks.Add;就可以了。
xlSheet:= xlapp.workbooks[1].sheets[1];
//先查表中有多少条记录
FDM.ADOQRcordcount.Close;
FDM.ADOQRcordcount.SQL.Clear;
FDM.ADOQRcordcount.SQL.Add('SELECT 课程性质, 课程名称, 主干课, 学分, 学期, 面授学时, 自学学时, 实验学时 FROM 课程计划查询 where 专业ID='+inttostr(zhuanyeID)+'');
FDM.ADOQRcordcount.Open;
//以下为制作表的格式
range:= xlapp.workbooks[1].sheets[1].range['A1:H'+inttostr(FDM.ADOQRcordcount.RecordCount+4)+'']; //单元格从A1到最右下角一个
Range.Borders.LineStyle:=1;//加边框
Range.Columns[1].ColumnWidth:=8;//设置列宽
Range.Columns[2].ColumnWidth:=24;//设置列宽
Range.Columns[3].ColumnWidth:=6;//设置列宽
Range.Columns[4].ColumnWidth:=6;//设置列宽
Range.Columns[5].ColumnWidth:=6;//设置列宽
Range.Columns[6].ColumnWidth:=8;//设置列宽
Range.Columns[7].ColumnWidth:=8;//设置列宽
Range.Columns[8].ColumnWidth:=8;//设置列宽
//制作表头
range:= xlapp.workbooks[1].sheets[1].range['A1:H1']; //单元格从A1到H1
Range.Merge; //合并单元格
xlsheet.cells(1,1):=combobox3.Text+'专业'+combobox2.Text+combobox1.Text+'课表';
Range.HorizontalAlignment:=3;//xlCenter(水平对齐方式)
Range.VerticalAlignment:=2;//xlCenter(垂直对齐方式)
Range.Rows.RowHeight:=30;//设置行高
Range.Borders.LineStyle:=1;//加边框
Range.Characters.Font.Name:='宋体';//字体
Range.Characters.Font.FontStyle:='加粗';
Range.Characters.Font.Size:=15;
//制作报表字段
range:= xlapp.workbooks[1].sheets[1].range['A2:H2']; //单元格从A2到H2
Range.Rows.RowHeight:=20;//设置行高
Range.HorizontalAlignment:=3;//xlCenter(水平对齐方式)
Range.VerticalAlignment:=2;//xlCenter(垂直对齐方式)
//以下为添加必修课
//查询有哪些必修课
FDM.qchaxun1.Close;
FDM.qchaxun1.SQL.Clear;
FDM.qchaxun1.SQL.Add('select 课程性质, 课程名称, 主干课, 学分, 学期, 面授学时, 自学学时, 实验学时 from 课程计划查询 where 课程性质 like'+''''+'%必修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.qchaxun1.open;
range1:= xlapp.workbooks[1].sheets[1].range['A3:A'+inttostr(FDM.qchaxun1.RecordCount+2)+'']; //单元格从A3到必修课结束 合并为一个必修课单元格
range1.merge;
Range1.HorizontalAlignment:=3;//xlCenter(水平对齐方式)
Range1.VerticalAlignment:=2;//xlCenter(垂直对齐方式)
xlsheet.cells(3, 1):='必修课';
xlSheet.Cells(2, 1):=FDM.qchaxun1.Fields[0].FieldName;
for i:=2 to FDM.qchaxun1.FieldCount do
xlSheet.Cells(2, i):=FDM.qchaxun1.Fields[i-1].FieldName; //添加表字段
FDm.qchaxun1.First; //添加表记录
for i:=1 to FDm.qchaxun1.RecordCount do
begin
for j:=2 to FDM.qchaxun1.FieldCount do
begin
if (j=3) and fdm.qchaxun1.FieldByName('主干课').AsBoolean then
xlSheet.Cells(i+2, j):='是'
else if (j=3) and not fdm.qchaxun1.FieldByName('主干课').AsBoolean then
xlSheet.Cells(i+2, j):=' '
else xlSheet.Cells(i+2, j):=FDM.qchaxun1.fieldbyname(trim(FDM.qchaxun1.fields[j-1].fieldname)).asstring;
end;
FDM.qchaxun1.Next;
end;
//查询必修课学分
FDM.xuefen.Close;
FDM.xuefen.SQL.Clear;
FDM.xuefen.SQL.Add('select sum(学分) as 学分 from 课程计划查询 where 课程性质 like'+''''+'%必修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.xuefen.open;
//查询面授总学时
FDM.mianshou.Close;
FDM.mianshou.SQL.Clear;
FDM.mianshou.SQL.Add('select sum(面授学时) as 面授学时 from 课程计划查询 where 课程性质 like'+''''+'%必修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.mianshou.open;
//查询自学总学时
14 楼
xinxi99206 [专家分:380] 发布于 2007-04-18 23:53:00
FDM.zixue.Close;
FDM.zixue.SQL.Clear;
FDM.zixue.SQL.Add('select sum(自学学时) as 自学学时 from 课程计划查询 where 课程性质 like'+''''+'%必修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.zixue.open;
//查询实验总学时
FDM.shiyan.Close;
FDM.shiyan.SQL.Clear;
FDM.shiyan.SQL.Add('select sum(实验学时) as 实验学时 from 课程计划查询 where 课程性质 like'+''''+'%必修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.shiyan.open;
//进行合计显示
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 1):='小 计';
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 2):=inttostr(FDM.qchaxun1.RecordCount)+'门次';
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 4):=FDM.xuefen.fieldbyname('学分').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 6):=FDM.mianshou.fieldbyname('面授学时').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 7):=FDM.zixue.fieldbyname('自学学时').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+3)+'', 8):=FDM.shiyan.fieldbyname('实验学时').Asstring;
k:=FDM.qchaxun1.RecordCount+3;//记录以使用多少行
//以下为添加选修课
//查询有哪些选修课
FDM.qchaxun1.Close;
FDM.qchaxun1.SQL.Clear;
FDM.qchaxun1.SQL.Add('select 课程性质, 课程名称, 主干课, 学分, 学期, 面授学时, 自学学时, 实验学时 from 课程计划查询 where 课程性质 like'+''''+'%选修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.qchaxun1.open;
if FDM.qchaxun1.RecordCount<>0 then
begin
range1:= xlapp.workbooks[1].sheets[1].range['A'+inttostr(k+1)+':A'+inttostr(FDM.qchaxun1.RecordCount+k)+'']; //选修课单元格 合并为一个选修课单元格
range1.merge;
Range1.HorizontalAlignment:=3;//xlCenter(水平对齐方式)
Range1.VerticalAlignment:=2;//xlCenter(垂直对齐方式)
xlsheet.cells(k+1, 1):='选修课';
{xlSheet.Cells(2, 1):=qchaxun1.Fields[0].FieldName;
for i:=2 to qchaxun1.FieldCount do
xlSheet.Cells(2, i):=qchaxun1.Fields[i-1].FieldName; //添加表字段 }
FDM.qchaxun1.First; //添加表记录
for i:=1 to FDM.qchaxun1.RecordCount do
begin
for j:=2 to FDM.qchaxun1.FieldCount do
//for j:=2 to FDM.qchaxun1.FieldCount do
begin
if (j=3) and fdm.qchaxun1.FieldByName('主干课').AsBoolean then
xlSheet.Cells(i+k, j):='是'
else if (j=3) and not fdm.qchaxun1.FieldByName('主干课').AsBoolean then
xlSheet.Cells(i+k, j):=' '
else xlSheet.Cells(i+k, j):=FDM.qchaxun1.fieldbyname(trim(FDM.qchaxun1.fields[j-1].fieldname)).asstring;
end;
//xlSheet.Cells(k+i, j):=FDM.qchaxun1.fieldbyname(trim(FDM.qchaxun1.fields[j-1].fieldname)).asstring;
FDM.qchaxun1.Next;
end;
//查询选修课总学分
FDM.xuefen.Close;
FDM.xuefen.SQL.Clear;
FDM.xuefen.SQL.Add('select sum(学分) as 学分 from 课程计划查询 where 课程性质 like'+''''+'%选修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.xuefen.open;
//查询面授总学时
FDM.mianshou.Close;
FDM.mianshou.SQL.Clear;
FDM.mianshou.SQL.Add('select sum(面授学时) as 面授学时 from 课程计划查询 where 课程性质 like'+''''+'%选修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.mianshou.open;
//查询自学总学时
FDM.zixue.Close;
FDM.zixue.SQL.Clear;
FDM.zixue.SQL.Add('select sum(自学学时) as 自学学时 from 课程计划查询 where 课程性质 like'+''''+'%选修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.zixue.open;
//查询实验总学时
FDM.shiyan.Close;
FDM.shiyan.SQL.Clear;
FDM.shiyan.SQL.Add('select sum(实验学时) as 实验学时 from 课程计划查询 where 课程性质 like'+''''+'%选修%'+''''+' and 专业ID='+INTTOSTR(zhuanyeID)+'');
FDM.shiyan.open;
//进行合计显示
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 1):='小 计';
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 2):=inttostr(FDM.qchaxun1.RecordCount)+'门次';
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 4):=FDM.xuefen.fieldbyname('学分').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 6):=FDM.mianshou.fieldbyname('面授学时').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 7):=FDM.zixue.fieldbyname('自学学时').Asstring;
xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 8):=FDM.shiyan.fieldbyname('实验学时').Asstring;
end
else xlsheet.cells(''+inttostr(FDM.qchaxun1.RecordCount+k+1)+'', 1):='该专业没有选修课';
xlapp.visible := true; //excel 前台显示
//xlapp.quit; //退出excel
15 楼
hanzhijun402 [专家分:3690] 发布于 2007-04-19 15:50:00
procedure TForm1.Button1Click(Sender: TObject);
var
j:integer;
msexcel:variant;
msexcelworkbook:variant;
msexcelworksheet:variant;
begin
msexcel:=createoleobject('Excel.application');
msexcelworkbook:=msexcel.workbooks.add;
msexcelworksheet:=msexcel.workbooks[1].worksheets[1];
msexcelworksheet.Activate;
msexcel.visible:=true;
with adoquery_rs do
begin
close;
sql.clear;
sql.add('select empno as 员工编号,empname as 员工名称,empengname as 英文名称 from rs_emp');
open;
First;
end;
try
msexcelworksheet.range['A1'].value:='员工编号';
msexcelworksheet.range['B1'].value:='员工名称';
msexcelworksheet.range['C1'].value:='英文名称';
j:=1;
while not adoquery_rs.Eof do
begin
msexcelworksheet.range['A'+inttostr(j+1)].value:=adoquery_rs.FieldByName('员工编号').asstring;
msexcelworksheet.range['B'+inttostr(j+1)].value:=adoquery_rs.FieldByName('员工名称').asstring;
msexcelworksheet.range['C'+inttostr(j+1)].value:=adoquery_rs.FieldByName('英文名称').asstring;
j:=j+1;
adoquery_rs.Next;
end;
finally
showmessage('请先关闭已打开的指示书Excel文件!');
msexcelworksheet:=null;
msexcel.workbooks.close;
msexcelworkbook:=null;
msexcel:=null;
end;
end;
16 楼
hanzhijun402 [专家分:3690] 发布于 2007-04-19 15:51:00
对了,是要先uses Excel2000,ComOBJ,ShellAPI;
我来回复