Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some data is missing in the Export to Excel using DataTable and Linq

I am exporting three worked sheet in single XL file, but I am missing some user data in the second DataTable (Education Details sheet) and third DataTable (Employeement Details sheet).

The Education Details sheet is some users are not there, but an Employeement Details sheet that users are showing. User Email Id's is there all three Database Tables.

    DataSe ds = new DataSet();
    DataTable dt = new DataTable("Registration Details");
    DataTable dt1 = new DataTable("Education Details");
    DataTable dt2 = new DataTable("Employeement Details");


    dt = bl.Get_Registrationdetailsbydate(bo);
    gv_Regdetails.DataSource = dt;
    gv_Regdetails.DataBind();
    dt1 = bl.Get_Registrationdetailsbydate1(bo);
    dt2 = bl.Get_Registrationdetailsbydate2(bo);
    DataTable filteredEducation = dt1.AsEnumerable()
          .Where(x => dt.AsEnumerable()
          .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
          .CopyToDataTable();
    DataTable filteredEmployee = dt2.AsEnumerable()
          .Where(x => dt.AsEnumerable()
          .Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()))
          .CopyToDataTable();

    dt.TableName = "Registration Details";
    filteredEducation.TableName = "Education Details";
    filteredEmployee.TableName = "Employeement Details";
    ds.Tables.Add(dt);
    ds.Tables.Add(filteredEducation);
    ds.Tables.Add(filteredEmployee);
    ExcelHelper.ToExcel(ds, "DangoteUsers.xls", Page.Response);

I did result base on first DataTable users Email, then fill second DataTable detail users base on first DataTable Email id's. Same as Employment Details. The issue in first DataTable and second DataTable. I am not returning the DataTable also.

I refer this example

like image 580
mohd mazhar khan Avatar asked Jan 27 '16 08:01

mohd mazhar khan


2 Answers

The problem is coming somewhere from the solution of conversion from DataSet to Excel in the article. Using this self made conversion is not a good idea. Use Jet/ACE engine or Microsoft Office Interop. At least they guarantee, they don't have such kind of bugs, which in future can became more. Better use something which is already highly accepted by the community. Here I wrote an approach how to do it with Interop.

First what you need to do is to add the reference to Microsoft.Office.Interop.Excel. Here is how to do it, taken from msdn article

Add the Excel assembly as a reference to the project: Right-click on the project, select Add Reference.

Click the COM tab of the Add Reference dialog box, and find Microsoft Excel 11 Object Library.

Double-click on Microsoft Excel 11 Object Library, and press OK.

Obviously if you have bigger version of Excel 11 use it.

Here is the code, there are comments/regions with the workflow of it. You should use using Excel = Microsoft.Office.Interop.Excel; as reference

    public void ExcelBtn_Click(object sender, EventArgs e)
    {
        DataSet dst = PrepareData();
        byte[] bytes = ExportDataSetToExcel(dst);

        Response.ClearContent();
        Response.ContentType = "application/msoffice";
        Response.AddHeader("Content-Disposition", @"attachment; filename=""ExportedExcel.xlsx"" ");
        Response.BinaryWrite(bytes);
        Response.End();

    }

    public static DataSet PrepareData()
    {
        DataTable badBoysDst = new DataTable("BadBoys");
        badBoysDst.Columns.Add("Nr");

        badBoysDst.Columns.Add("Name");
        badBoysDst.Rows.Add(1, "Me");
        badBoysDst.Rows.Add(2, "You");
        badBoysDst.Rows.Add(3, "Pepe");
        badBoysDst.Rows.Add(4, "Roni");

        //Create a Department Table
        DataTable goodBoysDst = new DataTable("GoodBoys");
        goodBoysDst.Columns.Add("Nr");
        goodBoysDst.Columns.Add("Name");
        goodBoysDst.Rows.Add("1", "Not me");
        goodBoysDst.Rows.Add("2", "Not you");
        goodBoysDst.Rows.Add("3", "Quattro");
        goodBoysDst.Rows.Add("4", "Stagioni");

        DataTable goodBoysDst2 = new DataTable("GoodBoys2");
        goodBoysDst2.Columns.Add("Nr");
        goodBoysDst2.Columns.Add("Name");
        goodBoysDst2.Rows.Add("1", "Not me");
        goodBoysDst2.Rows.Add("2", "Not you");
        goodBoysDst2.Rows.Add("3", "Quattro");
        goodBoysDst2.Rows.Add("4", "Stagioni");

        DataTable goodBoysDst3 = new DataTable("GoodBoys3");
        goodBoysDst3.Columns.Add("Nr");
        goodBoysDst3.Columns.Add("Name");
        goodBoysDst3.Rows.Add("1", "Not me");
        goodBoysDst3.Rows.Add("2", "Not you");
        goodBoysDst3.Rows.Add("3", "Quattro");
        goodBoysDst3.Rows.Add("4", "Stagioni");


        //Create a DataSet with the existing DataTables
        DataSet dst = new DataSet("SchoolBoys");
        dst.Tables.Add(badBoysDst);
        dst.Tables.Add(goodBoysDst);
        dst.Tables.Add(goodBoysDst2);
        dst.Tables.Add(goodBoysDst3);

        return dst;
    }

    public static byte[] ExportDataSetToExcel(DataSet dst)
    {

        #region Create The Excel

        Excel.Application excelApp = null;
        Excel.Workbook excelWorkBook = null;

        try
        {

            excelApp = new Excel.Application();

            if (excelApp == null)
                throw new Exception("You can throw custom exception here too");

            excelWorkBook = excelApp.Workbooks.Add();
            int sheetNr = 1;

            foreach (DataTable table in dst.Tables)
            {
                Excel.Worksheet excelWorkSheet = null;

                //Add a new worksheet or reuse first 3 sheets of workbook with the Datatable name
                if (sheetNr <= excelWorkBook.Sheets.Count)
                {
                    excelWorkSheet = excelWorkBook.Sheets.get_Item(sheetNr);
                }
                else
                {
                    excelWorkSheet = excelWorkBook.Sheets.Add(After: excelWorkBook.Sheets[excelWorkBook.Sheets.Count]);
                }

                excelWorkSheet.Name = table.TableName;

                for (int i = 1; i < table.Columns.Count + 1; i++)
                {
                    excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
                }

                for (int j = 0; j < table.Rows.Count; j++)
                {
                    for (int k = 0; k < table.Columns.Count; k++)
                    {
                        excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
                    }
                }

                sheetNr += 1;
            }
            //make first sheet active
            excelApp.ActiveWorkbook.Sheets[1].Select();
            excelWorkBook.SaveAs(@"c:\temp\DataSetToExcel.xlsx");


        }
        finally
        {
            excelWorkBook.Close();
            excelApp.Quit();

            //you should call GC here because there is memory problem with Interop
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        #endregion


        #region Take byte[] of the excel

        byte[] result = null;
        using (FileStream fs = new FileStream(@"c:\temp\DataSetToExcel.xlsx", FileMode.Open, FileAccess.Read))
        {
            BinaryReader reader = new BinaryReader(fs);
            result = reader.ReadBytes((int)fs.Length);
        }

        #endregion

        #region Delete the excel from the server

        File.Delete(@"c:\temp\DataSetToExcel.xlsx");

        #endregion

        return result;
    }

}

So try to use something established by the community already.This is pretty much full example how to do it with Interop. Personally I prefer to use ACE/JET engines, because there is no memory leaks problems like in the Interop(because of that we are calling the GC in the code). Creation of new sheets with ACE/JET engine is a little bit harder.

like image 94
mybirthname Avatar answered Nov 07 '22 07:11

mybirthname


I think your string comparison in linq query is a problem..your email address might have different case which could have caused this issue. Try below code

DataTable filteredEducation = dt1.AsEnumerable()
          .Where(x => dt.AsEnumerable()
          .Any(z => z.Field<string>("Email").Trim().Equals(x.Field<string>("Email").Trim(),StringComparison.CurrentCultureIgnoreCase)))
          .CopyToDataTable();
    DataTable filteredEmployee = dt2.AsEnumerable()
          .Where(x => dt.AsEnumerable()
          .Any(z => z.Field<string>("Email").Trim().Equals(x.Field<string>("Email").Trim(),StringComparison.CurrentCultureIgnoreCase)))
          .CopyToDataTable();
like image 1
Viru Avatar answered Nov 07 '22 05:11

Viru