我想使用嵌入了 Excel 数据的 SMTP 发送电子邮件。
我用datatable把外部数据带进来,用datatable的一部分做一个excel文件。我只想嵌入 excel 文件的 4 行。如何将 sheet1 更改为 html 以嵌入到电子邮件中?
private void Email()
{
//get the data from database
DataTable data = GetData();
IWorkbook workbook;
workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
....
}
最佳答案
你的问题不是很具体,但我想我明白了......
int startingRow = 0; // Row 1 in Excel is Row 0 in NPOI
int endingRow = 4;
StringBuilder builder = new StringBuilder();
builder.Append("<table>");
for (int r = startingRow; r < endingRow; r++)
{
// Check if current row is null
if (sheet1.GetRow(r) != null)
{
builder.Append("<tr>");
// Get the current row
IRow row = sheet1.GetRow(r);
// Loop through each cell in the row
for (int c = 0; c < row.LastCellNum; c++)
{
builder.Append("<td>");
// Check if current cell is null
if (row.GetCell(c) != null)
{
// Get cell value
ICell cell = row.GetCell(c);
// Append cell value between HTML table cells
builder.Append(cell.ToString());
}
builder.Append("</td>");
}
builder.Append("</tr>");
}
}
builder.Append("</table>");
// insert builder.ToString(); in your e-mail
关于c# - NPOI - 在电子邮件中嵌入excel文件的一部分 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767058/