請看如下程序,我想指定第5列導出時為字符型,請問怎么做啊?
//導出到EXCEL文件
procedure TfrmPrtDepartCharge.OutExcel(Title: String; DBGrid: TDBGrid);
var
MsExcel, WorkBook, WorkSheet: Variant;
i,j: Integer;
Row, Col: Integer;
FieldName: string;
DataSet: TDataSet;
S: String;
begin
try
MsExcel := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('您的PC中未安裝Microsoft Excel.','',32);
Exit;
end; WorkBook := MsExcel.WorkBooks.Add;
WorkSheet := MsExcel.WorkSheets.Add; MsExcel.Visible := True;
Col := 1;
MsExcel.Cells(1,Col) := Title;
Row := 3; DataSet := DBGrid.DataSource.DataSet;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if DBGrid.Columns[I].Visible then
begin
FieldName := DBGrid.Columns[I].Title.Caption;
MsExcel.Cells(Row, Col) := FieldName;
Col := Col + 1;
end;
end;// WorkSheet.cells(5,Col).NumberFormatLocal := '@'; Row := Row + 1; DataSet.First;
while not DataSet.Eof do
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
FieldName := DBGrid.Columns[J].FieldName;
MsExcel.Cells(Row, Col) :=' '+ DataSet.FieldByName(FieldName).AsString + ' ';
Col := Col + 1;
end;
Row:= Row + 1;
DataSet.Next;
end; Col := 1;
For J:=0 to DBGrid.Columns.Count - 1 do
begin S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end; if J = 0 then
begin
MsExcel.Cells(Row, Col):= '合計:';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger,ftSmallint,ftFloat,ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
MsExcel.Cells(Row, Col) := '=SUM('+ S +'4:'+S+IntToStr(Row-1)+')';
end;
Col:= Col + 1;
end; end;
//導出到EXCEL文件
procedure TfrmPrtDepartCharge.OutExcel(Title: String; DBGrid: TDBGrid);
var
MsExcel, WorkBook, WorkSheet: Variant;
i,j: Integer;
Row, Col: Integer;
FieldName: string;
DataSet: TDataSet;
S: String;
begin
try
MsExcel := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('您的PC中未安裝Microsoft Excel.','',32);
Exit;
end; WorkBook := MsExcel.WorkBooks.Add;
WorkSheet := MsExcel.WorkSheets.Add; MsExcel.Visible := True;
Col := 1;
MsExcel.Cells(1,Col) := Title;
Row := 3; DataSet := DBGrid.DataSource.DataSet;
for I := 0 to DBGrid.Columns.Count - 1 do
begin
if DBGrid.Columns[I].Visible then
begin
FieldName := DBGrid.Columns[I].Title.Caption;
MsExcel.Cells(Row, Col) := FieldName;
Col := Col + 1;
end;
end;// WorkSheet.cells(5,Col).NumberFormatLocal := '@'; Row := Row + 1; DataSet.First;
while not DataSet.Eof do
begin
Col := 1;
for J := 0 to DBGrid.Columns.Count - 1 do
begin
FieldName := DBGrid.Columns[J].FieldName;
MsExcel.Cells(Row, Col) :=' '+ DataSet.FieldByName(FieldName).AsString + ' ';
Col := Col + 1;
end;
Row:= Row + 1;
DataSet.Next;
end; Col := 1;
For J:=0 to DBGrid.Columns.Count - 1 do
begin S := Char(64 + ((J+1) mod 26));
if (J+1) > 26 then
begin
S := Char(65+(((J+1)-26) div 26)) + S;
end; if J = 0 then
begin
MsExcel.Cells(Row, Col):= '合計:';
end
else if DBGrid.Columns[J].Field.DataType in [ftInteger,ftSmallint,ftFloat,ftBCD] then
begin
FieldName := DBGrid.Columns[J].FieldName;
MsExcel.Cells(Row, Col) := '=SUM('+ S +'4:'+S+IntToStr(Row-1)+')';
end;
Col:= Col + 1;
end; end;
// WorkSheet.cells(5,Col).NumberFormatLocal := '@';