Click here to Skip to main content
15,896,414 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,

I want to import system event log data to excel file every 1 hour automatically using c#. Could anyone help me how to get? Is there any code ? or Is there any methods to do this? Please give your suggestions.

Thanks
Guna
Posted
Comments
Sinisa Hajnal 5-Mar-15 2:02am    
There is an EventLog class in System.Diagnostic. And there is excel class in Microsoft.Interop namespace. Combine the two to transfer events you need to excel.

Good luck.
Sergey Alexandrovich Kryukov 5-Mar-15 2:08am    
Log in Excel would be such a lame...
—SA
What have you tried and where is the issue exactly?

1 solution

IF YOU ARE USING SQL SERVER TO DOWNLOAD DATA IN EXCEL USE THIS CODE

FETCH THE DATA WITH SQL CONNECTION AND HERE CMD1 IS SQLCOMMAND

SqlDataAdapter da = new SqlDataAdapter(cmd1);
                 DataTable dt1 = new DataTable();
                 da.Fill(dt1);
                 DataColumnCollection dcCollection = dt1.Columns;

                 Excel.Application ExcelApp = new Excel.Application();
                 Excel.Workbook wb = ExcelApp.Workbooks.Add();

                 int i = 1;

                 int j = 1;
                 //header row
                 foreach (DataColumn col in dt1.Columns)
                 {
                     ExcelApp.Cells[i, j] = col.ColumnName;

                     j++;

                 }

                 i++;

                 //data rows
                 foreach (DataRow row in dt1.Rows)
                 {
                     for (int k = 1; k < dt1.Columns.Count + 1; k++)
                     {
                         ExcelApp.Cells[i, k] = row[k - 1].ToString();
                     }
                     i++;

                 }
                 Microsoft.Office.Interop.Excel.Range rg = (Microsoft.Office.Interop.Excel.Range)ExcelApp.Cells[1,8];
                 rg.EntireColumn.NumberFormat = "MM/DD/YYYY";
                 Microsoft.Office.Interop.Excel.Range rg1 = (Microsoft.Office.Interop.Excel.Range)ExcelApp.Cells[1,9];
                 rg1.EntireColumn.NumberFormat = "MM/DD/YYYY";

                 ExcelApp.Columns.AutoFit();
                //wb.ActiveWorkbook.SaveAs("EmployeeDetails.xls");
                 wb.SaveAs("C:\\EmployeeDetails.xls", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                 ExcelApp.ActiveWorkbook.Saved = true;
                 ExcelApp.Quit();
                 Response.Write("<script>alert('Data Downloaded Successfully ');</script>");
 
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