使用List<>填充Excel工作表项

时间:2015-03-05 09:39:56

标签: c#

我有一个List<>对于不同的条目,每个entrie都存在多个元素。 (品牌,版本,变体)

List<car> carList = new List<car>();

carList包含以下元素:品牌,版本,变体 这实际上是我的代码:

public partial class MainForm
{
    public void ProtokollToolStripMenuItemClick(object sender, EventArgs e)
    {
        Excel.Application ExcelApplication;
        Excel._Workbook ExcelWorkbook;
        Excel._Worksheet ExcelWorksheet;

        try
        {
            //Start Excel
            ExcelApplication = new Excel.Application();
            ExcelApplication.Visible = true;                
            //New Workbook/Worksheet
            ExcelWorkbook = (Excel._Workbook)(ExcelApplication.Workbooks.Add(Missing.Value));
            ExcelWorksheet = (Excel._Worksheet)ExcelWorkbook.ActiveSheet;               
            //Counter
            int countplus1 = 0;
            int countplus4 = 0;

            foreach (car A in carList)
            {               
                //Fill the List
                countplus1 += 1;
                countplus4 += 4;
                ExcelSheet.Cells[3 + countplus4, 2] = "Brand:";
                ExcelSheet.Cells[3 + countplus4 + 1, 2] = "Version:";
                ExcelSheet.Cells[3 + countplus4 + 2, 2] = "Variant:";
                ExcelSheet.Cells[3 + countplus4, 3] = A.CarBrand;
                ExcelSheet.Cells[3 + countplus4 + 1, 3] = A.CarVersion;
                ExcelSheet.Cells[3 + countplus4 + 2, 3] = A.CarVariant;
            }   
        }
        catch
        {
        }
    }

目前我的Excel表格看起来像这样:

    {
    Brand:      Audi
    Version:    A4
    Variant:    Limousine

    Brand:      VW
    Version:    Golf
    Variant:    Limousine

    Brand:      Audi
    Version:    A3
    Variant:    Wagon

    Brand:      Audi
    Version:    A4
    Variant:    Limousine
}

现在我希望excelsheet看起来像那样:

    {
    Brand:      Audi
    Version:    A4
    Variant:    Limousine
                Wagon
                Coupe

    Version:    A3
    Variant:    Limousine
                Wagon       

    Brand:      VW
    Version:    Golf
    Variant:    Limousine
                Coupe
}

有人可以帮助我,我现在不知道如何做到这一点?也许用try&amp; CATH?

谢谢大家

2 个答案:

答案 0 :(得分:0)

由于您没有发布Car课程的内容,我需要假设Car看起来像这样:

public class Car
{
  public String CarBrand{ get; set; }
  public String CarVersion{ get; set; }

  //if this is only a single string, you will need to change it.
  public List<String> Variants { get; set; }
}

然后你可以这样做:

public void ProtokollToolStripMenuItemClick(object sender, EventArgs e)
{
    Excel.Application ExcelApplication;
    Excel._Workbook ExcelWorkbook;
    Excel._Worksheet ExcelWorksheet;

    try
    {
        //Start Excel
        ExcelApplication = new Excel.Application();
        ExcelApplication.Visible = true;                
        //New Workbook/Worksheet
        ExcelWorkbook = (Excel._Workbook)(ExcelApplication.Workbooks.Add(Missing.Value));
        ExcelWorksheet = (Excel._Worksheet)ExcelWorkbook.ActiveSheet;               
        //Counter

        int countplus1 = 0;
        int countplus4 = 0;

        foreach (Car A in carList)
        {               
            //Fill the List
            countplus1 += 1;
            countplus4 += 4;
            ExcelSheet.Cells[3 + countplus4, 2] = "Brand:";
            ExcelSheet.Cells[3 + countplus4 + 1, 2] = "Version:";
            ExcelSheet.Cells[3 + countplus4 + 2, 2] = "Variant:";

            ExcelSheet.Cells[3 + countplus4, 3] = A.CarBrand;
            ExcelSheet.Cells[3 + countplus4 + 1, 3] = A.CarVersion;

            if(A.Variants == null) continue;

            foreach(String variant in A.Variants)
            {
                ExcelSheet.Cells[3 + countplus4 + (A.Variants.IndexOf(variant)+2), 3] = variant;
            }
        }   
    }
    catch
    {
    }
}

答案 1 :(得分:0)

您希望按品牌分类数据,然后按版本分组。你可以使用Linq来做到这一点。

要进行单级分组(例如按品牌),您需要这样做:

IGrouping<String,Car> carsByBrand = cars.GroupBy( car => car.Brand );

但要进行多级分组,我们首先按最内层标准(Version)进行分组,然后将所有这些组合在一起(SelectMany),然后按{{1}组合}}:

Brand

枚举此系列时,您将获得以下结果:

var carsByBrandByVersion =
    cars.GroupBy( car => car.Version ).
    SelectMany(
        g => g.GroupBy( g2 => g2.Brand )
    );

这为您提供所需格式的控制台输出,将其转换为使用Excel应该是一件小事:)