怎么文件输出到excel中(100分)

  • 主题发起人 主题发起人 qinsir
  • 开始时间 开始时间
Q

qinsir

Unregistered / Unconfirmed
GUEST, unregistred user!
我要把我的数据库中的数据导出到excel中,而且还要能够控制每一行的高度,颜色
等等
 
告诉你几个方法解决:
1.这个问题的资料原来就有不少,自己动手查找,节约你的分数
2.用SERVER下面的EXECL控件
3.用VARRINT变量,查看VBA帮助解决
4.http://www.djpate.freeserve.co.uk/Automation.htm
好像一个都没有用处啊!^_^!不过思路都对的![:D]
 
这是个从Grid中导数据的代码



function ExportToExcel(SheetTitle: string; AGrid: TCustomGrid; AOrientation: integer): boolean;
var
MSExcel,ExcelBook,ExcelSheet: Variant;
TitleRows,TitleCols: string;
r,c,Stepc: integer;
CurrRec: TBookmark;
g: TCustomDBGrid;
begin
Result:=False;
try
MSExcel:=CreateOleObject('Excel.Application');
except
ShowMessage('Microsoft Excel启动失败,请检查Excel是否正确安装,或与系统管理员联系。');
Exit;
end;

try
ExcelBook:=MSExcel.WorkBooks.Add;
MSExcel.Visible:=True;

ExcelSheet:=ExcelBook.Worksheets[1];
// ExcelSheet.Name:=Title;
MSExcel.ActiveWindow.WindowState:=$FFFFEFD7;

TitleRows:='';
TitleCols:='';
if AGrid is TCustomDBGrid then begin
g:=TCustomDBGrid(AGrid);
with g do begin
//设置报表标题格式
ExcelSheet.Cells[1,1]:=SheetTitle;
FormatRange(ExcelSheet.Range[ExcelSheet.Cells[1,1],ExcelSheet.Cells[1,FieldCount]],xlCenter,xlCenter,False,True,'宋体','加粗',18,xlAutomatic,0);

//倒出字段名称
for c:=0 to FieldCount-1 do
ExcelSheet.Cells[2,c+1]:=Fields[c].DisplayName;

//设置表头格式
FormatRange(ExcelSheet.Range[ExcelSheet.Cells[2,1],ExcelSheet.Cells[2,FieldCount]],xlCenter,xlCenter,False,False,'宋体','加粗',14,xlAutomatic,36);

//设置书签
CurrRec:=DataSource.DataSet.GetBookmark;

//倒出所有字段值
DataSource.DataSet.First;
r:=3;
while not DataSource.DataSet.Eof do begin
for c:=0 to FieldCount-1 do
if AGrid is TSYDBGrid then
ExcelSheet.Cells[r,c+1]:=TSYDBGrid(g).GetSYFields(c)
else
ExcelSheet.Cells[r,c+1]:=Fields[c].AsString;
r:=r+1;
DataSource.DataSet.Next;
end;

//Goto书签,并释放书签资源
DataSource.DataSet.GotoBookmark(CurrRec);
DataSource.DataSet.FreeBookmark(Currrec);

//格式化输出的表格
FormatRangeAsGrid(ExcelSheet.Range[ExcelSheet.Cells[2,1],ExcelSheet.Cells[r-1,FieldCount]]);

TitleRows:='$2:$2';
end
end else if AGrid is TStringGrid then
with AGrid as TStringGrid do begin
//倒出所有数据
for r:=0 to RowCount-1 do begin
Stepc:=0;
for c:=0 to ColCount-1 do
if ColWidths[c]>0 then begin
ExcelSheet.Cells[r+2,stepc+1]:=Cells[c,r];
Stepc:=stepc+1;
end;
end;
//格式化标题
ExcelSheet.Cells[1,1]:=SheetTitle;
FormatRange(ExcelSheet.Range[ExcelSheet.Cells[1,1],ExcelSheet.Cells[1,Stepc]],xlCenter,xlCenter,False,True,'宋体','加粗',18,xlAutomatic,0);
//格式化表头
if FixedCols>0 then begin
FormatRange(ExcelSheet.Range[ExcelSheet.Cells[2,1],ExcelSheet.Cells[RowCount+1,FixedCols]],xlCenter,xlCenter,False,False,'宋体','加粗',14,xlAutomatic,36);
TitleRows:='$A:$'+Chr(FixedCols+64);
end;
if FixedRows>0 then begin
FormatRange(ExcelSheet.Range[ExcelSheet.Cells[2,1],ExcelSheet.Cells[FixedRows+1,Stepc]],xlCenter,xlCenter,False,False,'宋体','加粗',14,xlAutomatic,36);
TitleRows:='$2:$'+Chr(FixedRows+49);
end;

//格式化输出的表格
FormatRangeAsGrid(ExcelSheet.Range[ExcelSheet.Cells[2,1],ExcelSheet.Cells[RowCount+1,Stepc]]);
end;

ExcelSheet.UsedRange.Columns.AutoFit;
ExcelSheet.UsedRange.Rows.AutoFit;

//Format page setup in new Excel file
FormatPage(ExcelSheet,TitleRows,TitleCols,AOrientation);

ExcelSheet.Cells[1,1].Select;
Result:=True;
except
ShowMessage('数据倒出失败,请检查Excel是否正确安装,或与系统管理员联系。');
Exit;
end;
end;


procedure FormatPage(ASheet: Variant; APrintTitleRows, APrintTitleColumns: string;
AOrientation: integer);
begin
ASheet.PageSetup.PrintTitleRows := APrintTitleRows;
ASheet.PageSetup.PrintTitleColumns := APrintTitleColumns;
// ASheet.PageSetup.PrintArea := '';

// ASheet.PageSetup.LeftHeader := '';
// ASheet.PageSetup.CenterHeader := '';
// ASheet.PageSetup.RightHeader := '';
// ASheet.PageSetup.LeftFooter := '';
// ASheet.PageSetup.CenterFooter := '';
// ASheet.PageSetup.RightFooter := '';
// ASheet.PageSetup.LeftMargin := Application.InchesToPoints(0.748031496062992);
// ASheet.PageSetup.RightMargin := Application.InchesToPoints(0.748031496062992);
// ASheet.PageSetup.TopMargin := Application.InchesToPoints(0.984251968503937);
// ASheet.PageSetup.BottomMargin := Application.InchesToPoints(0.984251968503937);
// ASheet.PageSetup.HeaderMargin := Application.InchesToPoints(0.511811023622047);
// ASheet.PageSetup.FooterMargin := Application.InchesToPoints(0.511811023622047);
// ASheet.PageSetup.PrintHeadings := False;
// ASheet.PageSetup.PrintGridlines := False;
// ASheet.PageSetup.PrintComments := xlPrintNoComments;
// ASheet.PageSetup.PrintQuality := 600;
ASheet.PageSetup.CenterHorizontally := True;
// ASheet.PageSetup.CenterVertically := False;
ASheet.PageSetup.Orientation := AOrientation;
// ASheet.PageSetup.Draft := False;
ASheet.PageSetup.PaperSize := xlPaperA4;
// ASheet.PageSetup.FirstPageNumber := xlAutomatic;
// ASheet.PageSetup.Order := xlDownThenOver;
// ASheet.PageSetup.BlackAndWhite := False;
// ASheet.PageSetup.Zoom := 100;
// ASheet.PageSetup.Zoom := False;
// ASheet.PageSetup.FitToPagesWide := 1;
// ASheet.PageSetup.FitToPagesTall := 1;
end;


procedure FormatRange(ARange: Variant; AHorAlign,AVerAlign: integer; AWarpText,AMergeCells: boolean; AFontName,AFontStyle: string; AFontSize,AFontColor,AInteriorColor: integer);
begin
ARange.HorizontalAlignment := AHorAlign;
ARange.VerticalAlignment := AVerAlign;
ARange.WrapText := AWarpText;
ARange.Orientation := 0;
ARange.AddIndent := False;
ARange.ShrinkToFit := False;
ARange.MergeCells := AMergeCells;

ARange.Font.Name := AFontName;
ARange.Font.FontStyle := AFontStyle;
ARange.Font.Size := AFontSize;
ARange.Font.Strikethrough := False;
ARange.Font.Superscript := False;
ARange.Font.Subscript := False;
ARange.Font.OutlineFont := False;
ARange.Font.Shadow := False;
ARange.Font.Underline := xlUnderlineStyleNone;
ARange.Font.ColorIndex := AFontColor;

if AInteriorColor<>0 then begin
ARange.Interior.ColorIndex := AInteriorColor;
ARange.Interior.Pattern := 1;
ARange.Interior.PatternColorIndex := xlAutomatic;
end;
end;


procedure FormatRangeAsGrid(ARange: Variant);
begin
ARange.Borders[xlDiagonalDown].LineStyle := xlNone;
ARange.Borders[xlDiagonalUp].LineStyle := xlNone;

ARange.Borders[xlEdgeLeft].LineStyle := xlContinuous;
ARange.Borders[xlEdgeLeft].Weight := xlThin;
ARange.Borders[xlEdgeLeft].ColorIndex := xlAutomatic;

ARange.Borders[xlEdgeTop].LineStyle := xlContinuous;
ARange.Borders[xlEdgeTop].Weight := xlThin;
ARange.Borders[xlEdgeTop].ColorIndex := xlAutomatic;

ARange.Borders[xlEdgeBottom].LineStyle := xlContinuous;
ARange.Borders[xlEdgeBottom].Weight := xlThin;
ARange.Borders[xlEdgeBottom].ColorIndex := xlAutomatic;

ARange.Borders[xlEdgeRight].LineStyle := xlContinuous;
ARange.Borders[xlEdgeRight].Weight := xlThin;
ARange.Borders[xlEdgeRight].ColorIndex := xlAutomatic;

ARange.Borders[xlInsideVertical].LineStyle := xlContinuous;
ARange.Borders[xlInsideVertical].Weight := xlThin;
ARange.Borders[xlInsideVertical].ColorIndex := xlAutomatic;

ARange.Borders[xlInsideHorizontal].LineStyle := xlContinuous;
ARange.Borders[xlInsideHorizontal].Weight := xlThin;
ARange.Borders[xlInsideHorizontal].ColorIndex := xlAutomatic;
end;
 
上面兄弟的代码很全了,别人不用废话了。
 
多人接受答案了。
 
后退
顶部