简体   繁体   中英

List of list string c#

I'm creating some methods for using CarlosAG ExcelXMLWriter, I want to improve this method in particular

public void insertaRango(Worksheet sheet, List<object> values, int columnMerge = 0, string styleID = null, DataType dt = DataType.String)
{
    WorksheetRow row = sheet.Table.Rows.Add();
    WorksheetCell cell;
    foreach (var value in values)
    {
        cell = row.Cells.Add();
        cell.Data.Text = value.ToString();
        cell.MergeAcross = columnMerge;
        cell.StyleID = styleID;
        cell.Data.Type = dt;
    }
}

it generates a range of data with some options, the main problem is if I want to write this data

//Client    Money       Date
//------    -----       ----------
//Jhon Doe  1,034.88    2013-02-01
//Chris S.  4,882.12    2013-02-08
//
//TOTAL     XXXXXXXX    

I can't do a =SUM function for column "Money" because the List only accepts one type of data, if I set it to string numbers won't be used like that, if I use int strings won't be able to parse... The same problem is present for rest of params for this method, it can't be one param for each item...

So basically what I want to do something like this, but I'm lost...

Objects {
    "Item 1" { // This is the value of the cell.
        dt => String,
        columnMerge => 0,
        styleID => "MyStyle",
    },
    "Item 2" { // This is the value of the cell.
        dt => String,
        columnMerge => 3,
        styleID => "MyStyle2",
    },
    "1,034.88" { // This is the value of the cell.
        dt => Number,
        columnMerge => 0,
        styleID => "numberStyle",
    }
}

And then, of course, parse everything on the method...

I hope I've explained everything good, thank you. UPDATE

Thanks to the answer given, I've added the constructor to the class and now everything is working, thank you.

public class CellValue
{
    public CellValue()
    {
        ColumnMerge = 0;
        StyleID = "Default";
        DataType = DataType.String;
        Value = null;
    }
    public int ColumnMerge { get; set; } // Combinación de celdas indice base 0.
    public string StyleID { get; set; } // Estilo deseado para la celda (consultar método generarEstilos).
    public DataType DataType { get; set; } // Tipo de datos de la celda ()
    public object Value { get; set; } // Valor de la celda.
}

If you want to be able to set individual parameters for each cell, declare container type for cell values:

class CellValue
{
    public int ColumnMerge { get; set; }
    public string StyleID { get; set; }
    public DataType DataType { get; set; }
    public object Value { get; set; }
}

and pass list of CellValue instead of list of object :

    public void insertaRango(Worksheet sheet, List<CellValue> values)
    {
        WorksheetRow row = sheet.Table.Rows.Add();
        WorksheetCell cell;
        foreach (var value in values)
        {
            cell = row.Cells.Add();
            cell.Data.Text = value.Value.ToString();
            cell.MergeAcross = value.ColumnMerge;
            cell.StyleID = value.StyleID;
            cell.Data.Type = value.DataType;
        }
    }

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