2017-06-26 11 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

関連する問題