2

我正在尝试将用作标题的单元格的值居中。这是我目前用来格式化这些单元格的代码。我正在使用带有 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();

    }
4

1 回答 1

13

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

workSheet_range.HorizontalAlignment =
     Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
于 2012-10-15T19:37:15.190 回答