如何使文本位于Excel中该单元格的中间?

时间:2013-12-15 17:36:09

标签: c# excel

我正在使用标准库

using Excel = Microsoft.Office.Interop.Excel;

这就是我创建Excel的方式,只是代码的一小部分:

//Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel._Application xlApp = new Excel.Application();

xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//add data 
xlWorkSheet.Cells[1, 1] = "";
xlWorkSheet.Cells[1, 2] = "Student1";
xlWorkSheet.Cells[1, 3] = "Student2";
xlWorkSheet.Cells[1, 4] = "Student3";

如何让单元格中的文本位于中间?

enter image description here

2 个答案:

答案 0 :(得分:5)

为此范围设置如下:

Range("B2").Style.HorizontalAlignment = xlHAlignCenter

垂直对齐同样可用:

Range("B2").Style.VerticalAlignment = xlVAlignCenter

答案 1 :(得分:2)

 Microsoft.Office.Interop.Excel.Range RangeYour= workSheet1.Range["B3:I3"];
          //  rngcompphon.Merge();
          //  rngcompphon.ShrinkToFit = true;
            rngcompphon.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            rngcompphon.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
           // rngcompphon.Font.Bold = true;