我正在使用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中。它们存在于Datatable中。这个问题的解决方案是什么?
答案 0 :(得分:1)
我没有添加列,这就是为什么它们没有出现: -
// 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;
答案 1 :(得分:0)
XlsIO是一个读取和写入Excel 2003/2007/2010/2013/2016文件的.NET库。使用XlsIO,您可以非常轻松地导入数据表xls / xlsx文档。如果您符合条件,则可以通过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工作