0

我需要使用 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);
                }
            }
4

4 回答 4

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);

于 2012-10-23T02:36:54.550 回答
0

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

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 更新:在评论中进行了一些讨论并更新了原始帖子后,问题变成了 IntelliSense,而不是 Excel 互操作的语法。

于 2012-10-19T06:55:19.693 回答
0

如果您只是想在您创建的表格周围放置一个边框,一旦您构建了整个表格,请尝试:

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

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

于 2012-10-19T10:52:14.447 回答
0

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

  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”为右侧启用了边框

于 2017-12-13T04:27:44.843 回答