乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>delphi</em>7<em>导出excel</em>的问题-

<em>delphi</em>7<em>导出excel</em>的问题-

作者:乔山办公网日期:

返回目录:excel表格制作


我觉得你这种用list方式导Excel,造成你导出的问题,因为里面有换行符号。我一般都用以下代码转Excel,你可以试试!

procedure TForm_main.btn_excelClick(Sender: TObject);
var
XL: Variant; //打开EXCEL文件的Variant变量
Sheet: Variant;//指向EXCEL表单的Variant变量
Range: Variant;
RecNo,I: Integer;//记录数据表的当前记录号
begin
if dlgSave1.Execute then
begin
XL := CreateOleObject('Excel.Application');
XL.Visible := true;
XL.WorkBooks.Add;
XL.WorkBooks[XL.WorkBooks.Count].WorkSheets[1].Name := 'test';
Sheet := XL.WorkBooks[XL.WorkBooks.Count].WorkSheets[Trim('test')];
RecNo := 1;

//填充表标题
for I:=0 to DBGridEh1.Columns.Count-1 do //初始化行e68a84e8a2ade799bee5baa6e997aee7ad94334,定位在第一 行
if DBGridEh1.Columns[I].Visible then
begin
Sheet.Cells.item[1, I+1] := DBGridEh1.Columns[I].Title.Caption; //.DisplayLabel;
Sheet.Cells[1, I+1].Borders.LineStyle:=1//加边框
end;
Sheet.rows[3].font.size:=12;

//填充数据
cds_bom.First;
while not cds_bom.Eof do
begin
for I := 0 to DBGridEh1.Columns.Count - 1 do
begin
if DBGridEh1.Columns[I].Visible then
begin
Sheet.Cells.Item[RecNo+1,I+1]:=
cds_bom.fieldbyname(DBGridEh1.Columns[I].FieldName).AsString; //[行,列]
Sheet.Cells[RecNo+1,I+1].Borders.LineStyle:=1//加边框
end;
end;

//各类标记 行着色
if cds_bom.fieldbyname('general_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_3.Font.Color;
end;

if cds_bom.fieldbyname('standard_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_4.Font.Color;
end;

if cds_bom.fieldbyname('cable_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_6.Font.Color;
end;

if cds_bom.fieldbyname('chemical_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_7.Font.Color;
end;

if cds_bom.fieldbyname('user1_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_8.Font.Color;
end;

if cds_bom.fieldbyname('user2_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_9.Font.Color;
end;

if cds_bom.fieldbyname('user3_tag').AsString='T' then
begin
Range:=XL.Range[sheet.cells[RecNo+1,1],sheet.cells[RecNo+1,DBGridEh1.Columns.Count]];
Range.Interior.Color:=checkbox_10.Font.Color;
end;

Sheet.rows[RecNo].font.size:=12;
Inc(RecNo);
cds_bom.Next;
end;

//底部颜色注释
Sheet.Cells[RecNo+3,1].Interior.Color:=checkbox_3.Font.Color;
Sheet.Cells.item[RecNo+3, 2] :=checkbox_3.Caption;
Sheet.Cells[RecNo+4,1].Interior.Color:=checkbox_4.Font.Color;
Sheet.Cells.item[RecNo+4, 2] :=checkbox_4.Caption;
Sheet.Cells[RecNo+5,1].Interior.Color:=checkbox_6.Font.Color;
Sheet.Cells.item[RecNo+5, 2] :=checkbox_6.Caption;
Sheet.Cells[RecNo+6,1].Interior.Color:=checkbox_7.Font.Color;
Sheet.Cells.item[RecNo+6, 2] :=checkbox_7.Caption;
Sheet.Cells[RecNo+7,1].Interior.Color:=checkbox_8.Font.Color;
Sheet.Cells.item[RecNo+7, 2] :=checkbox_8.Caption;
Sheet.Cells[RecNo+8,1].Interior.Color:=checkbox_9.Font.Color;
Sheet.Cells.item[RecNo+8, 2] :=checkbox_9.Caption;
Sheet.Cells[RecNo+9,1].Interior.Color:=checkbox_10.Font.Color;
Sheet.Cells.item[RecNo+9, 2] :=checkbox_10.Caption;

try
XL.WorkBooks[XL.WorkBooks.Count].SaveAs(dlgSave1.FileName);
except ;
end;
end;
end;

你的问题,不是内容长短的问题。
从截图的序号“104”的具体任务有很多条,每条的后面带着回车换行符,所以造成了换行。

修改代码:
m:string;
for y := 0 to ADOQuery.FieldCount - 1 do
begin
m:=StringReplace(ADOQuery.Fields[y].AsString,chr(13)+chr(10),'',[rfReplaceAll]);
s := s + m + #9;
Application.ProcessMessages;
end;
我测试了一下,可以了。不知道适用不适用你的表格。你测试一下。
祝你好运!
Project”——“Import Tpye Library”,在弹出的对话框中点击“Add”。找到你安装Excel的盘,,默认的安装路径:“C:\Program Files\Microsoft Office\OFFICE11”.安装的版本如e79fa5e98193e58685e5aeb9331果不同的话“OFFICE11”文件名会不一样。然后你可以找到“XL5CHS32.OLB”和“XL5EN32.OLB”文件。这两个文件你可以任选一个。XL5CHS32.OLB是中文版Excel,XL5EN32.OLB是英文版的Excel。选中后回到了Import Tpye Library对话框,在此对话框中选择“create unit”.这个新创建的单元会放在你当前文件存放的同一个目录下。记住不能删。
2、在你放按钮的窗体中添加一个ExcelApplication1控件,它放在Servers栏中。
3、继续上一步,在窗体的代码implementation部分添加如下代码:
uses dm,tools,Excel_TLB,comobj;
{$R *.dfm}

Function DetailToExcel(xl_file:string;grid:Tdbgrid;T1:string='';T2:string='';T3:string=''):integer;
var xlApp:Variant;
i,j:integer;
irow,icol:integer;
TitleLines:integer;
Titles:array [1..3] of string;
begin
if (not grid.DataSource.DataSet.Active) or (grid.DataSource.DataSet.RecordCount=0) then exit;

Try
xlapp:=createoleobject('Excel.application');
xlapp.workbooks.Add(-4167);
xlapp.visible:=true;
Except
showmessage('未安装Microsoft Excel 2000!请安装!');
exit
end;

//计算标题:
TitleLines:=0;
Titles[1]:=T1;
Titles[2]:=T2;
Titles[3]:=T3;

if T1<>'' then TitleLines:=1;
if T2<>'' then TitleLines:=2;
if T3<>'' then TitleLines:=3;

//填充列标题;
irow:=grid.DataSource.DataSet.RecordCount;
icol:=0;
for i:=0 to grid.Columns.Count-1 do
begin
if grid.Columns[i].Visible then
begin
inc(icol);
xlapp.cells[TitleLines+1,Icol]:=grid.Columns[i].Title.Caption;
end;
end;

//填充标题;
For i:=1 to TitleLines do
begin
xlapp.activesheet.cells[i,1]:=Titles[i];
xlapp.activesheet.range['A'+IntToStr(i)+':'+chr(64+icol)+IntToStr(i)].select;
xlapp.selection.HorizontalAlignment := xlCenter;
xlapp.selection.VerticalAlignment := xlCenter;
xlapp.selection.MergeCells := True;

if i=1 then
begin
xlapp.selection.RowHeight:=26;
xlapp.selection.Font.size:=18;
end
else
begin
xlapp.selection.RowHeight:=20;
xlapp.selection.Font.size:=12;
end;
end;

//设置表格线;
xlapp.activesheet.range['A'+IntTostr(TitleLines+1)+':'+chr(64+icol)+Inttostr(irow+TitleLines+1)].select;
xlapp.selection.Borders[xlEdgeBottom].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgetop].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeright].linestyle:=xlContinuous;
xlapp.selection.Borders[xlEdgeLeft].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideVertical].linestyle:=xlContinuous;
xlapp.selection.Borders[xlInsideHorizontal].linestyle:=xlContinuous;

//填充内容;
grid.DataSource.DataSet.First;
i:=TitleLines+2;
while not grid.DataSource.DataSet.Eof do
begin
icol:=0;
for j:=1 to grid.Columns.Count do
begin
if grid.Columns[j-1].Visible then
begin
inc(icol);
xlapp.cells[i,icol]:=grid.Columns[j-1].Field.DisplayText;
end;
end;
grid.DataSource.DataSet.Next;
inc(i);
end;

For i:=1 to icol do
begin
xlapp.activesheet.Columns[i].select;
xlapp.activesheet.Columns[i].EntireColumn.AutoFit;
end;

xlapp.activesheet.cells[1,1].select;

if xl_File<>'' then
xlapp.workbooks[1].SaveAs(xl_file);

result:=0;
end;
4、最后,按钮单击事件如下代码:
procedure TDayDutyForm.Button3Click(Sender: TObject);
begin
if DBGrid1.DataSource.DataSet.RecordCount=0 then
ShowMessage('没有内容!')
else
DetailToExcel(ExtractFilePath(application.ExeName)+'DayDuty.xls',DBgrid1,'日统计');
end;
/*DayDuty.xls是你需要的Excel的文件名,日统计是你需要的Excel的标题*/

以上内容是老师教的,很好用。希望你能用得成功哈。

有2种方法实现,一种是利用VBA修改工作名称

二种是,直接入外外部数据为 现在工作表 或 新建工作表 即可

相关阅读

  • <em>delphi</em>7<em>导出excel</em>的问题-

  • 乔山办公网excel表格制作
  • 我觉得你这种用list方式导Excel,造成你导出的问题,因为里面有换行符号。我一般都用以下代码转Excel,你可以试试! procedure TForm_main.btn_excelClick(Sender: TObject);var XL: Variant; //打开EXCEL文
  • <em>delphi</em> 如何写入<em>Excel</em>-d

  • 乔山办公网excel表格制作
  • Delphi中对Excel的操作:ExcelApplicat..." src="/uploads/tu/63.jpg" style="width: 400px; height: 267px;" />在开发数据库应用程序中,经常要将类型相同的数据导出来,放到Excel文件中,利用Excel强大的编辑功
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网