c# - NPOI - 在电子邮件中嵌入excel文件的一部分 C#

标签 c# excel smtp npoi

我想使用嵌入了 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/

相关文章:

c# - 从 Web API 在 MVC 中存储 Bearer Token 的位置

C# OleDbParameter 与 Access DateTime 查询

excel - 对于从第一个 FOR 单元格开始的每个 In : Is it possible to run through a single-column range and paste multiple columns,?

perl - sendmail 如何从任何域发送电子邮件?

smtp.office365.com SMTP 服务器中的 C# 邮件发送错误

c# - 在 Visual Studio 中增加构建修订号

c# - 如何以编程方式 checkout 要在 TFS 中编辑的项目?

excel - 当自动触发公式时,如何更改我的 Worksheet_Change 代码以更改单元格的值

VBA事件: load workbook before running code using workbook_open

java - java play框架中的gmail smtp异常