使用 Delphi 使用 OLE 自动化格式化单个 Excel 列的问题

发布于 2024-09-01 14:00:29 字数 2837 浏览 7 评论 0原文

我有一段代码,用于设置 Excel 中一系列单元格的格式。它在 Excel 2007 中工作正常,但当范围只有 1 列宽并且是 Excel 2003 而不是 2007 时,我会收到一条错误消息,指出我正在为边框的线条样式分配无效值。

** 有价值的东西,例如“xlInsideHorizo​​ntal”,我已将它们声明为具有正确值的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2024-09-08 14:00:29

同样,如果您指定了“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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文