Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi friends,

I am using Vs 2008,C#,asp.net.
I have a datagrid and i am exporting to excel.
I want to apply Sum(D1:D5)formula to a cell using C#

how to do this?

Thanks in advance
george n t
Posted
Comments
ZurdoDev 17-Jan-14 7:42am    
You can do it manually after the fact or include it in your SQL so that when it gets exported it will be there.

You need to use Microsoft.Office.Interop.Excel.dll or EPPlus for performing this..

have a look on these links you will get some idea.
i had the same requirement some month before.. i had used EPPlus.. it works fine..

http://epplus.codeplex.com/discussions/250690[^]

http://stackoverflow.com/questions/5512084/excel-interop-and-formula-range/5512369#5512369[^]

http://stackoverflow.com/questions/8878896/adding-formula-to-excel-with-c-sharp-making-the-formula-shown[^]
 
Share this answer
 
Comments
[no name] 17-Jan-14 8:58am    
Better calculate the total and showit in the gridview footer. then it is easy to export to the excel
Karthik_Mahalingam 17-Jan-14 9:03am    
ya, but he needs the formula to be applied on the excel CELL ...
[no name] 17-Jan-14 9:08am    
We have already calculated the Sum. I hope we dont need of the formulae. correct me if i'm wrong.

Thanks
Karthik_Mahalingam 17-Jan-14 9:13am    
Senthil,
If the data is static your logic is fine...
suppose the user changes the values on the excel file ( after downloading), then the sum has to be calculated dynamically. in this case the formula has to be applied on the CELL...
I Hope you understood :)
george@84 17-Jan-14 22:58pm    
you are right ,Karthik. that is the requirement.Formula should be applied through web application,not desktop application
Set the ShowFooter="True" in GridView

C#
double dTotal = new double[1, dsTest.Tables[0].Columns.Count];
       for (int p = 0; p <= dsTest.Tables[0].Rows.Count - 1; p++)
       {
           for (int c = 1; c <= dsTest.Tables[0].Columns.Count - 2; c++)
           {
               dTotal[0, c] = dTotal[0, c] + Convert.ToDouble(dsTest.Tables[0].Rows[p][c]);
               GridView.FooterRow.Cells[(c+1)].Text = dTotal[0, c].ToString())
           }
   
        GridView.FooterRow.Cells[1].Text = "Total ";
       }


it's the sample code you can modify it with foreach statements for better performance.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900