Jump to content

Exporting to excel . . . .

- - - - -

  • Please log in to reply
1 reply to this topic

#1
MyPianoSucks

MyPianoSucks

    Newbie

  • Members
  • Pip
  • 1 posts
Hi everybody!

I have a project with a timer and two labels. The timer should add 3 to the variable final each 30 seconds. The code i use to do that was:

numero++;


            if (numero == 30)

            {

                numero = 0;

                final += 3;

            }

            

                label1.Text = final.ToString();

I declareded the variable final and numero has global variables. Other way numero will just add 1 and not 30. Then we have the export button that exposrts the result of final for an excel sheet.
Code:

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"; //we can simply ignore this

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


ws.Cells[3,1] = txtprimeiro.Text;

ws.Cells[3,2] = txtultimo.Text;

ws.Cells[5,1] = "Tempo"«;

ws.Cells[5,2] = final;


Range a1 = ws.Cells[5,2];

double rawValue = a1.Value;


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.xls"); 

Here there isn't any errors, but it isn't happening what i want. Imagine that i have a button and user presses "Start". Timer start counting and in the label will show the time. It adds 3 to the variable final each 30 seconds. Then when user presses "Export" button value of final appears on cell[5,2] of the excel sheet. But if user closes the program the number that is on the cell[5,2] will reset to the variable final because i have this line of code(ws.Cells[5,2] = final;) before (ws.Cells[5,2] = final + rawValue;) and this CAN'T happen :( . Value that is on cell[5,2] can't reset, i mean, imagine that this time it adds 6. Saves the worksheet as 6 and next day user adds again 6. So now, the number that shoud appear on the cell[5,2] is 12. How can i do this? I also tried to don't put any value of cell[5,2] but it gaves me an error: "Cannot convert null to 'double' because it is a non-nullable value". This must be so simple, and i'm stuck here :(
Thanks!

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
i dont have excel installed, and cant test this... but, are you sure you dont have to say cell[5,2].Value = whatever?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users