R.D Ip

R.D Ip

  • NA
  • 76
  • 7.6k

how to add new row sum of column in excelsheet

May 19 2021 10:16 AM
I am working with excelsheet
I am trying to add sum of column in new row but not able to add sum of column
 
Current Output: 
 
 
 
Expected Output(I want to below output):
 
 
 
see code: 
  1. private void AddExcelSheet(DataTable sDT, ExcelWorksheet excelWorksheet)  
  2. {  
  3.   
  4.     DataTable dtCustomDetail = new DataTable();  
  5.     dtCustomDetail.Columns.Add("Id"typeof(string));  
  6.     dtCustomDetail.Columns.Add("Name"typeof(string));  
  7.     dtCustomDetail.Columns.Add("Cust Name"typeof(string));  
  8.     dtCustomDetail.Columns.Add("Cust Qty"typeof(int));  
  9.   
  10.     if (sDT != null && sDT.Rows.Count > 0)  
  11.     {  
  12.         foreach (DataRow Dr in sDT.Rows)  
  13.         {  
  14.             DataRow DRowDetail = dtCustomDetail.NewRow();  
  15.   
  16.             DRowDetailCO["Id"] = Dr["Cust_Id"].ToString();  
  17.             DRowDetailCO["Name"] = Dr["Name"].ToString();  
  18.             DRowDetailCO["Cust Name"] = Dr["CUST_NAME"].ToString();  
  19.             DRowDetailCO["Cust Qty"] = Dr["Cust_QTY"].ToString();  
  20.              
  21.             dtCustomDetail.Rows.Add(DRowDetails);  
  22.   
  23.         }  
  24.     }  
  25.   
  26.     excelWorksheet.Cells[2, 1].LoadFromDataTable(dtCustomDetail, true); //here load my table  
  27.   
  28.     excelWorksheet.Cells[].Formula = "=SUM(D3:D" + (6) + ")";   //here I am trying to add some of column in new line but I am stuck
why I am using foreach loop because I have not idea how many data in table so I use foreach loop 
 
now I want to add a new column for some but I have no idea 
 
please help 

Answers (1)