如何将列表视图项目拉入Excel?

时间:2015-09-08 19:47:50

标签: c# .net visual-studio

我已经被困在这个问题上一天左右,需要一些帮助。在我的应用程序中,我在listview中有一些数据,这是

 public void OnHostPing(HostPinger host)
    {
        if (InvokeRequired)
        {
            Invoke(new OnPingDelegate(OnHostPing), new object[] { host });
            return;
        }

        lock (_table)
        {
            ListViewItem item = (ListViewItem)_table[host.ID];
            if (item != null)
            {
                item.SubItems[0].Text = host.HostIP.ToString();
                item.SubItems[1].Text = host.HostName;
                item.SubItems[2].Text = host.HostDescription;

                item.SubItems[3].Text = host.StatusName;

                item.SubItems[4].Text = host.SentPackets.ToString();

                item.SubItems[5].Text = host.ReceivedPackets.ToString();
                item.SubItems[6].Text = PercentToString(host.ReceivedPacketsPercent);

                item.SubItems[7].Text = host.LostPackets.ToString();
                item.SubItems[8].Text = PercentToString(host.LostPacketsPercent);

                item.SubItems[9].Text = host.LastPacketLost ? "Yes" : "No";

                item.SubItems[10].Text = host.ConsecutivePacketsLost.ToString();
                item.SubItems[11].Text = host.MaxConsecutivePacketsLost.ToString();

                item.SubItems[12].Text = host.RecentlyReceivedPackets.ToString();
                item.SubItems[13].Text = PercentToString(host.RecentlyReceivedPacketsPercent);

                item.SubItems[14].Text = host.RecentlyLostPackets.ToString();
                item.SubItems[15].Text = PercentToString(host.RecentlyLostPacketsPercent);

                item.SubItems[16].Text = host.CurrentResponseTime.ToString();
                item.SubItems[17].Text = host.AverageResponseTime.ToString("F");

                item.SubItems[18].Text = host.MinResponseTime.ToString();
                item.SubItems[19].Text = host.MaxResponseTime.ToString();

                item.SubItems[20].Text = DurationToString(host.CurrentStatusDuration);

                item.SubItems[21].Text = DurationToString(host.GetStatusDuration(HostStatus.Alive));
                item.SubItems[22].Text = DurationToString(host.GetStatusDuration(HostStatus.Dead));
                item.SubItems[23].Text = DurationToString(host.GetStatusDuration(HostStatus.DnsError));
                item.SubItems[24].Text = DurationToString(host.GetStatusDuration(HostStatus.Unknown));

                item.SubItems[25].Text = PercentToString(host.HostAvailability);

                item.SubItems[26].Text = DurationToString(host.TotalTestDuration);
                item.SubItems[27].Text = DurationToString(host.CurrentTestDuration);
            }
            else
            {
                item = new ListViewItem(new string[] 
                { 
                    host.HostIP.ToString(), host.HostName, host.HostDescription,
                    host.StatusName,
                    host.SentPackets.ToString(),
                    host.ReceivedPackets.ToString(), PercentToString(host.ReceivedPacketsPercent),
                    host.LostPackets.ToString(), PercentToString(host.LostPacketsPercent),
                    host.LastPacketLost ? "Yes" : "No",
                    host.ConsecutivePacketsLost.ToString(), host.MaxConsecutivePacketsLost.ToString(),
                    host.RecentlyReceivedPackets.ToString(), PercentToString(host.RecentlyReceivedPacketsPercent),
                    host.RecentlyLostPackets.ToString(), PercentToString(host.RecentlyLostPacketsPercent),
                    host.CurrentResponseTime.ToString(), host.AverageResponseTime.ToString("F"),
                    host.MinResponseTime.ToString(), host.MaxResponseTime.ToString(),
                    DurationToString(host.CurrentStatusDuration),
                    DurationToString(host.GetStatusDuration(HostStatus.Alive)),
                    DurationToString(host.GetStatusDuration(HostStatus.Dead)),
                    DurationToString(host.GetStatusDuration(HostStatus.DnsError)),
                    DurationToString(host.GetStatusDuration(HostStatus.Unknown)),
                    PercentToString(host.HostAvailability),
                    DurationToString(host.TotalTestDuration),
                    DurationToString(host.CurrentTestDuration)
                });

我似乎无法弄清楚的是,如何将数据导出到Excel?我可以使用此代码将静态数据导出到Excel

  public static string RunSample1(DirectoryInfo outputDir)
        {

            if (!outputDir.Exists) throw new Exception("outputDir does not exist!");
            FileInfo newFile = new FileInfo(outputDir.FullName + @"\sample1.xlsx");
            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook
                newFile = new FileInfo(outputDir.FullName + @"\sample1.xlsx");
            }
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Current Devices");
                //Add the headers
                //worksheet.Cells[1, 1].Value = "ID";
                worksheet.Cells[1, 1].Value = "";
                worksheet.Cells[1, 2].Value = "Product";
                worksheet.Cells[1, 3].Value = "Quantity";
                worksheet.Cells[1, 4].Value = "Price";
                worksheet.Cells[1, 5].Value = "Value";

                //Add some items...
                worksheet.Cells["A2"].Value = 12001;
                worksheet.Cells["B2"].Value = "Nails";
                worksheet.Cells["C2"].Value = 37;
                worksheet.Cells["D2"].Value = 3.99;

我无法弄清楚如何将这些listview项目放入电子表格单元格而不是静态数据。 ListViewItems似乎没有产生我期望的属性。有人可以帮我一点吗?

0 个答案:

没有答案