title: Exporting a ListView to Excel format
body: <p>I have a <code>ListView</code> which after populating it, will look like this:</p>
  <p><a href="https://i.stack.imgur.com/ckC6c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ckC6c.png" alt="enter image description here" /></a></p>
  <p>I already can export it to a CSV formatted file using the following code:</p>
  <pre><code>StringBuilder sb = new StringBuilder();
  
  //Making columns!
  foreach (ColumnHeader ch in lvCnt.Columns)
  {
      sb.Append(ch.Text + &quot;,&quot;);
  }
  
  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(&quot; ,&quot;);
          else
              sb.Append(lvs.Text + &quot;,&quot;);
      }
      sb.AppendLine();
  }
  </code></pre>
  <p>But the problem is, in CSV, I can not export the back color of the <code>ListView</code> items and subitems, which in my case are very important. Would be nice if you can help me with this or at least point me to the right direction!</p>
  <p><strong>UPDATE</strong></p>
  <p>I managed to find a way to export directly to Excel, but I still can not export the background color of ListView items into Excel.</p>
  <pre><code>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++;
          }
  }
  </code></pre>