【问题标题】:Format cell in an excel sheet using interop使用互操作格式化Excel工作表中的单元格
【发布时间】:2017-12-13 12:27:44
【问题描述】:

我需要使用 c# 语言为自动颜色的 Excel 单元格添加边框。下面给出的是我使用的编码。但它不会为单元格添加任何边框。你能告诉我我在这里做错了什么吗:

当我尝试为单元格指定边框样式时,我没有获得任何边框设计功能:

 Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlApp.Visible = false;
            xlWorkBook = xlApp.Workbooks.Open(textBox1.Text, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            int i = 0;
            int j = 0;

            for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                {
                    DataGridViewCell cell = dataGridView1[j, i];
                    xlWorkSheet.Cells["19", "I"] = "Availablility";                         
                    xlWorkSheet.Cells[i + 20, j + 9] = cell.Value;                                                               
                    xlWorkSheet.Cells.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
                }
            }

【问题讨论】:

  • 我不确定您的颜色属性是否正常。如果删除最后一个参数,它会起作用吗?此外,该文档说“您必须指定 ColorIndex 或 Color,但不能同时指定两者。您可以指定 LineStyle 或 Weight,但不能同时指定两者。如果您不指定任何一个参数,Microsoft Excel 将使用默认的线条样式和粗细”
  • @John 如果我删除任何参数,它会抛出一个名为“方法“BorderAround”没有重载需要“3”个参数”的错误。我想我需要包括所有四个参数,因为如果我删除所有它会抛出一个错误,称为“方法“BorderAround”没有重载需要'0'参数”

标签: c# excel


【解决方案1】:

试试这个。不要发布整个代码

Excel.Worksheet oSheet;
Excel.Range oRange;
oRange = oSheet.get_Range("Q3", "Q40"); 

oRange.Font.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
oRange.Cells.Borders.Color=System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);

【讨论】:

  • 嗨,感谢您的回复。但它给出了一个运行时错误,称为“无法通过 IDispatch 调用具有 System.Color 参数或返回类型的方法”。而且我无法定义一个范围,因为它根据输入的 excel 表而变化
  • @hansikaattanayake 查看编辑。我认为有办法动态传递范围
【解决方案2】:

如果您只是想在已创建的表格周围设置边框,请在构建整个表格后尝试:

xlWorksheet.Cells ["19", "I"].CurrentRegion.BorderAround(xlContinuous, xlMedium, xlColorIndexAutomatic);

您的代码当前正在尝试在整个工作表周围设置边框

【讨论】:

  • 嗨,感谢您的回复。如果我指定一个单元格,我不会获得 CurrentRegion 功能。我在查询中附上了屏幕截图。
  • 是的。然后它输出给定的错误消息“'object'不包含'CurrentRegion'的定义,并且找不到接受'object'类型的第一个参数的扩展方法'CurrentRegion'(您是否缺少使用指令或程序集引用?)"
【解决方案3】:

我认为您需要先选择单元格。以下代码适用于我:

using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelInteropDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application excel = new Excel.Application();
            excel.Workbooks.Add();
            excel.Visible = true;
            excel.ActiveWorkbook.ActiveSheet.Range("a1").EntireRow.Select();
            excel.Selection.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, 
                Excel.XlColorIndex.xlColorIndexAutomatic, 
                System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189)));
        }
    }
}

2012 年 10 月 23 日更新: 在 cmets 中进行了一些讨论并更新了原始帖子后,问题变成了 IntelliSense,而不是 Excel 互操作的语法。

【讨论】:

  • 您打算设置所有单元格的边框样式,还是只设置您在 for 循环中创建的单元格?如果所有单元格,应该很容易(see here)如果您只想设置特定的单元格,也许您可​​以尝试这样的操作:xlWorkSheet.Cells[i + 20, j + 9].BorderAround(...);
  • 你确定吗?以下代码在我的机器上运行没有任何问题(我使用的是 Excel 2010):xlWorkSheet.Cells[1,1].BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189)));
  • 我认为您不能使用 xlWorkSheet.Cells.BorderAround 因为它无法确定您要设置的单元格。您需要明确指定一个单元格或一个单元格范围。
  • 是的。但是当我尝试指定一个单元格时,它不提供任何功能。我附上了截图
  • 请忽略这一点(我们正在处理动态调用,IntelliSense 可能在这里不起作用)。只需输入代码,您会发现它有效。如果你真的关心 IntelliSense,试试这个:(xlWorkSheet.Cells[1,1] as Excel.Range).BorderAround2(....);
【解决方案4】:

由于您只需要添加边框,因此请尝试以下代码。这个对我有用。

  private void AllBorders(Excel.Borders borders)
    {
           borders.Color = System.Drawing.Color.Black;            
    }

//Call the function now. 

AllBorders(activeWorkSheet.get_Range(Cell1: "A1",  Cell2: "lastcellname").Cells.Borders);

如果您只想设置边框 LEFT/RIGHT/TOP/Bottom,请使用以下代码。

borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlMedium;            
            borders[Excel.XlBordersIndex.xlEdgeRight].Color = System.Drawing.Color.Black;

根据您的要求设置边缘我仅使用“xlEdgeRight”为右侧启用了边框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多