Click here to Skip to main content
15,666,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created one application. I can export data to Excel using Excel Interop. Later I am doing calculation manually. Here I want use the Excel which already I written formulas in Sheet1 & the data which I exporting from my application should load in Sheet2. Once data loaded completely, result should display in Sheet1. Pl, help on this request.
Below is the Code that I can export data to new excel.

What I have tried:

public void ExportToExcel(ListView lsvData, string sSheeyName)
            Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            xla.Visible = true;
            Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;
            int i = 4;
            int j = 1;
            int c = 1;
            int r = 5;
            int ilsvCol = 0;
            string sFcol;
            ws.Name = sSheeyName;

            ws.Cells[c, r] = ws.Name;
            ws.Cells[c, r].Font.Bold = true;
            ws.Cells[c, r].Font.Size = 20;

            foreach (ColumnHeader lsvcolumn in lsvData.Columns)
                if (lsvcolumn.Width > 0)
                    ws.Cells[i, j] = lsvcolumn.Text;
                    ws.Cells[i, j].Font.Bold = true;
                    ws.Cells[i, j].Interior.Color = Color.GreenYellow;
                    ws.Columns[j].ColumnWidth = lsvcolumn.Text.Length + 10;

            if (j > 52)
                sFcol = "B" + Convert.ToChar(64 + j - 52).ToString();
            else if (j > 26)
                sFcol = "A" + Convert.ToChar(64 + j - 26).ToString();
                sFcol = Convert.ToChar(64 + j).ToString();

            i = 5;
            j = 1;
            foreach (ListViewItem comp in lsvData.Items)
                ilsvCol = 0;
                foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)
                   // if (lsvData.Columns[ilsvCol].Width > 0)
                    if (lsvData.Columns[ilsvCol].Width > 0)

                        // ws.Cells[i, j] = "'" + drv.Text.ToString();
                        ws.Cells[i, j] =  drv.Text.ToString();
                        ws.Cells[i, j].Style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                j = 1;


Updated 19-Sep-16 22:04pm
Patrice T 17-Sep-16 10:42am    
What is the problem with your code ?
ShivAkshay 20-Sep-16 1:29am    
Pl. See my newly added comments.
harish85 17-Sep-16 14:36pm    
please explain your problem clearly
ShivAkshay 20-Sep-16 1:30am    
Pl. See my comment added Neely.
ShivAkshay 19-Sep-16 22:53pm    
Hi harish85 & ppolymorphe. my question is very clear. With attached code I can export obtained data to one new excel sheet. There is no issue this code. Once export I want to spend extra time to do some calculation with export data. Instead of this I will create one excel with defined formulas in sheet1 & save it in designated folder. Later I want export data's from my C# application directly to this same excel sheet2. With out sending any extra time the result should display automatically in sheet1. I believe now you can under stand the my question.

1 solution

you can try this libray closedxml.
i used for some project and worked fine.
Share this answer

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