我需要使用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);
}
}
答案 0 :(得分: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);
答案 1 :(得分: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 Interop的语法。
答案 2 :(得分:0)
如果您只是想在您创建的表格周围放置边框,那么在构建整个表格后尝试:
xlWorksheet.Cells ["19", "I"].CurrentRegion.BorderAround(xlContinuous, xlMedium, xlColorIndexAutomatic);
您的代码目前正在尝试在整个工作表周围添加边框
答案 3 :(得分: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”启用右侧边框