Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
Can anyone help me, i am struck in one thing, i want to delete all the rows of a excel sheet and again repopulate it with data, I tried taking data in a dataset and then removing all the empty rows and then again populate the excel sheet with it but whats happening is, it inserts at the end of the old data.

C#
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\SCR.xls';Extended Properties='Excel 12.0;HDR=YES;'");                        
                        OleDbCommand cmd = new OleDbCommand();
                        DataTable records = new DataTable();
                        cmd.Connection = conn;
                        conn.Open();                        
                        cmd.CommandText = "UPDATE [Sheet3$] SET Geo = NULL WHERE Geo IS NOT NULL";                        
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "UPDATE [Sheet3$] SET Area = NULL WHERE Area IS NOT NULL";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "UPDATE [Sheet3$] SET Region = NULL WHERE Region IS NOT NULL";
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "UPDATE [Sheet3$] SET AverageLevel = NULL WHERE AverageLevel IS NOT NULL";
                        cmd.ExecuteNonQuery();

                        foreach (DataRow dtr in dsSCR.Tables[0].Rows)
                        {
                            cmd.CommandText = "INSERT INTO [Sheet3$] values( '" + dtr["Column1"].ToString() + "','" + dtr["EntityName"].ToString() + "','" + dtr["ManagerName"].ToString() + "','" + dtr["AverageLevel"].ToString() + "')";
                            cmd.ExecuteNonQuery();
                        }

                         //For deleting all the null rows
                        DataSet dataset = new DataSet();    	  
                        OleDbDataAdapter adapter = new OleDbDataAdapter();
	                    adapter.SelectCommand = new OleDbCommand("Select * from [Sheet3$]", conn);
	                    adapter.Fill(dataset);

                        foreach (DataRow dtRow in dataset.Tables[0].Rows)
                        {
                            if (dtRow["Geo"].ToString() == null || dtRow["Geo"].ToString() == "" && dtRow["Area"].ToString() == null || dtRow["Area"].ToString() == "" && dtRow["Region"].ToString() == null || dtRow["Region"].ToString() == "" && dtRow["AverageLevel"].ToString() == null || dtRow["AverageLevel"].ToString() == "")
                            {
                                dtRow.Delete();
                            }
                        }
	                    //DataRow dtRowDelete = dataset.Tables["Sheet3$"].Rows[0];
                        
	                    dataset.Tables[0].AcceptChanges();	 
	                    adapter.Update(dataset.Tables[0]);
                        foreach (DataRow dtr in dataset.Tables[0].Rows)
                        {
                            cmd.CommandText = "INSERT INTO [Sheet3$] values( '" + dtr["Geo"].ToString() + "','" + dtr["Area"].ToString() + "','" + dtr["Region"].ToString() + "','" + dtr["AverageLevel"].ToString() + "')";
                            cmd.ExecuteNonQuery();                            
                        }
                        cmd.Dispose();
                        adapter.Dispose();
                        conn.Close();


I have tried every possible way but i always gets this error:

C#
ex = {"Microsoft Office Excel cannot access the file 'C:\\SCR.xls'. There are several possible reasons:\n\n• The file name or path does not exist.\n• The file is being used by another program.\n• The workbook you are trying to save has the same name as a current...
Posted
Updated 5-Oct-11 5:21am
v4

1 solution

Why don't you delete the existing one and re-create it with the new data. Since you need to clear all the rows, that is much effective seems.
 
Share this answer
 
Comments
[no name] 5-Oct-11 16:02pm    
i can't as this sheet will be referenced with another table for creating charts, by the way can you tell me how to do the same?
CodingLover 5-Oct-11 21:03pm    
Then you have to look other handles moving with the file. The error "The file is being used by another program", better to have a look at. Is that you are reading and writing into the file in synchronously or asynchronously?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900