Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
hi,

my task is, in attendance machine, when we swipe the card ,i got card id then that card id i need to place in excel,

first i need to check whether the excel file exists or not and if not create it, if exist add that card id into excel in next row . is it possible without database .

my requirement is no database, directly i need to write into excel.

previously i have developed as , first saved into database, then i get the data from database into excel for that i used following code , but how can i write directly into excel.any suggestions.....
C#
try
               {
                   using (MySqlConnection con = new MySqlConnection(ConnectionString))
                   {
                       String query;
                       System.Data.DataTable dt = new System.Data.DataTable();

                       string fromDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                       string toDate = dateTimePicker2.Value.ToString("yyyy-MM-dd");
                       if ((reportsmachine.Text == "") && (reportscustomer.Text == ""))
                       {
                           query = "select c.customer_id as 'Customer ID',c.father_name as 'Father Name',c.address as Address,c.phone_number as 'Phone Number',c.mobile_number as 'Mobile Number',c.id_proof as 'ID Proof',c.area as Area,c.ip_address as 'IP Address',c.mac_address as 'MAC Address',c.package_type as 'Package Type',c.name as Name,c.activation_date as 'Activation Date',c.status as Status,c.installation_cost as 'Installation Cost',c.totalamount_paid as 'Total Amount Paid',c.monthly_amount as 'Monthly Amount',c.lastpaid_date as 'Last Paid Date',c.lastpaid_amount as 'Last Paid Amount',c.nextpay_date as 'Next Pay Date',c.totaldue_amount as 'Total Due Amount',t.agent_id as 'Agent ID',t.token_number as 'Token Number',t.machine_id as 'Machine ID' from customer c INNER JOIN transaction t ON c.customer_id=t.customer_id  WHERE DATE(t.paid_date)BETWEEN '" + fromDate + "'AND '" + toDate + "' GROUP BY c.customer_id ";
                           da = new MySqlDataAdapter(query, con);
                           ds = new DataSet();
                           datasetvalue = da.Fill(ds);

                           datatablevalue = da.Fill(dt);


                       }
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                               object misValue = System.Reflection.Missing.Value;

                               Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);

                               Microsoft.Office.Interop.Excel.Worksheet worksheet;
                               worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

                               try
                               {

                                   worksheet.Cells[1, 1] = "Customer Name";
                                   worksheet.Cells[1, 2] = "father Name";
                                   worksheet.Cells[1, 3] = "Address";
                                   worksheet.Cells[1, 4] = "Phone Number";
                                   worksheet.Cells[1, 5] = "Mobile number";
                                   worksheet.Cells[1, 6] = "ID Proof";
                                   worksheet.Cells[1, 7] = "Area";
                                   worksheet.Cells[1, 8] = "IP Address";
                                   worksheet.Cells[1, 9] = "MAC Address";
                                   worksheet.Cells[1, 10] = "Package Type";
                                   worksheet.Cells[1, 11] = "User Name";
                                   worksheet.Cells[1, 12] = "Activation Rate";
                                   worksheet.Cells[1, 13] = "Status";
                                   worksheet.Cells[1, 14] = "Installation Cost";
                                   worksheet.Cells[1, 15] = "Total Amount Paid";
                                   worksheet.Cells[1, 16] = "Monthly Amount";
                                   worksheet.Cells[1, 17] = "Last Paid Date";
                                   worksheet.Cells[1, 18] = "Last Paid Amount";
                                   worksheet.Cells[1, 19] = "Next Payment Date";
                                   worksheet.Cells[1, 20] = "Total Due Amount";
                                   worksheet.Cells[1, 21] = "Agent ID";
                                   worksheet.Cells[1, 22] = "Receipt Number";
                                   worksheet.Cells[1, 23] = "Machine ID";

                                   int row = 1;
                                   for (int k = 1; k <= 23; k++)
                                   {
                                       worksheet.Cells[row, k].Interior.ColorIndex = 39;
                                   }


                                   string data = null;

                                   int i = 0;

                                   int j = 0;


                                   for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                                   {

                                       for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                                       {

                                           data = ds.Tables[0].Rows[i].ItemArray[j].ToString();

                                           ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[i + 2, j + 1]).Value2 = data;

                                       }

                                   }
                                   excelApp.DisplayAlerts = false;


                                   //excelApp.Visible = true;
                                   //String fname =@"C:\Example.xls";

                                   workbook.SaveAs(fname, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                                   workbook.Close(true, misValue, misValue);
                                   excelApp.Quit();
                                   releaseObject(worksheet);
                                   releaseObject(workbook);
                                   releaseObject(excelApp);
                                   System.Diagnostics.Process.Start(fname);
                               }
                               catch (Exception p)
                               {
                                   MessageBox.Show(p.StackTrace);
                               }

                               finally
                               {
                                   if (excelApp != null)
                                       releaseObject(excelApp);
                                   if (workbook != null)
                                       releaseObject(workbook);
                                   if (worksheet != null)
                                       releaseObject(worksheet);
                               }
Posted
Comments
Sanjay K. Gupta 2-May-14 3:25am    
Please store data in database, at least in Access or you can prefer NOSQL approach also. Storing data in database would be good for long running. After storing the data in database, you can simply create a report and export it in any format like .csv, excel or .pdf.

1 solution

you can create OLEDB connection OLEDB connection [^] to your excel file and work as database. for more information check below code project article
Read and Write Excel Documents Using OLEDB[^]
 
Share this answer
 
Comments
Member 10263519 2-May-14 7:10am    
when i swipe the card i need to write into excel, so it's working only for one card , for second card it's giving error at query create table [table] as "table already exists"

so how to know whether the table exists or not, if exists i need to write into another row
how to do
Member 10263519 3-May-14 3:08am    
thanq

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