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:
i have to create a custom excel report from multiple table of database. please let me know which is best way to create custom excel report from multiple datatables in c#.i am new to report making
Posted
Updated 7-Nov-13 18:14pm
v2
Comments
Homero Rivera 7-Nov-13 23:55pm    
Please tell us more. Are you supposed to "design" the report from zero and then put that on an Excel file? Or are you supposed to create a template, where you will continuously use?
Member 10312159 8-Nov-13 0:07am    
basically i am importing data from tables of database.a excel sheet with relevent table data will be generated automaticaaly if i click on a button(like create excel report).And i am not using gridview i am getting data into datatable from sql query .Datatables can be one and more than one

1 solution

This Code(for winform) allow you to create a new .csv or .txt file and save it in any location you want in you Computer. you can change the Filter to add .xls or any other type.

C#
try
            {
                SaveFileDialog saveDiag = new SaveFileDialog();
                saveDiag.Filter = " Excel (*.csv) | *.CSV | Fichier Text (*.txt)| *.txt";
                saveDiag.DefaultExt = "*.CSV";
                if (saveDiag.ShowDialog() == DialogResult.OK)
                {
                    using (Stream stream = File.Open(saveDiag.FileName, FileMode.Create,     
                           FileAccess.ReadWrite))
                    {
                        using (StreamWriter sWriter = new StreamWriter(stream, 
                               Encoding.UTF8))
                        {
                            //put your logic in here
                            sWriter.WriteLine("Name ; Last Name ; Login; Password ");
                            foreach(User user in ListUsers)
                            {
                                sWriter.WriteLine(user.Name + ";" + user.LastName + ";" +   
                                                  user.Password);    

                            }
                        }
                    }
                    MessageBox.Show("operation Completed");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.InnerException.ToString());
            }


this will generate a organized table in excel. the ";" is the Column separator.
Hope it helps.
 
Share this answer
 
v2

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