将数据库中的数据按行一格格读到EXCEL 中但有些格内的数据显示不对,150分求解两问题(150分)

  • 主题发起人 主题发起人 zcxrdz
  • 开始时间 开始时间
Z

zcxrdz

Unregistered / Unconfirmed
GUEST, unregistred user!
我有两个问题:
(1)将数据库中的数据按行一格格读到EXCEL 中但有些格内的数据显示不对
转换程序为:
var
v:variant;
.......
v:=CreateOLEObject('Excel.Application');
............
V.ActiveCell.FormulaR1C1:=tblSource.Fields.AsString;
如文本型字段“合同编号”内容为"5-2",但转换到EXCEL中却为“5月20日”;
文本型字段“型号规格”内容为"1/16",但转换到EXCEL中却为“1月16日”;
请高手读解,50分;
(2)如何将Qreport中生成的预览结果导入到EXCEL中进行排版输出?
利用QR生成预览将基本数据生成想要的报表格式,希望能在EXCEL中排版。但在QR中
只能生成.Qpr之类的文件如何编程生成.xls文件请讲详细些?100分。
 
给我加分:
(我的DB2Excel类)
unit Excels;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,Dbtables;
type
TEPExcel=class
private
VExcel:variant;{Excel对象变量}
//FileName:string;{文件名}
public
constructor Create(FName:string);
procedure SetCells(CPosition:string;CValue:Variant;FontSize:integer);
procedure SetRange(rv:string;sx,ex,sy,ey,size:integer);
procedure Vsingle(var vquery:tquery;strqury,cc:string;fz:integer);
procedure Setvquary(var equery:Tquery;xs,ys,fs:integer);
procedure Cellmerge(cs,ce,ns,ne,fontsize:integer);
procedure Printpreview();
procedure Print();
procedure Save();
procedure Protect(password:string);
procedure WSCopy;
procedure PreviewBooks;
procedure Unprotect(pw:string);
procedure Unprotectworkbook(pwd:string);
end;

implementation
uses Comobj;

{创建一个Excel对象变量}
Constructor TEPExcel.Create(FName:string);
begin
VExcel:=CreateOleObject('Excel.Application');
VExcel.WorkBooks.Open(FName);
end;

{设置一个单元格属性}
procedure TEPExcel.SetCells(CPosition:string;CValue:Variant;FontSize:integer);
begin
VExcel.Range[CPosition].Font.Size:=FontSize;
VExcel.Range[CPosition].Font.Bold:=True;
VExcel.Range[CPosition].Value:=CValue;
end;

{设置区域属性}
procedure TEPExcel.SetRange(rv:string;sx,ex,sy,ey,size:integer);
const
{Excel的列标志号}
RowNo:array[1..26] of string=
('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
'R','S','T','U','V','W','X','Y','Z');
var
X:Integer;
Y:Integer;
CP:string;{单元格位置}
begin
for Y:=sy to ey do
begin
for X:=sx to ex do
begin
CP:=RowNo[X]+IntToStr(Y);
VExcel.Range[CP].Font.Bold:=True;
VExcel.Range[CP].Font.Size:=Size;
VExcel.Range[CP].Value:=rv;
end
end;
end;

procedure TEPExcel.setvquary(var equery:Tquery;xs,ys,fs:integer);
const
crs:array[1..26] of string=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
var
rangs:string;
i:integer;
j:integer;

begin

//equery.DatabaseName:=dabname;
//equery.TableName:=tabname;
equery.first;
for j:=ys to equery.recordcount-1+ys do
begin
for i:=xs to equery.fieldcount-1+xs do
begin
rangs:=crs+inttostr(j);
VExcel.range[rangs].font.bold:=true;
VExcel.range[rangs].font.size:=fs;
VExcel.range[rangs].value:=equery.fields[i-xs].asstring;
end;
equery.next;
end;
equery.Destroy;
end;

{合并单元格}
procedure TEPExcel.CellMerge(cs,ce,ns,ne,fontsize:integer);
const
cn:array[1..26] of string=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
var
scell:string;
begin
scell:=cn[cs]+inttostr(ns)+':'+cn[ce]+inttostr(ne);
VExcel.application.DisplayAlerts:=False;
VExcel.range[scell].merge;
VExcel.range[scell].font.size:=fontsize;
VExcel.range[scell].font.bold:=true;
VExcel.range[scell].WrapText:=True;
VExcel.application.DisplayAlerts:=true;
end;

{打印}
procedure TEPExcel.print();
begin
VExcel.range['b5'].activate;
VExcel.application.visible:=true;
VExcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
VExcel.Worksheets['Sheet1'].printout;
end;

{打印预览}
procedure TEPExcel.printpreview();
begin
VExcel.range['b5'].activate;
VExcel.application.visible:=true;
VExcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
VExcel.Worksheets['Sheet1'].printpreview;
end;


{存盘}
procedure TEPExcel.save();
begin
VExcel.ActiveWorkbook.Save
end;

{表保护}
procedure TEPExcel.protect(password:string);
begin
VExcel.worksheets['sheet1'].protect(password,true,true,true);
end;

procedure TEPExcel.Vsingle(var vquery:tquery;strqury,cc:string;fz:integer);
begin
vquery.close;
vquery.SQL.Clear;
vquery.SQL.add(strqury);
vquery.open;
VExcel.range[cc].value:=vquery.fields[0].asstring;
end;

procedure TEPExcel.WSCopy;
begin
VExcel.WorkSheets['Sheet1'].Copy(After:=VExcel.WorkSheets[VExcel.WorkSheets.Count]);
VExcel.WorkSheets[VExcel.Worksheets.Count].Protect('aa',true,true,true);
VExcel.workSheets['Sheet1'].Activate;
end;

procedure TEPExcel.PreviewBooks;
begin
VExcel.Application.Visible:=True;
VExcel.Application.ActiveWorkBook.PrintOut(Copies:=1, Preview:=True, Collate:=True );
end;

procedure TEPExcel.UnProtect(pw:string);
var
I:Integer;
begin
for I:=1 to VExcel.WorkSheets.Count do
begin
VExcel.WorkSheets.Select;
VExcel.ActiveSheet.UnProtect(pw);
end;
end;

{解除单元格保护}
procedure TEPExcel.UnProtectworkbook(pwd:string);
begin
VExcel.activeworkbook.unprotect(pwd);
end;

end.
 
对于第一个问题:
你在导出数据前没有设置单元格格式.如下就可解决:
Case FDataset.Fields[IntRow].DataType of
ftString,ftWord:
FExcelWorksheet.Range['A2','A20'].NumberFormat:='@';
ftCurrency:
FExcelWorksheet.Range['B2','B20'].NumberFormat:=CurrencyString+'#,##0.00;'+CurrencyString+'-#,##0.00';
...
end;
对于第二个问题:
直接从Query导出数据到Excel,再操纵Excel对单元格进行合并等完全可以达到要求.网上可找到许多这样的例子.
对于Qreport中生成的预览结果导入到EXCEL未听说过.其实Qreport也是取得数据后再Canvas.Draw.如果你写过
打印预览就明白其中道理了.
 

Similar threads

回复
0
查看
978
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部