Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am stuck for many days at this problem in my app...

plz can anyone help me..?

I have an application where a user enters some data in a form1... that data should be displayed on the form2 as well as on excel sheet[same data as form2]..I am able to get the display on form 2.. but not on excel...

Here is my code.. I'm giving 2 methods here...one is for form 2 display[seereport method] and the other is for sheet display[generatereport method]

plz tell me where's the prob.. the first part of both methods is almost identical...
C#
public void SeeReport()
{
                //To display the final report
                for (int i = 0; i < NonRepTasks.Count; i++)
                {
                    Listview1.Items.Add(new ListViewItem(new string[] { NonRepTasks[i],
                                                FinalTime[i], compl[i], status1[i] }));         }
                return FinalTime;
}
          
public void GenerateReport()
        {
            
            
               
                string Connection = string.Format("Data Source='" + docpath +
               @"\Report{0:yyyy_MM_dd}.xlsx';Provider=Microsoft.ACE.OLEDB.12.0; 
                      Extended Properties=Excel 12.0", DateTime.Now.Date);
               
                //for inserting data

                OleDbCommand myCommand = new OleDbCommand();
                string sql1 = null;
                OleDbConnection myConnection = new OleDbConnection(Connection);
                myConnection.Open();
                myCommand.Connection = myConnection;

                //For clearing the sheet data before inserting new data.

                OleDbCommand myCommand1 = new OleDbCommand();
                string sql = null;
                myCommand1.Connection = myConnection;              
                sql = "UPDATE [Sheet1$] set [Task]=null,[Time]=null,[Completion]=null,[Status]=null";
                myCommand1.CommandText = sql;
                myCommand1.ExecuteNonQuery();
             
                for (int i = 0; i < NonRepTasks.Count; i++)
                {

                    sql1 = "Insert into [Sheet1$] values( '" + NonRepTasks[i] + "','"
                   + FinalTime[i] + "','" + compl[i] + "','" + status1[i] + "')";
                    myCommand.CommandText = sql1;
                    myCommand.ExecuteNonQuery();
                }            
                myConnection.Close();

                Process PR = new Process();
                string Filename = string.Format(docpath + @"\Report   {0:yyyy_MM_dd}.xlsx",
                                  DateTime.Now.Date);
                PR.StartInfo.FileName = Filename;
                PR.Start();
                Thread.Sleep(15000);
                Process[] RunningPro = Process.GetProcesses();
                foreach (Process p1 in RunningPro)
                {
                    string s;
                    s = p1.ProcessName;
                    s = s.ToLower();

                    if (s.CompareTo("excel") == 0)
                    {
                        p1.Kill();
                    }
                }
            }
Posted
Updated 23-Aug-11 10:41am
v3

I guess data didn't insert on the excell sheet. For my case i am doing like that,

C#
System.Data.OleDb.OleDbConnection MyConnection ;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                sql = "Insert into [Sheet1$] ([id],[name]) values ('1','e')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();


And to Open the Excell sheet you might use Workbooks.Open Method

C#
Workbook Open(
	[In] string Filename, 
	[In, Optional] object UpdateLinks, 
	[In, Optional] object ReadOnly, 
	[In, Optional] object Format, 
	[In, Optional] object Password, 
	[In, Optional] object WriteResPassword, 
	[In, Optional] object IgnoreReadOnlyRecommended, 
	[In, Optional] object Origin, 
	[In, Optional] object Delimiter, 
	[In, Optional] object Editable, 
	[In, Optional] object Notify, 
	[In, Optional] object Converter, 
	[In, Optional] object AddToMru, 
	[In, Optional] object Local, 
	[In, Optional] object CorruptLoad
);


This something like this,

C#
Application excel = new Application();
Workbook workbk = excel.Workbooks.Open(......);


Lemme know if it is helpful to you or if you need any assistance for this.

Thanks,
Rashim
 
Share this answer
 
v2
Comments
AC777 25-Aug-11 0:52am    
Thanks...but when I do it d way u suggested...I mean when i write sql query as

sql1 = "Insert into [Sheet1$](Task,Time,Completion,Status) values( '" + NonRepTasks[i] + "','"
+ FinalTime[i] + "','" + compl[i] + "','" + status1[i] + "')";

it gives me an error that there is a syntax error in insert statement.
Md. Rashim Uddin 25-Aug-11 2:19am    
Please Use this string

sql1 = "Insert into [Sheet1$] ([Task],[Time],[Completion],[Status]) values ( '" + NonRepTasks[i] + "','"+ FinalTime[i] + "','" + compl[i] + "','" + status1[i] + "')";


I have tested it and working in my site. Hope will work on your site.
Md. Rashim Uddin 25-Aug-11 2:21am    
I have updated my Solution. Please have a look on that.
Md. Rashim Uddin 25-Aug-11 4:41am    
Is it Working for you. Please lemme know if not. I am doing it here for you. So Please lemme know if it is not working or if you need any assistance.

And of course Please don't forget to mark it as answer if it solves your problems
Md. Rashim Uddin 26-Aug-11 7:30am    
Is it solve your problems??

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