gpt4 book ai didi

c# - 使用 DataTable 和 Linq 导出到 Excel 时缺少某些数据

转载 作者:可可西里 更新时间:2023-11-01 08:28:33 26 4
gpt4 key购买 nike

我在单个 XL 文件中导出三个工作表,但我在第二个 DataTable(Education Details 表)和第三个 DataTable 中缺少一些用户数据(员工详细信息 表)。

Education Details 表是一些用户不存在的,但是用户显示的是 Employeement Details 表。用户电子邮件 ID 在所有三个数据库表中。

    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);

我根据第一个 DataTable 用户 Email 做了结果,然后根据第一个 DataTable 填充第二个 DataTable 详细用户> 电子邮件 ID。与 Employment Details 相同。第一个 DataTable 和第二个 DataTable 中的问题。我也不会返回 DataTable

我指的是 this example

最佳答案

问题出在文章中DataSet转Excel的解决方案中。使用这种自制的转换不是一个好主意。使用 Jet/ACE 引擎Microsoft Office Interop。至少他们保证,他们没有这种错误,将来会越来越多。最好使用社区已经高度接受的东西。在这里,我写了一个方法如何使用 Interop 来完成它。

首先您需要做的是添加对 Microsoft.Office.Interop.Excel 的引用。这是如何做到的,摘自 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.

显然,如果您有更大版本的 Excel 11,请使用它。

这是代码,有注释/区域和它的工作流程。您应该使用 using Excel = Microsoft.Office.Interop.Excel; 作为引用

    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;
}

}

所以尝试使用社区已经建立的东西。这是如何使用 Interop 完成它的非常完整的示例。 我个人更喜欢使用ACE/JET引擎,因为没有像Interop那样的内存泄漏问题(因为我们在代码中调用了GC)。使用 ACE/JET 引擎创建新工作表有点困难。

关于c# - 使用 DataTable 和 Linq 导出到 Excel 时缺少某些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com