XLSReadWriteII控件针对cxTreeList导出EXCEL实例
最近使用XLSReadWrite进行Excel导出,效率确实超高!比原生的Ole导出快了好多....
一、要导出的原始界面数据样式
二、目标输出EXEl样式
三、导出关键代码:
function TExportExcel.WriteFYB(const ASheet: TXLSWorksheet): Integer;
const
cnOrderNo_B = 0; cnName_B = 1; cnQty_B = 2; cnPrice_B = 3;
cnTotal_B = 4; cnOrderNo_C = 5; cnName_C = 6; cnQty_C = 7;
cnPrice_C = 8; cnTotal_C = 9;
procedure AddRow(ANode: TcxTreeListNode; var ARow: Integer);
var
i: Integer;
begin
Inc(ARow);
ASheet.Rows[ARow].PixelHeight := 45;
for i := cnOrderNo_B to cnTotal_C do
begin
ASheet.AsString[i, ARow] := ANode.Texts[i];
if (FSrcTree.Columns[i].Properties <> nil) and
(FSrcTree.Columns[i].PropertiesClassName = 'TcxCurrencyEditProperties') then
ASheet.Cell[i, ARow].HorizAlignment := chaRight
else
ASheet.Cell[i, ARow].HorizAlignment := chaLeft;
ASheet.Cell[i, ARow].VertAlignment := cvaCenter;
end;
//子项递归
for i := 0 to ANode.Count - 1 do
AddRow(ANode.Items[i], ARow);
end;
var
nRow, nRowBegin, nRowEnd, i: Integer;
sPath: string;
oProj: ICPProject;
begin
Result := 0;
//工程路径
sPath := FLinkProj.Caption;
oProj := FLinkProj.Parent;
while oProj <> nil do
begin
sPath := oProj.Caption + '\' + sPath;
oProj := oProj.Parent;
end;
//列宽定义
ASheet.Columns[cnOrderNo_B].PixelWidth := 10 * 5;
ASheet.Columns[cnName_B].PixelWidth := 10 * 10;
ASheet.Columns[cnQty_B].PixelWidth := 10 * 20;
ASheet.Columns[cnOrderNo_C].PixelWidth := 10 * 5;
ASheet.Columns[cnName_C].PixelWidth := 10 * 10;
ASheet.Columns[cnQty_C].PixelWidth := 10 * 20;
//第一行空
nRow := 0;
ASheet.AsString[cnOrderNo_B, nRow] := ' ';
Inc(nRow);
ASheet.Rows[nRow].FontName := '宋体';
ASheet.Rows[nRow].FontSize := 24;
ASheet.Rows[nRow].FontStyle := [xfsBold];
ASheet.AsString[cnOrderNo_B, nRow] := '费用对比表';
ASheet.MergeCells(cnOrderNo_B, nRow, cnPrice_C, nRow);
ASheet.Cell[cnOrderNo_B, nRow].HorizAlignment := chaCenter;
ASheet.Cell[cnOrderNo_B, nRow].VertAlignment := cvaCenter;
Inc(nRow);
ASheet.AsString[cnOrderNo_B, nRow] := ' 文件名:' + ExtractFileName(FLinkFile.FileName);
ASheet.MergeCells(cnOrderNo_B, nRow, cnOrderNo_C, nRow);
Inc(nRow);
ASheet.Rows[nRow].PixelHeight := 25;
ASheet.AsString[cnOrderNo_B, nRow] := ' 工程名称:' + sPath;
ASheet.Cell[cnOrderNo_B, nRow].VertAlignment := cvaCenter;
ASheet.MergeCells(cnOrderNo_B, nRow, cnOrderNo_C, nRow);
ASheet.AsString[cnPrice_C, nRow] := '单位:元(人民币)';
ASheet.Cell[cnPrice_C, nRow].VertAlignment := cvaCenter;
ASheet.MergeCells(cnPrice_C, nRow, cnTotal_C, nRow);
nRowBegin := nRow;
//表格绘制区
Inc(nRow);
ASheet.Rows[nRow].PixelHeight := 40;
ASheet.AsString[cnOrderNo_B, nRow] := FSrcTree.Columns[cnOrderNo_B].Position.Band.Caption.Text;
ASheet.Cell[cnOrderNo_B, nRow].FontStyle := [xfsBold];
ASheet.Cell[cnOrderNo_B, nRow].VertAlignment := cvaCenter;
ASheet.Cell[cnOrderNo_B, nRow].HorizAlignment := chaCenter;
ASheet.MergeCells(cnOrderNo_B, nRow, cnTotal_B, nRow);
ASheet.AsString[cnOrderNo_C, nRow] := FSrcTree.Columns[cnOrderNo_C].Position.Band.Caption.Text; //对比文件序号不显示
ASheet.Cell[cnOrderNo_C, nRow].FontStyle := [xfsBold];
ASheet.Cell[cnOrderNo_C, nRow].VertAlignment := cvaCenter;
ASheet.Cell[cnOrderNo_C, nRow].HorizAlignment := chaCenter;
ASheet.MergeCells(cnOrderNo_C, nRow, cnTotal_C, nRow);
Inc(nRow);
ASheet.Rows[nRow].FontStyle := [xfsBold];
ASheet.Rows[nRow].VertAlignment := cvaCenter;
ASheet.Rows[nRow].HorizAlignment := chaCenter;
//填充表格列头
for i := cnOrderNo_B to cnTotal_C do
begin
ASheet.AsString[i, nRow] := FSrcTree.Columns[i].Caption.Text;
end;
//填充数据区
for i := 0 to FSrcTree.Count - 1 do
begin
AddRow(FSrcTree.Items[i], nRow);
end;
inc(nRow);
nRowEnd := nRow;
//为表格画框格线
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin + 2, nRowEnd])].BorderInsideVertStyle := cbsThin;
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin + 2, nRowEnd])].BorderInsideHorizStyle := cbsThin;
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin + 2, nRowEnd])].BorderOutlineStyle := cbsThin;
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin + 2, nRowEnd])].WrapText := True;
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin, nRowEnd])].FontName := '宋体';
ASheet.Range.ItemsRef[Format('A%d:J%d', [nRowBegin, nRowEnd])].FontSize := 9;
end;
四、蓝色字体代码的说明
最初我想要通过下面代码,实现控制行高,而且让文字纵向居中显示,可是输出结果是,行高和居中都没有达到预期的结果,现在看来设置行高后,要定义文字的居中或者字体样式,只能是单元格范围内来进行设置了。
ASheet.Rows[nRow].PixelHeight := 25;
ASheet.Rows[nRow].VertAlignment := cvaCenter;
ASheet.AsString[cnOrderNo_B, nRow] := ' 工程名称:' + sPath;
ASheet.MergeCells(cnOrderNo_B, nRow, cnOrderNo_C, nRow);
ASheet.AsString[cnPrice_C, nRow] := '单位:元(人民币)';
ASheet.MergeCells(cnPrice_C, nRow, cnTotal_C, nRow);