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

NPOI - embed the part of excel file in the email C#

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