How can I insert a new row in excel after a specific value written vertically on merged cells in column A, using C#?

StackOverflow https://stackoverflow.com/questions/21877591

Question

So I have an excel file with multiple columns and rows, and column A is divided into 4 categories. Category one witch spreads over 3 merged rows vertically, category 2 witch spreads over 54 rows vertically and so on. The name of the category is also written vertically. I also have a form with a comboBox from witch I can select one of those categories and using that selection I want to compare it to the category from excel and be able to insert a new row at the end of that specific category.

Was it helpful?

Solution

Figured out the solution and it's something like this:

public static void WriteToExcelFolderAccess(Employee empl)
{
    try
    {
        string[] cellVal = new string[lastRowFold];
        for (int k = 1; k <= lastRowFold; k++)
        {
            string str = mySheetFold.UsedRange.Cells[k, 1].Text;
            cellVal[k - 1] = str;
        }

        var temp = new List<string>();
        foreach (string val in cellVal)
        {
            if (!string.IsNullOrEmpty(val))
                temp.Add(val);
        }
        cellVal = temp.ToArray();

        for (int i = 3; i <= lastRowFold; i++)
        {
            if (mySheetFold.Cells[i, 1].Text == cellVal[empl.CompanyIndex].ToString())
            {
                MessageBox.Show("found it");
                int found = i;

                empl.CompanyIndex += 1;
                for (int j = i; j <= lastRowFold; j++)
                {
                    if (empl.CompanyIndex < cellVal.Length)
                    {
                        if (mySheetFold.Cells[j, 1].Text == cellVal[empl.CompanyIndex].ToString())
                        {
                            MessageBox.Show("insert row above");
                            excel.Range rng = mySheetFold.Cells[j, 1];
                            excel.Range row = rng.EntireRow;
                            row.Insert(excel.XlInsertShiftDirection.xlShiftDown, System.Type.Missing);
                            mySheetFold.Range[mySheetFold.Cells[found, 1], mySheetFold.Cells[j, 1]].Merge();
                            break;
                        }
                    }
                    else
                    {
                        MessageBox.Show("insert row below");
                        excel.Range rng = mySheetFold.Cells[j, 1];
                        excel.Range row = rng.EntireRow;
                        row.Insert(excel.XlInsertShiftDirection.xlShiftDown, System.Type.Missing);
                        mySheetFold.Range[mySheetFold.Cells[found, 1], mySheetFold.Cells[j+1, 1]].Merge();
                        break;
                    }
                }
                break;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        folderAccessList.Save();
        folderAccessList.Close();
    }
}

The MessageBox()'es used are just for testing purposes. My problem now is how can I insert a row after the last row of data therefor keeping the cell's formatting. The following piece of code is the one that needs modifying in order to insert bellow.

else
{
     MessageBox.Show("insert row below");
     excel.Range rng = mySheetFold.Cells[j, 1];
     excel.Range row = rng.EntireRow;
     row.Insert(excel.XlInsertShiftDirection.xlShiftDown, System.Type.Missing);
     mySheetFold.Range[mySheetFold.Cells[found, 1], mySheetFold.Cells[j+1, 1]].Merge();
     break;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top