Click here to Skip to main content
14,694,079 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this kind of code. I would like to asked what should I do if I want to export this report into excel. Many of the example I found usually used data bind concept or data grid view. Can somebody teach me how to do it?

What I have tried:

<pre> private int LoadReport()
        {
            try
            {
                int rowcount;
                int result;
                DataTable records;
                Process process;

                dgResult.ItemsSource = null;

                switch (_process)
                {
                    case Process.I:
                        process = Process.I_RECEIVING;
                        break;
                    case Process.A:
                        process = Process.A_RECEIVING;
                        break;
                    case Process.c:
                        process = Process.C_RECEIVING;
                        break;
                    default:
                        return 0;
                }
                result = DB.GetReport(Session, out rowcount, out records, User, (int)process, DateTime.Now.AddMonths(-3), DateTime.Now);

                dgResult.ItemsSource = records.DefaultView;

                return 0;

            }
            catch
            {
                throw;
            }
Posted
Updated 18-Nov-19 11:42am

There is not space in Quick Answers to explain such a concept. However there are plenty of resources that Google will find for you. And you could also look into Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^] and Microsoft.Office.Interop.Excel Namespace | Microsoft Docs[^]
   
Comments
Member 14638721 15-Nov-19 4:34am
   
Thank you. I will go through this during weekend.
I'd recommend using a library which doesn't depend on the user having the correct version of Excel installed. For example:
GitHub - JanKallman/EPPlus: Create advanced Excel spreadsheets using .NET[^]
result = DB.GetReport(Session, out rowcount, out records, User, (int)process, DateTime.Now.AddMonths(-3), DateTime.Now);

using (var package = new ExcelPackage())
{
    var sheet = package.Workbook.Worksheets.Add("Report");
    sheet.Cells["A1"].LoadFromDataTable(records, true, TableStyles.Medium9);
    package.SaveAs(filePath);
}
   
Comments
Member 14638721 20-Nov-19 3:35am
   
result = DB.GetReport(Session, out rowcount, out records, User, (int)process, DateTime.Now.AddMonths(-3), DateTime.Now);

using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("Report");
sheet.Cells["A1"].LoadFromDataTable(records, true, TableStyles.Medium9);
package.SaveAs(package.SaveAs(new
System.IO.FileInfo(@"C:\Users\Desktop\try.xlsx"));
}
I did it like that however nothing coming up. Can you advise? Plus I actually using combo box where there are other different report cases so is it possible to use button to export it?
Richard Deeming 20-Nov-19 6:26am
   
What do you mean by nothing coming up? You're saving a file to disk.

Were you expecting to see it on screen? If so, call Process.Start passing in the file path to open the file.

If you want to export when the user clicks a button, you can call this code from the click event handler for that button.

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