将ListView导出为Excel格式 [英] Exporting a ListView to Excel format

查看:144
本文介绍了将ListView导出为Excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

填充后,我有一个ListView夹,看起来像这样:

I have a ListView wich after populating it, will look like this:

我已经可以使用以下代码将其导出为CSV格式的文件:

I already can export it to a CSV formatted file using the following code:

 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();
}

但是问题是,在CSV中,我无法导出ListView项和子项的背景色,这对我来说非常重要.如果您能为我提供帮助,或者至少将我指向正确的方向,那就太好了!

But the problem is, in CSV, I can not export the back color of the ListView items and subitems, which in my case are very important. Would be nice if you can help me with this or atleast point me to the right direction!

更新

我设法找到一种直接导出到Excel的方法,但是我仍然无法将ListView项的背景色导出到Excel.请帮忙!

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. Please help!

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++;
        }
}

推荐答案

似乎这是一个非常简单的项目,可以使用以下方式导出数据:

Seems like this is a pretty easy project to export your data with:

其中有一些示例显示了如何设置背景色和其他格式设置项.

It has examples showing how to set background colours and other formatting items.

您已经具有遍历标题和行的代码,因此您应该可以使用它!

You already have your code to loop through the headers and rows so you should be able to work with it!

这篇关于将ListView导出为Excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆