简体   繁体   中英

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

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?

If you want to avoid navigating all of the various values that Range.Value and Range.Value2 can return, it's easier just to read the Text property. It returns a string containing the visible contents of the the cell. If it's an empty string, the cell doesn't contain a value. It's never null.

if(excelWorksheet.Cells[4,System.DateTime.Today.Day].Text == string.Empty)

or

if(excelWorksheet.Cells[4,System.DateTime.Today.Day].Text.Length == 0)

That's assuming that the cell you're inspecting would never contain a formula that returns an empty string.

Suppose the cell contains

=""

Then the .Text property would return an empty string, but the cell isn't really empty. In the very unlikely chance that you have to account for that, you would also want to make sure that the .Formula property also returns an empty string.

For simplicity you could write a function like:

bool IsEmptyCell(Range cell)
{
    return cell.Text.Length == 0 && cell.Formula.Length == 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM