Hi everybody!

I'm trying to solve a problem on excel.
So it's the following. I need to create a timer where it adds 3 to the variable final each 30 seconds . . This is already done.
Now it needs to create an excel sheet and write the value of final on cell [5,2].
Code is:

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
            Worksheet ws = (Worksheet)xla.ActiveSheet;
            
            
            xla.Visible = true;

            ws.Cells[1, 1] = "Primeiro";
            ws.Cells[1, 2] = "Último";

            ws.Cells[3, 1] = txtprimeiro.Text;
            ws.Cells[3, 2] = txtultimo.Text;
            ws.Cells[5, 1] = "Tempo:";
            ws.Cells[5,2 ] = final;

Done too!

Now it needs to add the value of final and save the sheet. Next time users push start button cell [5,2] should show the value of the cell + new final value.

To save, I use the following:

string activDir = @"C:";
            string newPath = System.IO.Path.Combine(activDir, "exemplo");
            System.IO.Directory.CreateDirectory(newPath);

            ws.SaveAs(@"C:\exemplo\teste");

To find the value of cell [5,2] :

Range a1 = ws.Cells[5, 2];
    double rawValue;
    if(a1.Value == null)
    {
    rawValue = 0;
     }
    else
    {
     rawValue = a1.Value;
     }
    ws.Cells[5, 2] = final + rawValue;

This isn't working. What's wrong with this?

Range a1 = ws.Cells[5, 2];
    double rawValue;
    if(a1.Value == null)
    {
      rawValue = 0;
     }
    else
    {
     rawValue = double.Parse(a1.Value.ToString());
     }
    ws.Cells[5, 2] = final + rawValue;

This might be easier to help you understand:

Export Button:

private void btnexport_Click(object sender, EventArgs e)
        {
            
            Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
            Worksheet ws = (Worksheet)xla.ActiveSheet;
            
            
            xla.Visible = true;

            ws.Cells[1, 1] = "Primeiro";
            ws.Cells[1, 2] = "Último";

            ws.Cells[3, 1] = txtprimeiro.Text;
            ws.Cells[3, 2] = txtultimo.Text;
            ws.Cells[5, 1] = "Tempo:";
            
            
            Range a1 = ws.Cells[5, 2];
            double rawValue;
            rawValue = a1.Value;
            if (a1.Value == null)
            {
                rawValue = 0;
            }
            else
            {

              rawValue = double.Parse(a1.Value.ToString());

            }
            ws.Cells[5, 2] = final + rawValue;
            
            
            string activDir = @"C:";
            string newPath = System.IO.Path.Combine(activDir, "exemplo");
            System.IO.Directory.CreateDirectory(newPath);

            ws.SaveAs(@"C:\exemplo\teste");
          
              
}

Timer:

private void timer1_Tick(object sender, EventArgs e)
        {
            numero++;

            if (numero == 5)
            {
                numero = 0;
                final += 3;
            }
            
                label1.Text = final.ToString();
            
        }

Now i'm just getting an error on line rawValue = a1.Value; because it says "Cannot convert null to 'double' because it's a non nullable value"

Thanks for helping me so far.

Put comment // on line #21

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.