使用Excel Interop w / C#中心Excel单元格内容

时间:2022-09-28 17:14:02

I am trying to center the values of the cells that will be used as headers. This is the code I am currently using to format these cells already. I am using the Excel Interop with C#. How do I center the values in this range of cells.

我试图将将用作标题的单元格的值居中。这是我目前用于格式化这些单元格的代码。我正在使用Excel Interop和C#。如何将此单元格范围中的值居中。

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

    }

1 个解决方案

#1


11  

the ContentAlignment enum is from System.Drawing and does not apply to Excel ranges. try

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

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

#1


11  

the ContentAlignment enum is from System.Drawing and does not apply to Excel ranges. try

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

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