L'exportation d'une liste au format Excel

J'ai un ListView qui, après remplissage, ressemblera à ceci:
L'exportation d'une liste au format Excel

J'ai déjà pouvez l'exporter dans un fichier au format CSV en utilisant le code suivant:

 StringBuilder sb = new StringBuilder();

//Making columns!
foreach (ColumnHeader ch in lvCnt.Columns)
{
    sb.Append(ch.Text + ",");
}

sb.AppendLine();


 //Looping through items and subitems
 foreach (ListViewItem lvi in lvCnt.Items)
{
    foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
    {
        if (lvs.Text.Trim() == string.Empty)
            sb.Append(" ,");
        else
            sb.Append(lvs.Text + ",");
    }
    sb.AppendLine();
}

Mais le problème est, dans le format CSV, je ne peux pas exporter la couleur de fond de la ListView éléments et sous-éléments, qui dans mon cas sont très importants. Ce serait bien si vous pouvez m'aider ou au moins m'indiquer la bonne direction!

Mise à JOUR

J'ai réussi à trouver un moyen d'exporter directement vers Excel, mais je ne peux toujours pas exporter la couleur d'arrière-plan des éléments ListView dans Excel. S'il vous plaît aider!

private void ToExcel()
{
    Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = true;
    Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(1);
    Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
        int i = 1;
        int i2 = 1;
        foreach (ListViewItem lvi in myList.Items)
        {
            i = 1;
            foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
            {                   
                ws.Cells[i2, i] = lvs.Text;
                i++;
            }
            i2++;
        }
}

OriginalL'auteur Saeid Yazdani | 2011-11-29