La celda vacía contiene entre comillas

Im reading data from a speadsheet and moving into a notepad csv file, in c# and then back to a speadsheet. However in the spreadsheet some cell are empty (by empty i mean have a value of = " ", just spaces and the number of spaces can vary). In the move over to notepad and back to spreadsheet these values can be lost. Is there a way to differentiate these cells so i can put quote marks around then so the cell is still present?

I have figured out that most of this empty cells have file types data_string or bool, this code works for all data_string

if (fileType == "data_string")
{
    string see = cell.getFormula().ToString();
    sw.Write("'" + see + "' ");
}
else
{

    string see = cell.getFormula().ToString();
    sw.Write(see + " ");
}

However i dont want all bool data to be in quote marks just the ones that are empty.so, so far i have

if (fileType == "bool")
{

    if (cell.getFormula().Empty)
    {
        sw.Write("'" + cell.getFormula().ToString() + "' ");
    }
}

However this doesnt work as really the cell is not empty, is there another way round this?

preguntado el 31 de julio de 12 a las 14:07

4 Respuestas

then ignore the fact that they are bool and just check whether the field is empty like:

 if ( cell.getFormula().StartsWith(" "))
                    {
                        {
                            for (int i = 0; i < cell.getFormula().Length; i++)
                            {
                                if (cell.getFormula()[i] == ' ')
                                {

                                    emptyBool.Add(" ");

                                }
                            }
                            string emptyBoolString = string.Join(" ", emptyBool.ToArray());

Respondido 03 ago 12, 15:08

i didnt think about doing it that way. ive managed to get it to work - loles

You could check to see if the cells length is longer than 0 (hence not being empty) and if not (that means it is empty) then add your apostrophes. I'm new to c# so I don't know if this will work or not

if (fileType == "bool")
{
    if (!cell.getFormula().length > 0) // If the cell is not longer than 0 characters
    {
        string see = cell.getFormula().ToString();
        sw.Write("'" + see + "' ");
    }
    else 
    {
        string see = cell.getFormula().ToString();
        sw.Write(see + " ");
    }

Respondido 31 Jul 12, 15:07

wont work spaces still count as character (oh and account deleted) - loles

i have fixed it (still being deleted) - loles

Coding import and export delimited functions by hand is fraught with danger. I would instead suggest that you use a 3rd party library like Ayudantes de archivo, which has been specifically developed to assist in these situations.

A library like FileHelpers allows you to decorate your models with specific attributes and then use the library to do the heavy lifting. Here is the example from their site: -

Modelo

[DelimitedRecord(",")]
public class Customer
{
    public int CustId;

    public string Name;

    public decimal Balance;

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

}

Clase de analizador

var engine = new FileHelperEngine(typeof(Customer));

// To Read Use:
Customer[] res = engine.ReadFile("FileIn.txt") as Customer[];

// To Write Use:
engine.WriteFile("FileOut.txt", res);

Note: There are other libraries that do a similar job however I have only used the Ayudantes de archivo and have not been disappointed.

Respondido 31 Jul 12, 15:07

 if you are using **bool**, then its a **bit** so it can't be empty,
 it is either true or false or you can use null

Respondido 31 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.