1

I am working on a school project and im using microsoft visual studio 2010 language c# (I have teached only C and a little bit of C++). I am making an Excel file. I'm using something like "add reference", .COM and add library from excel.

I can make a page some text in row and col now.

When I push button1_Click, it opens my Excel file, and shows me the string "date" on location [1,2]. For my button2_click he is showing me my DateTime.Now (MessageBox...)

Short code version:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //public string ReturnValue1 { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)     
        {      
            ....stuff      
            oSheet.Cells[1, 2] = "Date";     
            ....      
            //now i want is oSheet.Cells[2, 2] = .//that shows me the actual date
        }

        public void button2_Click(object sender, EventArgs e)
        {
            DateTime theDate;       //variabele theDate
            theDate = DateTime.Now;  //  Date + time
            MessageBox.Show(theDate.ToString("   dd-MM-yyyy")); //only date
        }
    }
}

Any idea how to deal with it (to send my DATE to print it in Excel) if it is possible?

Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
Marcel
  • 33
  • 2
  • The answer is you your first button1 click event. In your button1 click you have oSheet.Cells[1,2] = "Date". So same concept oSheet.Cells[2,2] = DateTime.Now.ToString(" dd-MM-yyyy"); – Sorceri Jan 20 '15 at 18:36

1 Answers1

0

You are pretty much there. Only thing missing is to set the cell value. Try this

public void button2_Click(object sender, EventArgs e)
{
    oSheet.Cells[2, 2] = DateTime.Now.ToString("   dd-MM-yyyy");
}

This will set the date as a string. You can also set the data as a date so that Excel can deal with the value directly:

public void button2_Click(object sender, EventArgs e)
{
    oSheet.Cells[2, 2] = DateTime.Today; // date only, no time
}

Play a little bit around and you'll find how easy it is to set cell and range values.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62