使用 Delphi 使用 OLE 自动化格式化单个 Excel 列的问题
我有一段代码,用于设置 Excel 中一系列单元格的格式。它在 Excel 2007 中工作正常,但当范围只有 1 列宽并且是 Excel 2003 而不是 2007 时,我会收到一条错误消息,指出我正在为边框的线条样式分配无效值。
** 有价值的东西,例如“xlInsideHorizontal”,我已将它们声明为具有正确值的 CONSTANT。
请帮忙。
procedure formatCells(FRCELLROW, FRCELLCOL, TOCELLROW, TOCELLCOL: Integer;
TOPSTYLE, TOPCOLOUR, TOPWEIGHT,
BOTTOMSTYLE, BOTTOMCOLOUR, BOTTOMWEIGHT,
LEFTSTYLE, LEFTCOLOUR, LEFTWEIGHT,
RIGHTSTYLE, RIGHTCOLOUR, RIGHTWEIGHT: Integer;
INNERVSTYLE, INNERVCOLOUR, INNERVWEIGHT: Integer;
INNERHSTYLE, INNERHCOLOUR, INNERHWEIGHT: Integer;
HORIZONTALCELLALIGNMENT: Integer;
FontBold: Boolean;
NumberFormat: String
);
var
tmpRange: Variant;
begin
tmpRange := eclApp.range[eclApp.Cells[FRCELLROW, FRCELLCOL],
eclApp.Cells[TOCELLROW, TOCELLCOL]];
tmpRange.Borders[xlEdgeTop].LineStyle := TOPSTYLE;
if TOPSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeTop].ColorIndex := TOPCOLOUR;
tmpRange.Borders[xlEdgeTop].Weight := TOPWEIGHT;
end; //if
tmpRange.Borders[xlEdgeBottom].LineStyle := BOTTOMSTYLE;
if BOTTOMSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeBottom].ColorIndex := BOTTOMCOLOUR;
tmpRange.Borders[xlEdgeBottom].Weight := BOTTOMWEIGHT;
end; //if
tmpRange.Borders[xlEdgeLeft].LineStyle := LEFTSTYLE;
if LEFTSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeLeft].ColorIndex := LEFTCOLOUR;
tmpRange.Borders[xlEdgeLeft].Weight := LEFTWEIGHT;
end; //if
tmpRange.Borders[xlEdgeRight].LineStyle := RIGHTSTYLE;
if RIGHTSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeRight].ColorIndex := RIGHTCOLOUR;
tmpRange.Borders[xlEdgeRight].Weight := RIGHTWEIGHT;
end; //if
tmpRange.Borders[xlInsideVertical].LineStyle := INNERVSTYLE;
if INNERVSTYLE <> xlNone then begin
tmpRange.Borders[xlInsideVertical].ColorIndex := INNERVCOLOUR;
tmpRange.Borders[xlInsideVertical].Weight := INNERVWEIGHT;
end; //if
tmpRange.Borders[xlInsideHorizontal].LineStyle := INNERHSTYLE;
if INNERHSTYLE <> xlNone then begin
tmpRange.Borders[xlInsideHorizontal].ColorIndex := INNERHCOLOUR;
tmpRange.Borders[xlInsideHorizontal].Weight := INNERHWEIGHT;
end; //if
tmpRange.HorizontalAlignment := HORIZONTALCELLALIGNMENT;
tmpRange.Font.Bold := FontBold;
tmpRange.NumberFormat := NumberFormat;
end; //
I have piece of code which I use to format a range of cells in Excel. It works fine in Excel 2007 but when the range is only 1 column wide and it is Excel 2003 instead of 2007, I'll get an error saying the I am assigning invalid value for a border's line style.
** valuables such as "xlInsideHorizontal", I have declared them as CONSTANT with the proper values.
Please help.
procedure formatCells(FRCELLROW, FRCELLCOL, TOCELLROW, TOCELLCOL: Integer;
TOPSTYLE, TOPCOLOUR, TOPWEIGHT,
BOTTOMSTYLE, BOTTOMCOLOUR, BOTTOMWEIGHT,
LEFTSTYLE, LEFTCOLOUR, LEFTWEIGHT,
RIGHTSTYLE, RIGHTCOLOUR, RIGHTWEIGHT: Integer;
INNERVSTYLE, INNERVCOLOUR, INNERVWEIGHT: Integer;
INNERHSTYLE, INNERHCOLOUR, INNERHWEIGHT: Integer;
HORIZONTALCELLALIGNMENT: Integer;
FontBold: Boolean;
NumberFormat: String
);
var
tmpRange: Variant;
begin
tmpRange := eclApp.range[eclApp.Cells[FRCELLROW, FRCELLCOL],
eclApp.Cells[TOCELLROW, TOCELLCOL]];
tmpRange.Borders[xlEdgeTop].LineStyle := TOPSTYLE;
if TOPSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeTop].ColorIndex := TOPCOLOUR;
tmpRange.Borders[xlEdgeTop].Weight := TOPWEIGHT;
end; //if
tmpRange.Borders[xlEdgeBottom].LineStyle := BOTTOMSTYLE;
if BOTTOMSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeBottom].ColorIndex := BOTTOMCOLOUR;
tmpRange.Borders[xlEdgeBottom].Weight := BOTTOMWEIGHT;
end; //if
tmpRange.Borders[xlEdgeLeft].LineStyle := LEFTSTYLE;
if LEFTSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeLeft].ColorIndex := LEFTCOLOUR;
tmpRange.Borders[xlEdgeLeft].Weight := LEFTWEIGHT;
end; //if
tmpRange.Borders[xlEdgeRight].LineStyle := RIGHTSTYLE;
if RIGHTSTYLE <> xlNone then begin
tmpRange.Borders[xlEdgeRight].ColorIndex := RIGHTCOLOUR;
tmpRange.Borders[xlEdgeRight].Weight := RIGHTWEIGHT;
end; //if
tmpRange.Borders[xlInsideVertical].LineStyle := INNERVSTYLE;
if INNERVSTYLE <> xlNone then begin
tmpRange.Borders[xlInsideVertical].ColorIndex := INNERVCOLOUR;
tmpRange.Borders[xlInsideVertical].Weight := INNERVWEIGHT;
end; //if
tmpRange.Borders[xlInsideHorizontal].LineStyle := INNERHSTYLE;
if INNERHSTYLE <> xlNone then begin
tmpRange.Borders[xlInsideHorizontal].ColorIndex := INNERHCOLOUR;
tmpRange.Borders[xlInsideHorizontal].Weight := INNERHWEIGHT;
end; //if
tmpRange.HorizontalAlignment := HORIZONTALCELLALIGNMENT;
tmpRange.Font.Bold := FontBold;
tmpRange.NumberFormat := NumberFormat;
end; //
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
同样,如果您指定了“1”行高,您也会得到相同的错误。内部样式适用于相邻单元格之间的线条。 “1”列宽范围没有水平相邻的单元格,因此没有垂直内线。 Excel 2007 可能会忽略无效值,而 Excel 2003 则会引发错误。
在将值传递给“formatCells”过程之前测试范围的列数和行数,如果在其中任何一个过程中遇到“1”,则传递“xlNone”(-4142)代替“INNERVSTYLE,INNERVCOLOUR,内在重量”或“内在风格、内在色彩、内在重量”。
Likewise, if you have specified a "1" row height, you'd get the same error. Inner styles are for lines between adjacent cells. A "1" column width range have no horizontally adjacent cells, hence no vertical inner lines. Excel 2007 is probably ignoring the invalid value, whereas Excel 2003 is throwing an error.
Test for the number of columns and number of rows for ranges before passing values to your "formatCells" procedure, and if you encounter a "1" in any of these pass "xlNone" (-4142) in place of "INNERVSTYLE, INNERVCOLOUR, INNERVWEIGHT" or "INNERHSTYLE, INNERHCOLOUR, INNERHWEIGHT".