除了XLSReadWriteII5,还有个NativeExcel也是比较好的操作excel的组件,现将NativeExcel3的使用示例写一下,以下是代码和生成的excel表格的效果:

procedure TForm1.Button2Click(Sender: TObject);
var
i, n: Integer;
XLS: IXLSWorkbook; // 引用nExcel, ShellAPI
ws: IXLSWorksheet;
begin
XLS := TXLSWorkbook.Create;
try
ws := XLS.Sheets.Add;
ws.Name := '导出';
// 注意NativeExcel是从1开始的,不是0开始
for i := to do
ws.Cells.Item[, i].Value := '标题' + IntToStr(i);
for i := to do
for n := to do
With ws.Cells.Item[n, i] do
begin
Value := IntToStr(i) + ':' + IntToStr(n - );
if ColumnWidth < Length(AnsiString(Value)) then // 自动列宽
ColumnWidth := Length(AnsiString(Value));
end; for i := to do // 从第一列到最后一列
begin
for n := to do // 从第一行到最后一行
begin
With ws.Cells.Item[n, i] do
begin
Borders.LineStyle := xlContinuous;
Borders.Color := clBlack;
// 黑色#0
if n = then
begin
Interior.Color := clWebOrange; // RGB(255, 140, 0); // 橘黄#FF8000
Font.Color := clWhite;
HorizontalAlignment := xlHAlignCenter;
end
else
Interior.Color := RGB(, , ); // 杏仁灰#FFFFCD
end;
end;
end;
XLS.SaveAs(ExtractFilePath(paramstr()) + 'temp.xls', xlOpenXMLWorkbook);
ShellExecute(, 'Open', PChar(ExtractFilePath(paramstr()) + 'temp.xls')
, nil, nil, SW_SHOW);
finally
XLS.close;
end;
end;
 
05-20 03:27