Solved

c# excel assign cell value to another worksheet

Posted on 2010-11-30
1
1,056 Views
Last Modified: 2012-06-21
I am creating an excel workbook by copying a template sheets from another workbook and renaming them.  What I need to do is sum all the created sheets on the first sheet.  When I assign the value to the cell(worksheet.Cells[3, 2]), the cell's value is System.__ComObject.  I don't even set the value of the child cell in code, it is a formula in the template.
This is my code:

                        Worksheet worksheet = ((Worksheet)originalWorkbook.Worksheets["Division Template"]);
                        worksheet.Copy(Missing.Value, previous);
                        worksheet = ((Worksheet)workbook.Worksheets["Division Template"]);
                        worksheet.Activate();
                        worksheet.Name = "Division " + ddlDivision.SelectedItem.Text;

                        while (dReader.Read())
                        {
                            previous = writeSU(dReader["SU_ID"].ToString(), dReader["Name"].ToString(), previous);
                        }

                        int i = 0;
                        foreach (Worksheet sheet in workbook.Worksheets)
                        {
                            if (sheet.Name != "Division " + ddlDivision.SelectedItem.Text)
                            {
                                i++;
                                if (i == 1)
                                    worksheet.Cells[3, 2] = "='" + sheet.Name + "'!B3";
                                else
                                    worksheet.Cells[3, 2] = worksheet.Cells[3, 2].ToString() + "+'" + sheet.Name + "'!B3";

                            }
                        }

        private Worksheet writeSU(string SU_ID, string Employee_Name, Worksheet previous)
        {
            Worksheet result;
            numberSUs++;

            result = ((Worksheet)originalWorkbook.Worksheets["SU Template"]);
            result.Copy(Missing.Value, previous);
            result = ((Worksheet)workbook.Worksheets["SU Template"]);
            result.Activate();
            result.Name = SU_ID + " " + Employee_Name;
...
            return result;
0
Comment
Question by:KS_Mis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 34242494
to apply formula you need to work with range object. please go through the following MSDN Example, i am sure it will help you:-

http://support.microsoft.com/kb/302084
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question