Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88809

How do I get if a value of an excel cell is empty in C#

$
0
0

So I'm making a simple app for time tracking at work, it scans a barcode for when you check in, and again for check out, it calculates the hours, and then saves them in a .xml. The thing is, it works depending on the day, but you may check in and out more than once per day (if you work during the morning and afternoon for example). So I need the code to check if the value of the particular cell is empty, if it is it just saves the hours, if it isnt empty tho, it gets the value of the cell and adds the new hours calculated during that time. My code is as follows.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace LCB
{

    public partial class Form1 : Form
    {
        string name;
        DateTime T_Entrada;
        DateTime T_Salida;
        TimeSpan Horas;
        public float nhoras;
        bool B_Entrada;

        public Form1()
        {
            InitializeComponent();
            B_Entrada = false;           
            textBox4.Text = B_Entrada.ToString();
        }


        //ESCIBIR EN EXCEL CODIGOO
        public void WriteSample()
        {
 if (name == "Paco")
                {

                  if(excelWorksheet.Cells[4,System.DateTime.Today.Day].Value2 == null)
                    {
                        excelWorksheet.Cells[4, System.DateTime.Today.Day].Value2 = nhoras;
                    }
                    else
                    {
                        excelWorksheet.Cells[4, System.DateTime.Today.Day].Value2 += nhoras; 
                    }
                    excelApp.ActiveWorkbook.SaveAs(@"C:\Users\PRACTICVAS\Desktop\" + System.DateTime.Today.Month + ".xls", Excel.XlFileFormat.xlWorkbookNormal);
                }
                excelWorkbook.Close();
                excelApp.Quit();
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorksheet);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelWorkbook);
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

name is a string used for now to check each different person, so the thing is, its not checking if the cell value is null or not, and always saves the new value for nhoras, which is the amount of hours after every check out. Any ideas?


Viewing all articles
Browse latest Browse all 88809

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>