定义EXCEL里面单元格的格式

simon77_shen 2002-09-12 11:50:42
问题一:
当我想让单元格向右靠的时候
ExlApp.Selection.HorizontalAlignment := xlRight;
但是却没有这个效果,就只有
ExlApp.Selection.HorizontalAlignment := 3;(表示靠中),这是可以的
不知道我上面的用什么来表示,我用2还是不行的,1好象是靠左是吗?
问题二:
我想让单元格的边框都有实线生成,我用宏做了一下
比如:
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
。。。。。

我用ExlApp.Selection.Borders(xlDiagonalDown).LineStyle := xlNone;
却编译不过去,我其他类似的属性都可以成功的,就这个边框画实线的不行?
请大家帮忙!!!1



...全文
66 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
simon77_shen 2002-09-16
  • 打赏
  • 举报
回复
我知道了,是我的错误,谢谢,给分!
kaidu 2002-09-13
  • 打赏
  • 举报
回复
//设置页面
if PageSize=1 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperA3; //纸张大小 :A3
if PageSize=2 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperA4; //纸张大小 :A4
if PageSize=3 then
ExlApp.ActiveSheet.PageSetup.PaperSize:=xlPaperB5; //纸张大小 :B5
if PageLay=1 then
ExlApp.ActiveSheet.PageSetup.Orientation:=xlportrait; //页面放置方向:纵向
if PageLay=2 then
ExlApp.ActiveSheet.PageSetup.Orientation:=xlLandscape; //页面放置方向:横向

//设置页宽自动适应
ExlApp.ActiveSheet.PageSetup.Zoom := False;
ExlApp.ActiveSheet.PageSetup.FitToPagesWide := 1;
ExlApp.ActiveSheet.PageSetup.FitToPagesTall := False;

//设置页眉、页脚(即:页标题、页号)
ExlApp.ActiveSheet.PageSetup.RightFooter:='打印时间: '+'&D &T';
ExlApp.ActiveSheet.PageSetup.CenterFooter:='第&''&P&''页,共&''&N&''页';

//设置页边距:
ExlApp.ActiveSheet.PageSetup.TopMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.BottomMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.LeftMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.RightMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.HeaderMargin:=0.5/0.035;
ExlApp.ActiveSheet.PageSetup.FooterMargin:=0.5/0.035;

//设置页面对齐方式
ExlApp.ActiveSheet.PageSetup.CenterHorizontally:=True; //页面水平居中
// ExlApp.ActiveSheet.PageSetup.CenterVertically :=True; //页面垂直居中

//设置整体字体格式
ExlApp.Cells.Font.Name:='宋体'; //字体
ExlApp.Cells.Font.Size:=12; //字号
ExlApp.Cells.RowHeight:=16; //行高
ExlApp.Cells.VerticalAlignment:=2; //垂直方向对齐方式:居中
end;{CreatRepSheet}

procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String);
//用户自定义页眉、页脚(即:页标题、页号)
begin
ExlApp.ActiveSheet.PageSetup.LeftHeader:=H_Mess1;
ExlApp.ActiveSheet.PageSetup.CenterHeader:=H_Mess2;
ExlApp.ActiveSheet.PageSetup.RightHeader:=H_Mess3;
end;{SetAddMess}

procedure SetRepBody(x,ch:Integer;cw:Double;cf:String);
//设置整体各列数据格式
begin
ExlApp.ActiveSheet.Columns[x].ColumnWidth:=cw; //列宽
ExlApp.ActiveSheet.Columns[x].NumberFormat:=Cf; //单元格数据格式
ExlApp.ActiveSheet.Columns[x].HorizontalAlignment:=ch; //水平方向对齐方式
end;{SetRepBody}

procedure CreatTitle(TitleName:String;y:Integer);
{设置标题}
Var
RepSpace:String;
begin
CellMerge(1,1,1,y);
ExlApp.cells(1,1):=TitleName;
RepSpace:='A1'+':'+GetRepRange(1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.Font.Size:=22;
ExlApp.Selection.Font.Name:='黑体';
ExlApp.Selection.Font.Bold:=True;
ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中
ExlApp.Rows[1].RowHeight:=28;
end;{RepHead}

procedure CreatSubHead(SubTitle: Array of String);
{设置常规子表头}
Var
i,j:Integer;
begin
j:=0;
for i:=Low(SubTitle) to High(SubTitle) do
begin
Inc(j);
ExlApp.cells(2,j):=SubTitle[i];
end;
end;{CreatRepHead}

procedure SubHeadFormat(y,r:Integer);
{设置子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:='A2'+':'+GetRepRange(1+r,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=1 to r do
begin
ExlApp.Rows[1+n].RowHeight:=18;
SetRepLine(1+n,y);
end;{for}
end;{SubHeadFormat}

procedure DTSubHeadGS(x,y,r:Integer);
{设置动态子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:=GetRepRange(x,1)+':'+GetRepRange(x+r-1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=0 to r-1 do
begin
ExlApp.Rows[x+n].RowHeight:=18;
SetRepLine(x+n,y);
end;{for}
end;{DTSubHeadGS}

procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据
{写数据}
begin
if flag=1 then
ExlApp.cells(x,y):=StrToDate(RepData)
else
ExlApp.cells(x,y):=RepData;
end;{WriteDate}

procedure RepPageBreak(x,y,r:Integer); //X:分页处行数,Y:列数,R:子表头总共的行数
//分页、复制表头
Var
RepSpace:String;
n:Integer;
begin
ExlApp.ActiveSheet.Rows[x].PageBreak := 1;
RepSpace:='A1'+':'+GetRepRange(r+1,y);
ExlApp.ActiveSheet.Range[RepSpace].Copy;
RepSpace:='A'+IntToStr(x);
ExlApp.ActiveSheet.Range[RepSpace].PasteSpecial;
ExlApp.Rows[x].RowHeight:=28;
for n:=2 to r do
ExlApp.Rows[x+n].RowHeight:=18;
end;{RepPageBreak}

procedure RepSaveAs(FileName:String);
{保存为*.xls文件}
begin
try
ExlBook.saveas(FileName);
except
MessageDlg('不能访问文件,请关闭Microsoft Excel后再运行本程序!', mtError, [mbOk], 0);
end;
end;{RepSaveAs}

procedure RepPrivew(FileName:String);
{预览}
begin
RepCreat;
ExlApp.Visible :=True;
try
ExlApp.workBooks.Open(FileName);
ExlApp.Workbooks[1].WorkSheets[1].PrintPreview;
finally
ExlApp.Quit;
ExlApp:=Unassigned;
//ExlApp:='';
end;{try}
end;{RepPrivew}

procedure RepQuit;
{退出Excel}
begin
ExlBook.Close;
ExlApp.Quit; //退出Excel Application
ExlApp:=Unassigned; //释放VARIANT变量
end;{RepQuit}

procedure RepDestroy;
{非正常退出Excel}
begin
if Not VarIsEmpty(ExlApp) then
RepQuit;
end;{RepDestroy}

end.


续上!!!!
kaidu 2002-09-13
  • 打赏
  • 举报
回复
unit U_Report;

interface
uses Windows, SysUtils, Messages, Dialogs, Classes, Forms, OleCtnrs, OleServer, Excel97, ComObj;

Const ReNo=23; //一页显示的记录数
Const MAX=35; //最大的数组个数

Var
ExlApp:OleVariant;
ExlBook:OleVariant;

function GetRepRange(x,y:integer):String; //将(x,y)坐标形式改为Excel区域(A1:B1)形式
procedure CellMerge(x1,y1,x2,y2:integer); //合并指定单元格
procedure SetRepLine(x,y:Integer); //加边框线
procedure CellWrite(RepData: String; x,y:Integer); //单元格写数据
procedure CellFormat(x1,y1,x2,y2:integer); //指定单元格格式
procedure CellGS(x1,y1,x2,y2,f:integer); //灵活单元格格式

procedure RepCreat; //创建OLE对象(Excel Application与WorkBook)
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); //新建工作簿、页面设置
procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String); //设置附加信息
procedure SetRepBody(x,ch:Integer;cw:Double;cf:String); //设置整体各列数据格式
procedure CreatTitle(TitleName:String;y:Integer); //设置标题
procedure CreatSubHead(SubTitle: Array of String); //设置常规子表头
procedure SubHeadFormat(y,r:Integer); //设置子表头格式
procedure DTSubHeadGS(x,y,r:Integer); //设置动态子表头格式
procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据
procedure RepPageBreak(x,y,r:Integer); //分页、复制表头
procedure RepSaveAs(FileName:String); //保存为*.xls文件
procedure RepPrivew(FileName:String); //预览
procedure RepQuit; //退出Excel
procedure RepDestroy; //非正常退出Excel

implementation

function GetRepRange(x,y:integer):string;
var fX,fY:string;
begin
if y<=0 then
fX:='A';
if y<=26 then
fX := chr(64+y);
if y>26 then
fX:=chr(64+(y div 26))+chr(64+(y mod 26));

fY:=IntToStr(x);
Result:=fX+fY;
end;

procedure CellMerge(x1,y1,x2,y2:integer);
{合并指定单元格}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.Merge;
end;{CellMerge}

procedure CellFormat(x1,y1,x2,y2:integer);
{指定单元格格式}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.Font.Bold:=True;
ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中

end;{CellFormat}

procedure CellGS(x1,y1,x2,y2,f:integer);
{灵活单元格格式}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x1,y1)+':'+GetRepRange(x2,y2);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :='G/通用格式';
ExlApp.Selection.HorizontalAlignment:=f; //水平方向对齐方式:居中
end;{CellGS}

procedure SetRepLine(x,y:Integer);
{加边框线}
Var
RepSpace:String;
begin
RepSpace:=GetRepRange(x,1)+':'+GetRepRange(x,y);
ExlApp.ActiveSheet.Range[RepSpace].Borders.LineStyle:=xlContinuous;
end;{SetRepLine}

procedure CellWrite(RepData: String; x,y:Integer);
{单元格写数据}
begin
ExlApp.cells(x,y):=RepData;
end;{CellWrite}

procedure RepCreat;
{创建Excel对象}
begin
try
ExlApp:=CreateOLEObject('Excel.Application');
ExlBook:=CreateOLEObject('Excel.Sheet');
ExlApp.Visible :=False;// True;
ExlApp.DisplayAlerts := False;
except
MessageDlg('您的机器里未安装Microsoft Excel!', mtError, [mbOk], 0);
Exit;
end;{try}
end;{RepCreat}
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer);
{新建Excel工作簿、进行页面设置}
begin
{新建Excel工作簿}
if ExlApp.WorkBooks.Count<1 then
begin
ExlBook:=ExlApp.Workbooks.Add; //ExlBook:=ExlApp.WorkBooks[1].WorkSheets[1];
ExlApp.ActiveSheet.Name:=SheetName;
end;{if}

{进行页面设置}
simon77_shen 2002-09-13
  • 打赏
  • 举报
回复
Excel.Selection.Borders[9].ColorIndex:=1;

这句话你编译通过了吗?我这里一直是
Constant expression expected的错误提示?
simon77_shen 2002-09-13
  • 打赏
  • 举报
回复
谢谢,不过你的方法
ExlApp.ActiveSheet.Range[RepSpace].Borders.LineStyle:=xlContinuous;

它使所有的框都加上了实线,但是我想中间的对角线不要加实线,

于是我对照宏和前面给的方法用:
ExlApp.ActiveSheet.Range[RepSpace].Borders[11].LineStyle := xlNone;
ExlApp.ActiveSheet.Range[RepSpace].Borders[12].LineStyle := xlNone;

却怎么也编译不过去?请帮忙!!!!!
JonHua 2002-09-12
  • 打赏
  • 举报
回复
给你一个例程,自己去看吧。

procedure TfrmWaixStockmessage.ExportToExcel(Excel, Sheet: Variant; Srow: integer);
var
I:integer;
Range:String;
Year,Month,Day:Word;
begin
DecodeDate(Date,Year,Month,Day);
sheet.Columns[1].ColumnWidth:= 3.5;
sheet.Columns[2].ColumnWidth:=11;
Sheet.Columns[3].ColumnWidth:=14.5;
Sheet.Columns[4].ColumnWidth:=3.5;
Sheet.Columns[5].ColumnWidth:=8.5;
Sheet.Columns[6].ColumnWidth:=17;
Sheet.Columns[7].ColumnWidth:=12;
Range:= Format('%d:%d', [SRow, SRow]);
Sheet.Rows[Range].Select;
Excel.Selection.RowHeight:=25.5;
Range:=Format('%d:%d', [SRow + 1, SRow + 1]);
Sheet.Rows[Range].Select;
Excel.Selection.RowHeight:=25.5;
Range:= Format('%d:%d', [SRow + 4, SRow + 4]);
Sheet.Rows[Range].Select;
Excel.Selection.RowHeight:=39;
Range:= Format('%d:%d', [SRow + 5, SRow + 19]);
Sheet.Rows[Range].Select;
Excel.Selection.RowHeight:=19.5;
Range:= Format('A%d:G%d', [SRow + 4, SRow + 19]);
Sheet.Range[Range].Borders[9].ColorIndex:=1;
Sheet.Range[Range].Borders[9].Weight:=3;
Sheet.Range[Range].Borders[7].ColorIndex:=1;
Sheet.Range[Range].Borders[7].Weight:=3;
Sheet.Range[Range].Borders[8].ColorIndex:=1;
Sheet.Range[Range].Borders[8].Weight:=3;
Sheet.Range[Range].Borders[10].ColorIndex:=1;
Sheet.Range[Range].Borders[10].Weight:=3;
Sheet.Range[Range].Borders[12].ColorIndex:=1;
Sheet.Range[Range].Borders[12].Weight:=2;
Sheet.Range[Range].Borders[11].ColorIndex:=1;
Sheet.Range[Range].Borders[11].Weight:=2;
Range:= Format('B%d:G%d', [SRow + 20, SRow + 29]);
Sheet.Range[Range].Borders[9].ColorIndex:=1;
Sheet.Range[Range].Borders[9].Weight:=3;
Sheet.Range[Range].Borders[7].ColorIndex:=1;
Sheet.Range[Range].Borders[7].Weight:=3;
Sheet.Range[Range].Borders[8].ColorIndex:=1;
Sheet.Range[Range].Borders[8].Weight:=3;
Sheet.Range[Range].Borders[10].ColorIndex:=1;
Sheet.Range[Range].Borders[10].Weight:=3;
Range:=Format('A%d:G%d',[SRow, SRow + 29]);
Sheet.Range[Range].Font.Name:='楷体_GB2312';
Sheet.Range[Range].Font.Size:=12;
Sheet.Cells(SRow, 1):= '中川电气科技有限公司';
Sheet.Cells[SRow, 1].Font.Size:=20;
Sheet.Cells[SRow, 1].Font.Bold:=true;
Sheet.Cells(SRow, 6):=IntToStr(Year)+'年'+IntToStr(Month)+'月物资采购通知单';
Sheet.Cells[SRow, 6].Font.Name:='宋体';
Sheet.Cells(SRow + 1, 1):='物资供应部';
Sheet.Cells[SRow + 1, 1].Font.Size:=20;
Sheet.Cells[SRow + 1, 1].Font.Bold:=true;
Sheet.Cells(SRow + 1, 6):='通知单号:'+edt_TableNO.Text;
Sheet.Cells(SRow + 2, 1):='供 货';
Sheet.Cells(SRow + 2, 6):='计划单号:'+edt_StockTableNO.Text;
Sheet.Cells(SRow + 3, 1):='单 位:'+edt_Server.Text;
Sheet.Cells(SRow + 3, 6):='编 辑:'+edt_Weave.Text;
Range:= Format('A%d:G%d',[SRow + 4, SRow + 4]);
Sheet.Range[Range].HorizontalAlignment:=3;
Sheet.Range[Range].VerticalAlignment:=-4108;
Sheet.Cells(SRow + 4, 1):='序号';
Sheet.Cells[SRow + 4, 1].WrapText:=true;
Sheet.Cells(SRow + 4, 2):='物资名称';
Sheet.Cells(SRow + 4, 3):='型号规格';
Sheet.Cells(SRow + 4, 4):='单位';
Sheet.Cells[SRow + 4, 4].WrapText:=true;
Sheet.Cells(SRow + 4, 5):='数 量';
Sheet.Cells(SRow + 4, 6):='备 注';
Sheet.Cells(SRow + 4, 7):='交货时间';
Range:= Format('A%d:A%d', [SRow + 5, SRow + 19]);
Sheet.Range[Range].HorizontalAlignment:=3;
frmTData.Q_MX.First;
for i:=1 to 15 do
with frmTdata.Q_MX do
begin
Sheet.Cells(SRow + 4 + i, 1):=''''+ IntToStr(i);
Sheet.Cells(SRow + 4 + i, 2):=FieldByName('PartName').AsString;
Sheet.Cells(SRow + 4 + i, 3):=FieldByName('PartType').AsString;
Sheet.Cells(SRow + 4 + i, 4):=FieldByName('Unit').AsString;
Sheet.Cells(SRow + 4 + i, 5):=FieldByName('Number').AsString;
Sheet.Cells(SRow + 4 + i, 6):=FieldByName('Remark').AsString;
Sheet.Cells(SRow + 4 + i, 7):=FieldByName('Date').AsString;
Next;
if Eof then
Break;
end;
Sheet.Cells(SRow + 20, 1):='技术要求';
Range:=Format('A%d:A%d', [SRow + 20, SRow + 29]);
Sheet.Range[Range].Select;
Excel.Selection.Merge;
Excel.Selection.Borders[9].ColorIndex:=1;
Excel.Selection.Borders[9].Weight:=3;
Excel.Selection.Borders[7].ColorIndex:=1;
Excel.Selection.Borders[7].Weight:=3;
Excel.Selection.Borders[8].ColorIndex:=1;
Excel.Selection.Borders[8].Weight:=3;
Excel.Selection.Borders[10].ColorIndex:=1;
Excel.Selection.Borders[10].Weight:=3;
Sheet.Cells[SRow + 20, 1].WrapText:=true;
Sheet.Cells[SRow + 20, 1].HorizontalAlignment:=3;
Sheet.Cells[SRow + 20, 1].VerticalAlignment:=-4108;
Sheet.Cells(SRow + 30, 6):='通知日期:'+IntToStr(Year)+'年'+IntToStr(Month)+'月'+IntToStr(Day)+'日';
Sheet.Cells(SRow+30,1):='联系电话:0577-62789228';
Sheet.Cells[SRow +21,2].HorizontalAlignment:=-4131;
Sheet.Cells[SRow +21, 2].VerticalAlignment:=-4160;
Range:=Format('B%d:G%d', [SRow + 20, SRow + 29]);
Sheet.Range[Range].Select;
Sheet.Cells(SRow+21,2):=trim(edt_Require.Text);
Excel.Selection.Merge;
Sheet.Cells[SRow + 21, 2].WrapText:=true;
end;
procedure TfrmWaixStockmessage.btnExcelClick(Sender: TObject);
var
Excel,WorkBook,Sheet:OleVariant;
Year,Month,Day:Word;
begin
DecodeDate(Date,Year,Month,Day);
try
Excel:=CreateOleObject('Excel.Application');
Excel.WorkBooks.Add;
except
Application.MessageBox('请确认是否已安装了 Mircorsoft Excel !','提示',48);
abort;
end;
try
SaveDialog.Title:='将外协采购通知单保存为 Excel 文档';
SaveDialog.FileName:=IntToStr(Month)+'月外协采购通知单.xls';
if SaveDialog.Execute then
begin
WorkBook:=Excel.ActiveWorkBook;
Sheet:= WorkBook.ActiveSheet;
ExportToExcel(Excel, Sheet, 2);
WorkBook.SaveAs(SaveDialog.FileName);
end;
Excel.DisplayAlerts := false;
Excel.Quit;
finally
Excel:= Unassigned;
end;
end;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧