none
Merge Column Cell using C# Interop Excel RRS feed

  • Question

  • Below code, I had done column is not merge help me to solve this problem

    private void button2_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[5, 1];
                Excel.Range oRange = oSheet.get_Range(c1, c2);
                oRange.Merge(true);
                oRange.VerticalAlignment = XlVAlign.xlVAlignCenter;
                oSheet.Cells[4, 1] = "SL.No";

                oBook.SaveAs(filetest);
                oApp.Visible = true;
                // oBook.Close();
                // oApp.Quit();
                MessageBox.Show("File Created");
            }

                
    Tuesday, November 13, 2018 10:53 AM

All replies

  • Hi Harish,

    Try the following code:

    private void button2_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[5, 1];
                Excel.Range oRange = oSheet.get_Range(c1, c2);
                oRange.Merge(Missing.Value);
                oRange.VerticalAlignment = XlVAlign.xlVAlignCenter;
                oSheet.Cells[2, 1] = "SL.No";
    
                oBook.SaveAs(filetest);
                oApp.Visible = true;
                // oBook.Close();
                // oApp.Quit();
                MessageBox.Show("File Created");
            }

    This modified code was successfully run on my machine.

    You could refer to the following link:

    Merge or UnMerge Cells in a Worksheet

    Best Regards,

    Bruce

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, November 14, 2018 10:02 AM
    Moderator
  • Hi Harish,

    Have you solved your problem yet?

    If you have any question, or update, please feel free to let me know.

    I wish you a happy life!

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, November 16, 2018 9:37 AM
    Moderator
  • Hi Harish,

    Have you solved your problem?

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, November 30, 2018 2:56 AM
    Moderator
  • Hi Harish,

    This thread has been around for a long time. Please remember to mark the replies as answers if they

    helped. Please help us close the thread.

    Thank you for understanding! If you have any question, or update, please feel free to let me know.

    I wish you a happy life!

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Wednesday, December 5, 2018 2:32 AM
    Moderator