Try
ExcelApp.Connect;
Except
End;
//打开文件
Excelbook.ConnectTo(ExcelApp.Workbooks.Open(FReportName,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,True,EmptyParam,
EmptyParam,EmptyParam,0));
//另存文件
ExcelApp.ActiveWorkbook.SaveAs(FReportName,xlNormal,'','',false,False,xlExclusive,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0);
//自己封装的部分函数
unit ExcelFunctions;
{BWQ_2005.08.02 Excel 常用操作函数封装
.这里建议尽量使用单元格的行、列进行数据操作;不过封装时也支持按单元格名称的操作
.在数据填充时如果不指定列的位置,按顺序填充满足不了实际表格的要求。因此FillData方法
又封装了一个带有列头名称的参数。(在使用过程中,在Excel中给每个固定的列定义一个名称)
.CopyRow 拷贝CurCell所在行,并在下一行进行插入。
}
interface
uses Excel97, OleServer,Db, DBClient,Dialogs,SysUtils,Classes;
Function GetCellValue(ExcelSheet:TExcelWorkSheet;CellName:String):Variant;Overload;
Function GetCellValue(ExcelSheet:TExcelWorkSheet;Row,Col:Integer):Variant;Overload;
Function SetCellValue(ExcelSheet:TExcelWorkSheet; CellName,Value:String):Boolean; Overload;
Function SetCellValue(ExcelSheet:TExcelWorkSheet; Row,Col:Integer;Value:String):Boolean; Overload;
Function InsertRow(ExcelSheet:TExcelWorkSheet; CurCell :String):Boolean;Overload;
Function InsertRow(ExcelSheet:TExcelWorkSheet; Row,Col:Integer):Boolean;Overload;
Function CopyRow(ExcelSheet:TExcelWorkSheet;CurCell:String):Boolean;OverLoad;
Function CopyRow(ExcelSheet:TExcelWorkSheet;Row,Col:Integer):Boolean;OverLoad;
Function CopyCol(ExcelSheet:TexcelWorkSheet;CurCell:String):Boolean;Overload;
Function CopyCol(ExcelSheet:TexcelWorkSheet;Row,Col:Integer):Boolean;Overload;
Function InsertCol(ExcelSheet:TExcelWorkSheet; CurCell :String):Boolean;Overload;
Function InsertCol(ExcelSheet:TExcelWorkSheet; Row,Col:Integer):Boolean;Overload;
Function MergeCell(ExcelSheet:TExcelWorkSheet; BeginCell,EndCell:String):Boolean;OverLoad;
Function MergeCell(ExcelSheet:TExcelWorkSheet; BeginRow,BeginCol,EndRow,EndCol:Integer):Boolean;OverLoad;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String;CDS:TClientDataSet;IsInsert:Boolean):Boolean;OverLoad;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String;ColName:TStringList):Boolean;OverLoad;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol:Integer;CDS:TClientDataSet;IsInsert:Boolean):Boolean;OverLoad;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String;Cols:TStringList;CDS:TClientDataSet;IsInsert:Boolean;IsCopyNextRow:Boolean= False):Boolean;OverLoad;
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginCell:String;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol:Integer;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginCell:String;IgnoreRow:TStringList;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
Function DataSum(ExcelSheet:TexcelWorkSheet;BeginCell,EndCell:String):Boolean;Overload;
Function DataSum(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol,EndRow,EndCol:Integer):Boolean;OverLoad;
implementation
Function GetCellValue(ExcelSheet:TExcelWorkSheet;CellName:String):Variant;Overload;
var
x,y :Integer;
begin
X := ExcelSheet.Range[CellName,CellName].Row;
Y := ExcelSheet.Range[CellName,CellName].Column;
Result := ExcelSheet.Cells.Item[X,Y];
end;
Function GetCellValue(ExcelSheet:TExcelWorkSheet;Row,Col:Integer):Variant;Overload;
begin
Result := ExcelSheet.Cells.Item[Row,Col];
end;
//单元格付值(按单元格名称)
Function SetCellValue(ExcelSheet:TExcelWorkSheet; CellName,Value:String):Boolean; Overload;
var
X,Y : Integer;
begin
ExcelSheet.Range[CellName,CellName].Select;
ExcelSheet.Range[CellName,CellName].Value := Value;
// ExcelSheet.Range[CellName,CellName].Cells.FormulaR1C1 := Value;
// X := ExcelSheet.Range[CellName,CellName].Row;
// Y := ExcelSheet.Range[CellName,CellName].Column;
// Excelsheet.Cells.Item[X,Y] := Value;
Result := True;
end;
//单元格付值(按行列位置)
Function SetCellValue(ExcelSheet:TExcelWorkSheet; Row,Col:Integer;Value:String):Boolean; Overload;
begin
Excelsheet.Cells.Item[Row,Col] := Value;
Result := True;
end;
//插入行
Function InsertRow(ExcelSheet:TExcelWorkSheet; CurCell :String):Boolean;Overload;
begin
ExcelSheet.Range[CurCell,CurCell].EntireColumn.Insert(EmptyParam);
end;
Function InsertRow(ExcelSheet:TExcelWorkSheet; Row,Col:Integer):Boolean;Overload;
begin
ExcelSheet.Range['A1','A1'].Cells.item[Row,Col].Select;
ExcelSheet.Range['A1','A1'].Cells.item[Row,Col].EntireRow.Insert(EmptyParam);
end;
//拷贝行
Function CopyRow(ExcelSheet:TExcelWorkSheet;CurCell:String):Boolean;
var
X,Y :Integer;
begin
X :=ExcelSheet.Range[CurCell,CurCell].Row;
Y :=ExcelSheet.Range[CurCell,CurCell].Column;
ExcelSheet.Rows.Range_['A'+IntToStr(X),'IV'+IntToStr(X)].Select;
ExcelSheet.Rows.Range_['A'+IntToStr(X),'IV'+IntToStr(X)].Copy(EmptyParam);
ExcelSheet.Rows.Range_['A'+IntToStr(X+1),'IV'+IntToStr(X+1)].Select;
ExcelSheet.Rows.Range_['A'+IntToStr(X+1),'IV'+IntToStr(X+1)].Insert(xlDown);
end;
Function CopyRow(ExcelSheet:TExcelWorkSheet;Row,Col:Integer):Boolean;OverLoad;
var
X,Y :Integer;
begin
X :=Row;
Y :=Col;
ExcelSheet.Rows.Range_['A'+IntToStr(X),'IV'+IntToStr(X)].Select;
ExcelSheet.Rows.Range_['A'+IntToStr(X),'IV'+IntToStr(X)].Copy(EmptyParam);
ExcelSheet.Rows.Range_['A'+IntToStr(X+1),'IV'+IntToStr(X+1)].Select;
ExcelSheet.Rows.Range_['A'+IntToStr(X+1),'IV'+IntToStr(X+1)].Insert(xlDown);
end;
Function CopyCol(ExcelSheet:TexcelWorkSheet;CurCell:String):Boolean;Overload;
var
X,Y :Integer;
begin
X :=ExcelSheet.Range[CurCell,CurCell].Row;
Y :=ExcelSheet.Range[CurCell,CurCell].Column;
ExcelSheet.Columns.Range_[ExcelSheet.Cells.item[1,Y],
ExcelSheet.Cells.item[65536,Y]].Select;
ExcelSheet.Columns.Range_[ExcelSheet.Cells.item[1,Y],
ExcelSheet.Cells.item[65536,Y]].Copy(EmptyParam);
ExcelSheet.Rows.Range_[ExcelSheet.Cells.item[1,Y+1],
ExcelSheet.Cells.item[65536,Y+1]].Select;
ExcelSheet.Rows.Range_[ExcelSheet.Cells.item[1,Y+1],
ExcelSheet.Cells.item[65536,Y+1]].Insert(xlRight);
end;
Function CopyCol(ExcelSheet:TexcelWorkSheet;Row,Col:Integer):Boolean;Overload;
var
X,Y :Integer;
begin
X :=Row;
Y :=Col;
ExcelSheet.Columns.Range_[ExcelSheet.Cells.item[1,Y],
ExcelSheet.Cells.item[65536,Y]].Select;
ExcelSheet.Columns.Range_[ExcelSheet.Cells.item[1,Y],
ExcelSheet.Cells.item[65536,Y]].Copy(EmptyParam);
ExcelSheet.Rows.Range_[ExcelSheet.Cells.item[1,Y+1],
ExcelSheet.Cells.item[65536,Y+1]].Select;
ExcelSheet.Rows.Range_[ExcelSheet.Cells.item[1,Y+1],
ExcelSheet.Cells.item[65536,Y+1]].Insert(xlRight);
end;
//插入列
Function InsertCol(ExcelSheet:TExcelWorkSheet; CurCell :String):Boolean;
begin
ExcelSheet.Range[CurCell,CurCell].EntireRow.Insert(EmptyParam);
end;
Function InsertCol(ExcelSheet:TExcelWorkSheet; Row,Col:Integer):Boolean;Overload;
begin
ExcelSheet.Range['A1','A1'].Cells.item[Row,Col].Select;
ExcelSheet.Range['A1','A1'].Cells.item[Row,Col].EntireColumn.Insert(EmptyParam);
end;
//合并单元格
Function MergeCell(ExcelSheet:TExcelWorkSheet; BeginCell,EndCell:String):Boolean;
var
tmpValue :String;
begin
//合并单元格;
//先保留所选区域的左上角第一个单元格的数据,然后清除所选区域的所有数据,
//最后再把左上角的数据恢复。{这样处理就不会有提示}
Result := True;
tmpValue := ExcelSheet.Cells.item[ExcelSheet.Range[BeginCell,EndCell].Row,
ExcelSheet.Range[BeginCell,EndCell].Column];
ExcelSheet.Range[BeginCell,EndCell].Select;
ExcelSheet.Range[BeginCell,EndCell].Clear;
ExcelSheet.Range[BeginCell,EndCell].HorizontalAlignment := xlLeft;
ExcelSheet.Range[BeginCell,EndCell].VerticalAlignment := xlTop;
ExcelSheet.Range[BeginCell,EndCell].WrapText := False;
ExcelSheet.Range[BeginCell,EndCell].Orientation := 0;
ExcelSheet.Range[BeginCell,EndCell].AddIndent := False;
ExcelSheet.Range[BeginCell,EndCell].IndentLevel := 0;
ExcelSheet.Range[BeginCell,EndCell].ShrinkToFit := False;
ExcelSheet.Range[BeginCell,EndCell].MergeCells := True;
ExcelSheet.Cells.item[ExcelSheet.Range[BeginCell,EndCell].Row,
ExcelSheet.Range[BeginCell,EndCell].Column] := tmpValue;
end;
Function MergeCell(ExcelSheet:TExcelWorkSheet; BeginRow,BeginCol,EndRow,EndCol:Integer):Boolean;OverLoad;
var
tmpValue :String;
begin
//合并单元格;
//先保留所选区域的左上角第一个单元格的数据,然后清除所选区域的所有数据,
//最后再把左上角的数据恢复。{这样处理就不会有提示}
Result := True;
tmpValue := ExcelSheet.Cells.item[BeginRow,BeginCol];
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].Select;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].Clear;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].HorizontalAlignment := xlLeft;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].VerticalAlignment := xlTop;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].WrapText := False;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].Orientation := 0;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].AddIndent := False;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].IndentLevel := 0;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].ShrinkToFit := False;
ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
ExcelSheet.Cells.item[EndRow,EndCol]].MergeCells := True;
ExcelSheet.Cells.item[BeginRow,BeginCol] := tmpValue;
end;
//区域填充数据 (按行填充,用于列固定的表格)
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String;CDS:TClientDataSet;IsInsert:Boolean):Boolean;
var
I,F,X,Y :Integer;
begin
//这里插入数据是在BeginCell的下一行开始
Result := False;
if CDS.RecordCount= 0 then exit;
X := ExcelSheet.Range[BeginCell,BeginCell].Row;
Y := ExcelSheet.Range[BeginCell,BeginCell].Column;
For I := 1 to CDS.RecordCount do
begin
if IsInsert then
InsertRow(ExcelSheet,X+I,Y);
For F:= 0 to CDS.FieldCount -1 do
begin
SetCellValue(ExcelSheet,X+I,Y+F,CDS.Fields[F].asString);
end;
CDS.Next;
end;
end;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String;ColName:TStringList):Boolean;OverLoad;
var
I,F,X,Y :Integer;
begin
//这里插入数据是在BeginCell的下一行开始
Result := False;
if ColName.Count = 0 then exit;
X := ExcelSheet.Range[BeginCell,BeginCell].Row;
Y := ExcelSheet.Range[BeginCell,BeginCell].Column;
For I := 0 to ColName.Count-1 do
begin
CopyCol(ExcelSheet,x,Y+I);
SetCellValue(ExcelSheet,X,Y+I,ColName[I]);
end;
end;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol:Integer;CDS:TClientDataSet;IsInsert:Boolean):Boolean;OverLoad;
var
I,F,X,Y :Integer;
begin
//这里插入数据是在BeginCell的下一行开始
Result := False;
if CDS.RecordCount= 0 then exit;
X := BeginRow;
Y := BeginCol;
For I := 1 to CDS.RecordCount do
begin
if IsInsert then
InsertRow(ExcelSheet,X+I,Y);
For F:= 0 to CDS.FieldCount -1 do
begin
SetCellValue(ExcelSheet,X+I,Y+F,CDS.Fields[F].asString);
end;
CDS.Next;
end;
end;
Function FillData(ExcelSheet:TexcelWorkSheet;BeginCell:String; Cols:TStringList;CDS:TClientDataSet;IsInsert:Boolean;IsCopyNextRow:Boolean= False):Boolean;OverLoad;
var
I,F,X,Y,Col :Integer;
begin
//这里插入数据是在BeginCell的下一行开始
Result := False;
if CDS.RecordCount= 0 then exit;
X := ExcelSheet.Range[BeginCell,BeginCell].Row;
Y := ExcelSheet.Range[BeginCell,BeginCell].Column;
For I := 1 to CDS.RecordCount do
begin
if IsCopyNextRow then
CopyRow(ExcelSheet,X+I,Y)
else if IsInsert then
InsertRow(ExcelSheet,X+I,Y);
For F:= 0 to CDS.FieldCount -1 do
begin
Col := ExcelSheet.Range[Cols[F],Cols[F]].Column;
SetCellValue(ExcelSheet,X+I,Col,CDS.Fields[F].asString);
end;
CDS.Next;
end;
end;
//填充列数据(按列填充,用于行固定的表格)
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginCell:String;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
var
I,F,X,Y :Integer;
begin
Result := False;
if CDS.RecordCount= 0 then exit;
X := ExcelSheet.Range[BeginCell,BeginCell].Row;
Y := ExcelSheet.Range[BeginCell,BeginCell].Column;
For I := 0 to CDS.RecordCount-1 do
begin
if IsInsert and IsCopyCol then
CopyCol(ExcelSheet,X,Y+I)
else if IsInsert then
InsertCol(ExcelSheet,X,Y+I);
For F:= 0 to CDS.FieldCount-1 do
begin
SetCellValue(ExcelSheet,X+F,Y+I,CDS.Fields[F].asString);
end;
CDS.Next;
end;
end;
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol:Integer;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
var
I,F,X,Y :Integer;
begin
Result := False;
if CDS.RecordCount= 0 then exit;
X := BeginRow;
Y := BeginCol;
For I := 0 to CDS.RecordCount-1 do
begin
if IsInsert and IsCopyCol then
CopyCol(ExcelSheet,X,Y+I)
else if IsInsert then
InsertCol(ExcelSheet,X,Y+I);
For F:= 0 to CDS.FieldCount-1 do
begin
SetCellValue(ExcelSheet,X+F,Y+I,CDS.Fields[F].asString);
end;
CDS.Next;
end;
end;
Function FillColData(ExcelSheet:TexcelWorkSheet;BeginCell:String;IgnoreRow:TStringList;CDS:TClientDataSet;IsInsert:Boolean;IsCopyCol:Boolean=False):Boolean;OverLoad;
var
I,F,X,Y,J,CurRow :Integer;
begin
Result := False;
if CDS.RecordCount= 0 then exit;
X := ExcelSheet.Range[BeginCell,BeginCell].Row;
Y := ExcelSheet.Range[BeginCell,BeginCell].Column;
For I := 0 to CDS.RecordCount-1 do
begin
if IsInsert and IsCopyCol then
CopyCol(ExcelSheet,X,Y+I)
else if IsInsert then
InsertCol(ExcelSheet,X,Y+I);
CurRow := X;
For F:= 0 to CDS.FieldCount-1 do //按字段顺序填入列;
begin
J :=0;
while J<= IgnoreRow.Count-1 do
begin
if CurRow = StrToInt(IgnoreRow[J]) then //排除不需要填写的行
begin
CurRow := CurRow+1;
J :=0;
end
else
J:=J+1;
end;
SetCellValue(ExcelSheet,CurRow,Y+I,CDS.Fields[F].asString);
CurRow :=CurRow +1;
end;
CDS.Next;
end;
end;
Function DataSum(ExcelSheet:TexcelWorkSheet;BeginCell,EndCell:String):Boolean;Overload;
begin
end;
Function DataSum(ExcelSheet:TexcelWorkSheet;BeginRow,BeginCol,EndRow,EndCol:Integer):Boolean;OverLoad;
begin
// ExcelSheet.Range[ExcelSheet.Cells.item[BeginRow,BeginCol],
// ExcelSheet.Cells.item[EndRow,EndCol]].Select;
ExcelSheet.Range[ExcelSheet.Cells.item[EndRow,EndCol],
ExcelSheet.Cells.item[EndRow,EndCol]].Activate;
ExcelSheet.Range[ExcelSheet.Cells.item[EndRow,EndCol],
ExcelSheet.Cells.item[EndRow,EndCol]].FormulaR1C1 :='=SUM(R[-'+IntToStr(EndRow-BeginRow)+']C:R[-1]C)' ;
end;
end.