2017-06-26 24 views
2

我想使用帶有嵌入式excel數據的SMTP發送電子郵件。NPOI - 將excel文件的一部分嵌入到電子郵件中C#

我使用數據表來引入外部數據,並使用數據表的一部分創建一個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"); 


     .... 
     } 

回答

0

你的問題不是很具體,但我想我明白......

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 
+1

哦,是的,這就是我究竟想... row.GetCell()是辦法帶上數據..謝謝! – Scarlett

相關問題