如何合并和居中两个单元格的文本?

时间:2018-11-13 05:36:41

标签: c# excel-interop

我想使用互操作在Microsoft Excel中合并两个单元格和中心文本,我解决了水平合并的单元格,但是在垂直方向上未合并的单元格帮助我解决了这个问题

 private void button1_Click(object sender, EventArgs e)
    {
        String filetest = "C:\\com\\test.xlsx";
        if(File.Exists(filetest))
        {
            File.Delete(filetest);
        }

        Excel.Application oApp;
        Excel.Workbook oBook;
        Excel.Worksheet oSheet;

        oApp = new Excel.Application();
        oBook = oApp.Workbooks.Add();
        oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
        Excel.Range c1 = oSheet.Cells[2, 1];
        Excel.Range c2 = oSheet.Cells[2, 5];
        Excel.Range oRange = oSheet.get_Range(c1, c2);
        oRange.Merge(true);
        oRange.HorizontalAlignment= Excel.XlHAlign.xlHAlignCenter;
        //oRange.VerticalAlignment = XlVAlign.xlVAlignCenter;
        oSheet.Cells[3, 1] = "SL.No";
        oRange.Interior.ColorIndex = 36;
        oBook.SaveAs(filetest);
        oApp.Visible = true;
       // oBook.Close();
       // oApp.Quit();
        MessageBox.Show("File Created");
    }

0 个答案:

没有答案