从listview写入Excel非常慢

时间:2015-10-19 19:27:10

标签: c# excel

我导出的列表视图中包含六列和约50个项目。尝试将其导出到Excel文件时,大约需要2到3分钟。代码:

        linkLabel2.Text = "Please Wait...";
        Excel.Application app = new Excel.Application();
        app.Visible = false;
        Excel.Workbook wb = app.Workbooks.Add(1);
        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
        Excel.Range cells = wb.Worksheets[1].cells;
        cells.NumberFormat = "@";
        int i = 1;
        int i2 = 2;

        int x = 1;
        int x2 = 1;
        foreach (ColumnHeader ch in listView1.Columns)
        {
            ws.Cells[x2, x] = ch.Text;
            x++;
        }
        foreach (ListViewItem lvi in listView1.Items)
        {
            i = 1;
            foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
            {
                cells[i2, i] = lvs.Text;
                ws.Columns.AutoFit();
                if (lvs.Text.Contains(">"))
                {
                    cells[i2, i].Font.Color = Color.CornflowerBlue;
                }
                if (lvs.ForeColor == Color.Firebrick)
                {
                    cells[i2, i].Font.Color = Color.Firebrick;
                }
                else if (lvs.ForeColor == Color.Olive)
                {
                    cells[i2, i].Font.Color = Color.Olive;
                }
                else if (lvs.ForeColor == Color.LimeGreen)
                {
                    cells[i2, i].Font.Color = Color.LimeGreen;
                }
                else if (lvs.ForeColor == Color.Orange)
                {
                    cells[i2, i].Font.Color = Color.Orange;
                }


                i++;
            }
            i2++;
        }

        saveFileDialog1.ShowDialog();
        wb.SaveAs(saveFileDialog1.FileName);
        wb.Close();
        linkLabel2.Text = "Export";

    }

有谁知道如何让它更快?

0 个答案:

没有答案