【问题标题】:Center Excel Cell Content using Excel Interop w/ C#使用带有 C# 的 Excel 互操作将 Excel 单元格内容居中
【发布时间】:2012-10-15 19:23:03
【问题描述】:

我正在尝试将用作标题的单元格的值居中。这是我目前用来格式化这些单元格的代码。我正在使用带有 C# 的 Excel 互操作。如何在此单元格范围内将值居中。

public void createHeaders(int row, int col, string htext, string cell1,
    string cell2, int mergeColumns, string b, bool font, int size, string
    fcolor)
    {
        worksheet.Cells[row, col] = htext;
        workSheet_range = worksheet.get_Range(cell1, cell2);
        workSheet_range.Merge(mergeColumns);
        switch (b)
        {
            case "BLUE":
                workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();
                break;
            case "GAINSBORO":
                workSheet_range.Interior.Color =
        System.Drawing.Color.Gainsboro.ToArgb();
                break;
            //case "Turquoise":
               // workSheet_range.Interior.Color =
        //System.Drawing.Color.Turquoise.ToArgb();
                //break;
            case "PeachPuff":
                workSheet_range.Interior.Color =
        System.Drawing.Color.PeachPuff.ToArgb();
                break;
            default:
                //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                break;
        }

        //workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
        //workSheet_range.Borders = null;
        workSheet_range.Font.Bold = font;
        workSheet_range.ColumnWidth = size;
        workSheet_range.Font.Color = System.Drawing.Color.FloralWhite.ToArgb();

    }

【问题讨论】:

  • 如何让这些单元格区域的值居中?
  • 从范围内的第一个单元格中读取值。

标签: c# excel excel-interop


【解决方案1】:

ContentAlignment 枚举来自 System.Drawing,不适用于 Excel 范围。试试

workSheet_range.HorizontalAlignment =
     Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

【讨论】:

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