簡體   English   中英

使用Excel Interop w / C#中心Excel單元格內容

[英]Center Excel Cell Content using Excel Interop w/ C#

我試圖將將用作標題的單元格的值居中。 這是我目前用於格式化這些單元格的代碼。 我正在使用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();

    }

ContentAlignment枚舉來自System.Drawing,不適用於Excel范圍。 嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM