//方便经典的Delphi 6/7 调用使用,输出速度杠杠的
//代码在Delphi XE下编译,必须先装Dev套件哦
library exportxlsx;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
Winapi.Windows,
System.SysUtils,
System.Classes,
vcl.Graphics,
Forms,
dxCore,
dxCoreClasses,
dxHashUtils,
dxSpreadSheetCore,
dxSpreadSheetCoreHistory,
dxSpreadSheetConditionalFormatting,
dxSpreadSheetConditionalFormattingRules,
dxSpreadSheetClasses,
dxSpreadSheetContainers,
dxSpreadSheetFormulas,
dxSpreadSheetHyperlinks,
dxSpreadSheetFunctions,
cxGraphics,
dxSpreadSheetGraphics,
dxSpreadSheetPrinting,
dxSpreadSheetTypes,
dxSpreadSheetUtils,
dxBarBuiltInMenu,
dxSpreadSheet;
{$R *.res}
var
xlsDoc: TdxSpreadSheet=nil;
ASheet: TdxSpreadSheetTableView;
xlsFileName:string;
DefaultBorder:Boolean;
docCodePage:Cardinal=0;
function CreateExportFileA(AFilename,ATitle:PAnsiChar;ACodePage:Cardinal):LongInt;stdcall;
var
sn:string;
begin
docCodePage:=ACodePage;
if Assigned(xlsDoc) then
begin
xlsDoc.Free;
xlsDoc:=nil;
end;
try
xlsFileName:=dxAnsiStringToString(afilename);
xlsDoc:=TdxSpreadSheet.Create(nil);
xlsdoc.Visible:=false;
ASheet:=xlsdoc.Sheets[0] as TdxSpreadSheetTableView;
sn:=dxAnsiStringToString(atitle,ACodePage);
if sn='' then
sn:=extractfilename(xlsFilename);
Asheet.Caption:=changefileext(sn,'');
xlsdoc.DefaultCellStyle.Font.Name:='Tahoma';
xlsdoc.DefaultCellStyle.Font.Size:=11;
result:=LongInt(Pointer(xlsDoc));
except
result:=0;
end;
end;
function CreateExportFile(AFilename,ATitle:PChar;ACodePage:Cardinal):LongInt;stdcall;
var
sn:string;
begin
docCodePage:=ACodePage;
if Assigned(xlsDoc) then
begin
xlsDoc.Free;
xlsDoc:=nil;
end;
try
xlsFileName:=strpas(afilename);
xlsDoc:=TdxSpreadSheet.Create(nil);
xlsdoc.Visible:=false;
ASheet:=xlsdoc.Sheets[0] as TdxSpreadSheetTableView;
sn:=strpas(atitle);
if sn='' then
sn:=extractfilename(xlsFilename);
Asheet.Caption:=changefileext(sn,'');
// xlsdoc.DefaultCellStyle.Font.Charset
xlsdoc.DefaultCellStyle.Font.Name:='Tahoma';
xlsdoc.DefaultCellStyle.Font.Size:=11;
result:=LongInt(Pointer(xlsDoc));
except
result:=0;
end;
end;
procedure BeginExport;stdcall;
begin
ASheet.BeginUpdate;
end;
procedure EndExport;stdcall;
begin
try
try
ASheet.EndUpdate;
xlsDoc.savetofile(xlsfilename);
except
on e:exception do
Application.MessageBox(pchar(e.Message),'错误',MB_OK + MB_ICONERROR);
end;
finally
xlsDoc.Free;
xlsDoc:=nil;
end;
end;
procedure SetCellDefaultStyle(ACharset:Byte;fontName:PChar;fontSize:Integer;ABorder:Boolean);stdcall;
begin
xlsdoc.DefaultCellStyle.Font.Charset:=ACharset;
xlsdoc.DefaultCellStyle.Font.Name:=strpas(fontname);
xlsdoc.DefaultCellStyle.Font.Size:=fontSize;
DefaultBorder:=ABorder;
end;
procedure SetCellDefaultStyleA(ACharset:Byte;fontName:PAnsiChar;fontSize:Integer;ABorder:Boolean);stdcall;
begin
xlsdoc.DefaultCellStyle.Font.Charset:=ACharset;
xlsdoc.DefaultCellStyle.Font.Name:=dxAnsiStringToString(fontname,docCodePage);
xlsdoc.DefaultCellStyle.Font.Size:=fontSize;
DefaultBorder:=ABorder;
end;
Procedure CreateDataCells(AColCC,ARowCC:Integer;ABorder:Boolean);stdcall;
var
c,r:integer;
acell:TdxSpreadSheetCell;
begin
for r := 0 to aRowCC-1 do
for c := 0 to acolcc-1 do
begin
ACell:=ASheet.CreateCell(r,c);
if aborder or DefaultBorder then
with acell.Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
AlignHorz:=ssahLeft;
AlignVert:=ssavCenter;
end;
end;
end;
procedure SetColumnWidth(const ACol, ARow: Integer;AWidth:Integer);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
column.Size:=Awidth;
end;
end;
procedure SetCellStyle(const ACol, ARow: Integer;ABorder:Boolean;AWidth,AHAlign,AVAlign:Integer;AWordWrap:Boolean);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
column.Size:=Awidth;
with style do
begin
if aborder then
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
end;
WordWrap:=AWordWrap;
AlignHorz:=TdxSpreadSheetDataAlignHorz(AHAlign);
AlignVert:=TdxSpreadSheetDataAlignVert(AVAlign);
end;
end;
end;
procedure SetCellDataBoolean(const ACol, ARow: Integer; const AValue: Boolean);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsBoolean:=Avalue;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataCurrency(const ACol, ARow: Integer; const AValue: Currency);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsCurrency:=Avalue;
Style.AlignHorz:=ssahRight;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataDouble(const ACol, ARow: Integer; const AValue: Double);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsFloat:=Avalue;
Style.AlignHorz:=ssahRight;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataDateTime(const ACol, ARow: Integer; const AValue: TDateTime);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsDateTime:=Avalue;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataInteger(const ACol, ARow: Integer; const AValue: Integer);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsInteger:=Avalue;
Style.AlignHorz:=ssahRight;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataStringA(const ACol, ARow: Integer; AText: PAnsiChar);stdcall;
var
s:string;
begin
s:=dxAnsiStringToWideString(atext,docCodePage);
//if strtointdef(s,0)>0 then
// s:=''''+s;
with ASheet.CreateCell(arow,acol) do
begin
//settext(s,false);
asString:=s;
Style.AlignHorz:=ssahLeft;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellDataString(const ACol, ARow: Integer; AText: PChar);stdcall;
var
s:string;
begin
s:=strpas(atext);
//if strtointdef(s,0)>0 then
// s:=''''+s;
with ASheet.CreateCell(arow,acol) do
begin
//settext(s,false);
asString:=s;
Style.AlignHorz:=ssahLeft;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetHeaderStringA(const ACol, ARow: Integer; AText: PAnsiChar);stdcall;
var
s:string;
begin
s:=dxAnsiStringToWideString(atext,docCodePage);
with ASheet.CreateCell(arow,acol) do
begin
asString:=s;//settext(s,false);
Style.AlignHorz:=ssahCenter;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetHeaderString(const ACol, ARow: Integer; AText: PChar);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
//settext(strpas(atext),false);
asString:=strpas(atext);
Style.AlignHorz:=ssahCenter;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
font.Charset:=xlsdoc.DefaultCellStyle.Font.Charset;
font.Name:=xlsdoc.DefaultCellStyle.Font.Name;
font.Size:=xlsdoc.DefaultCellStyle.Font.Size;
end;
end;
end;
procedure SetCellMerged(const ACol, ARow: Integer; W, H: Integer);stdcall;
var
r:Trect;
begin
r:=Asheet.MergedCells.CheckCell(arow,acol);
r.Right:=r.left+W;
r.Bottom:=h;
Asheet.MergedCells.Add(r);
end;
procedure SetCellValue(const ACol, ARow: Integer; const AValue: Variant);stdcall;
begin
with ASheet.CreateCell(arow,acol) do
begin
AsVariant:=Avalue;
Style.AlignHorz:=ssahLeft;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellFormulaA(const ACol, ARow: Integer; const AValue: PAnsiChar);stdcall;
var
s:string;
begin
s:=dxAnsiStringToWideString(AValue,docCodePage);
with ASheet.CreateCell(arow,acol) do
begin
settext(s,true);
Style.AlignHorz:=ssahRight;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
procedure SetCellFormula(const ACol, ARow: Integer; const AValue: PChar);stdcall;
var
s:string;
begin
s:=strpas(Avalue);
with ASheet.CreateCell(arow,acol) do
begin
settext(s,true);
Style.AlignHorz:=ssahRight;
if DefaultBorder then
with Style do
begin
Borders[bLeft].Style:=sscbsThin;
Borders[bRight].Style:=sscbsThin;
Borders[bTop].Style:=sscbsThin;
Borders[bBottom].Style:=sscbsThin;
Borders[bLeft].Color:=clGray;
Borders[bRight].Color:=clGray;
Borders[bTop].Color:=clGray;
Borders[bBottom].Color:=clGray;
end;
end;
end;
exports
CreateExportFileA,CreateExportFile,EndExport,BeginExport,
SetCellDefaultStyle,SetCellDefaultStyleA,CreateDataCells,SetCellStyle,SetColumnWidth,SetCellMerged,
SetCellDataBoolean,SetCellDataCurrency,SetCellDataDouble,SetCellDataInteger,SetCellDataDatetime,
SetCellDataStringA,SetCellDataString,SetHeaderStringA,SetHeaderString,
SetCellValue,SetCellFormulaA,SetCellFormula;
begin
end.