Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi,

am developing attendance system, when i swipe the card , i will get card Id into text box, then i need to insert into excel in button click.

here is the code:
C#
private void button1_Click(object sender, EventArgs e)
       {
           if(txtCardID.Text !="")
           {
               newString = "";
               string connectionString = GetConnectionString();
               string s = Convert.ToString(Program.ConnectObj.Output);
               if (s.Length == 7)
               {

                   newString = s.PadLeft(10, '0');
               }
               else if (s.Length == 8)
               {

                   newString = s.PadLeft(10, '0');
               }
               else if (s.Length == 9)
               {

                   newString = s.PadLeft(10, '0');
               }

               using (OleDbConnection conn = new OleDbConnection(connectionString))
               {
                   conn.Open();
                   OleDbCommand cmd = new OleDbCommand();
                   cmd.Connection = conn;

                   cmd.CommandText = "CREATE TABLE [sheet1] (id INT);";
                   cmd.ExecuteNonQuery();

                   cmd.CommandText = "INSERT INTO [sheet1$](id) VALUES('" + newString + "');";
                   cmd.ExecuteNonQuery();
                   MessageBox.Show("Successfully Inserted");

                   conn.Close();
               }
               txtCardID.Text = "";
           }
           else
               MessageBox.Show("Please Swipe the Card");


       }

first time it's creating "sheet1" writes card id into that, but for next time it's giving Exception as "sheet1 already exists" how to solve it.

am using oledb connection.
C#
private string GetConnectionString()
       {
           Dictionary<string, string> props = new Dictionary<string, string>();

           // XLSX - Excel 2007, 2010, 2012, 2013
           props["Provider"] = "Microsoft.ACE.OLEDB.12.0;";
           props["Extended Properties"] = "Excel 12.0 XML";
           string appPath = Path.GetDirectoryName(Application.ExecutablePath);
           string fName = @appPath + "\\MyExcel.xlsx ";
            props["Data Source"] = fName;
          // props["Data Source"] = "C:\\MyExcel.xlsx";

           StringBuilder sb = new StringBuilder();

           foreach (KeyValuePair<string, string> prop in props)
           {
               sb.Append(prop.Key);
               sb.Append('=');
               sb.Append(prop.Value);
               sb.Append(';');
           }

           return sb.ToString();
       }
Posted
Updated 5-May-14 1:21am
v2
Comments
[no name] 5-May-14 7:25am    
Simple. Stop trying to create sheet 1 every time.
Maciej Los 5-May-14 7:50am    
My vitrual 5!

Hi,
Here you are creating table 'sheet1' every time you click on the button1,
you can validate if table exists or not to solve your problem.
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Sheet1]') AND type in (N'U'))
[YOUR SQL QUERY]

Please check the solutions and let me know if it helps.
Thanks,
Hitesh Varde
 
Share this answer
 
You should check if that particular worksheet already exists, if not then create else skip:
how-to-check-if-the-worksheet-already-exist-in-interop[^]
 
Share this answer
 
hello,


here you should checkout the query string ,and in query string insert Mode=Share Exclusive ;

it use to open the file if it already exist.

checkout the code whether you are creating the sheet1 everytime . take if {}else{} ,
if the file exist then open as Mode=Share Exclusive; otherwise create a sheet1;
 
Share this answer
 

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