-2

Using this table as an example:

Excel Example Table

I need to code in C# a way to merge rows 2 and 3 (so that all data is merged into 1 row because this excel document has metrics on diff rows like the example) and delete any row in excel that contains the word 'N/A' in column B, how would I go about doing that in C#? I need to code in C# to alter excel files

  • Are you trying to merge row 1 and 2, or row 2 and 3? Or are you asking more about how to even start opening an Excel file in c#? – Ibrennan208 May 04 '22 at 19:24
  • SO is not a code writing service, post what you have tried, along with any errors or exceptions you might have. – mxmissile May 04 '22 at 21:07

2 Answers2

0
  1. First create a model that contains each property which are now in the top row of your excel file
  2. Loop through the rows and within that loop through the columns
  3. Within the loop check whether the item (row/column combination) contains a value which does not equal 'N/A' and if so set the property corresponding to the column with the value of the item

For code I would suggest the following question: Modify excel cell

Kai
  • 732
  • 1
  • 7
  • 23
-2

It's not entirely clear what you want from your question. Are you only trying to merge row 1 and 2 so that your row looks like:

|ID1|NameShirt|Size|Qty1|Price|?

This should be achievable using SQL and C#.

Excel files can actually be treated like a database, and Microsoft has some database drivers made specifically for Excel, as seen here.

You'll have to make some sort of SQL or OLEDB connection, like so:

OleDbConnection con = new OleDbConnection(
    "provider=Microsoft.Jet.OLEDB.4.0;data source="%%ExcelFilePath%%";Extended Properties=Excel 8.0;");

And then query that connection using SQL syntax to get record values or to update them, like so:

 using (OleDbConnection connection = new OleDbConnection(con))
        {
            connection.Open();
            try
            {

                string sqlCommand = "Your SQL Syntax to get the rows or to update them";
                DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                OleDbCommand cmd = new OleDbCommand(sqlCommand, con);

                cmd.ExecuteNonQuery(); //or cmd.ExecuteQuery()

                // this is where you would also be able to extract rows 
                //from the excel file and modify the information

                con.Close();


            }
            catch (Exception ex) { }
        }

Another option you can use is referencing the Microsoft Interop libraries to directly open an excel file and manage it, but be warned the interop library doesn't always work well if the application is supposed to be hidden from the user because Office applications have modal popups that will freeze your application if a user cannot dismiss them.

Once you pick your method of Excel manipulation, then you can get a list of rows/columns and iterate through them, updating the information in accordance with your conditions.

Ibrennan208
  • 1,345
  • 3
  • 14
  • 31
  • Oh sorry my bad, I edited my post to say 2 and 3 instead! so I'd want to have the rows 2 and 3's data merged into 1 line using C# I'm also coding this in a script that gets run so I wouldn't be able to use SQL, only C# – user3033661 May 04 '22 at 20:22
  • @user3033661 The SQL you would be using would actually be in c#. I've update my answer to reflect that. – Ibrennan208 May 04 '22 at 22:13