EPPlus - Excel 作为邮件附件 - 列名丢失
EPPlus - Excel as Mail attachment - Column Names are missing
我正在使用 EPPlus
。我的要求是将 excel file
作为 attachment
发送。因此,我通过定义列然后添加行来填充 DataTable
。
一切正常。直到附件到达电子邮件。但是当 Excel 文件打开时 Columns which i have defined are missing
。行显示正确。
.....
MemoryStream ms = new MemoryStream();
ms = DataTableToExcelXlsx(dt, "Attendance");
ms.Position = 0;
Attachment file = new Attachment(ms, "Attendance.xlsx");
message.Attachments.Add(file);
.....
smtp.Send(message);
...
public static MemoryStream DataTableToExcelXlsx(DataTable table, string sheetName)
{
MemoryStream Result = new MemoryStream();
ExcelPackage pack = new ExcelPackage();
ExcelWorksheet ws = pack.Workbook.Worksheets.Add(sheetName);
int col = 1;
int row = 1;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(Result);
return Result;
}
为什么列没有出现在 Excel 中。它们存在于数据表中。这个问题的解决方案是什么?
我没有添加列,这就是它们没有出现的原因:-
// Columns
int rowIndex = 1;
int colIndex = 1;
foreach (DataColumn dc in table.Columns) //Creating Headings
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.LightGray);
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
//Setting Value in cell
cell.Value = dc.ColumnName;
colIndex++;
}
// Rows
int col = 1;
int row = 2;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(Result);
return Result;
XlsIO is a .NET library that reads and writes Excel 2003/2007/2010/2013/2016 files. Using XlsIO, you can import data table xls/xlsx document very easily. The whole suite of controls is available for free (commercial applications also) through the community license program 如果您符合条件。社区许可证是没有限制或水印的完整产品。
步骤 1:创建控制台应用程序
第 2 步: 添加对 Syncfusion.XlsIO.Base 和 Syncfusion.Compression.Base 的引用;您也可以使用 NuGet 将这些引用添加到您的项目中。
第 3 步:复制并粘贴以下代码片段。
以下代码片段说明了如何使用 XlsIO 从最后一行开始填充新行
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Instantiate the excel application object.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
//Create a workbook with single worksheet
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Style definition
IStyle style = workbook.Styles.Add("Border");
style.Borders.Color = ExcelKnownColors.Black;
style.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
//Import data table
worksheet.ImportDataTable(GetDataTable(), true, 1, 1);
//Assgin created border
worksheet.UsedRange.CellStyle = style;
//Autofit columns
worksheet.UsedRange.AutofitColumns();
//Save the excel document
using (MemoryStream excelStream = new MemoryStream())
{
workbook.SaveAs(excelStream);
workbook.Close();
excelStream.Position = 0;
System.Net.Mail.Attachment oAttachment = new System.Net.Mail.Attachment(excelStream, "DataTableImported.xlsx");
}
}
有关 XlsIO 的更多信息,请参阅我们的 help documentation
注意:我为 Syncfusion 工作
我正在使用 EPPlus
。我的要求是将 excel file
作为 attachment
发送。因此,我通过定义列然后添加行来填充 DataTable
。
一切正常。直到附件到达电子邮件。但是当 Excel 文件打开时 Columns which i have defined are missing
。行显示正确。
.....
MemoryStream ms = new MemoryStream();
ms = DataTableToExcelXlsx(dt, "Attendance");
ms.Position = 0;
Attachment file = new Attachment(ms, "Attendance.xlsx");
message.Attachments.Add(file);
.....
smtp.Send(message);
...
public static MemoryStream DataTableToExcelXlsx(DataTable table, string sheetName)
{
MemoryStream Result = new MemoryStream();
ExcelPackage pack = new ExcelPackage();
ExcelWorksheet ws = pack.Workbook.Worksheets.Add(sheetName);
int col = 1;
int row = 1;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(Result);
return Result;
}
为什么列没有出现在 Excel 中。它们存在于数据表中。这个问题的解决方案是什么?
我没有添加列,这就是它们没有出现的原因:-
// Columns
int rowIndex = 1;
int colIndex = 1;
foreach (DataColumn dc in table.Columns) //Creating Headings
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.LightGray);
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
//Setting Value in cell
cell.Value = dc.ColumnName;
colIndex++;
}
// Rows
int col = 1;
int row = 2;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(Result);
return Result;
XlsIO is a .NET library that reads and writes Excel 2003/2007/2010/2013/2016 files. Using XlsIO, you can import data table xls/xlsx document very easily. The whole suite of controls is available for free (commercial applications also) through the community license program 如果您符合条件。社区许可证是没有限制或水印的完整产品。
步骤 1:创建控制台应用程序
第 2 步: 添加对 Syncfusion.XlsIO.Base 和 Syncfusion.Compression.Base 的引用;您也可以使用 NuGet 将这些引用添加到您的项目中。
第 3 步:复制并粘贴以下代码片段。
以下代码片段说明了如何使用 XlsIO 从最后一行开始填充新行
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Instantiate the excel application object.
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
//Create a workbook with single worksheet
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Style definition
IStyle style = workbook.Styles.Add("Border");
style.Borders.Color = ExcelKnownColors.Black;
style.Borders[ExcelBordersIndex.EdgeBottom].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeLeft].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeRight].LineStyle = ExcelLineStyle.Thin;
style.Borders[ExcelBordersIndex.EdgeTop].LineStyle = ExcelLineStyle.Thin;
//Import data table
worksheet.ImportDataTable(GetDataTable(), true, 1, 1);
//Assgin created border
worksheet.UsedRange.CellStyle = style;
//Autofit columns
worksheet.UsedRange.AutofitColumns();
//Save the excel document
using (MemoryStream excelStream = new MemoryStream())
{
workbook.SaveAs(excelStream);
workbook.Close();
excelStream.Position = 0;
System.Net.Mail.Attachment oAttachment = new System.Net.Mail.Attachment(excelStream, "DataTableImported.xlsx");
}
}
有关 XlsIO 的更多信息,请参阅我们的 help documentation
注意:我为 Syncfusion 工作