Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表视图项拉入Excel?_C#_.net_Visual Studio - Fatal编程技术网

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

C# 如何将列表视图项拉入Excel?,c#,.net,visual-studio,C#,.net,Visual Studio,我已经在这个问题上纠缠了一天左右,需要一些帮助。在我的应用程序中,listview中有一些数据 public void OnHostPing(HostPinger host) { if (InvokeRequired) { Invoke(new OnPingDelegate(OnHostPing), new object[] { host }); return; } loc

我已经在这个问题上纠缠了一天左右,需要一些帮助。在我的应用程序中,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似乎没有产生我所期望的属性。有人能帮我一点忙吗?

我找到了一个使用Microsoft.Office.Interop.Excel的解决方案,而不是尝试使用ePlus解决方案。

由于您使用listviewitem,要从中获取属性,您必须将对象的副本存储在listviewitem.tag中。然后,在准备将数据放入Excel后,将当前listviewitem.tag强制转换为Hostpinger对象,它将具有预期的属性。ie:类似于item.tag=host然后稍后,Hostpinger foobar=(Hostpinger)item.tagso在每个item.subitem之后,我需要创建一个item.tag=hostip或其他任何子项?不只是初始项。是item.tag,不是item.subitem[].tag。然后您可以执行类似于sheet.Cells[“A2”]的操作。value=foobar.Hostname;基本上ListViewItem是它自己的类型,保存对象供以后使用的唯一方法是使用.Tag。顺便说一句,我不会这么做,但其他海报可能会试图说服您ListViewItem可能不是您任务的正确控件。按照您的示例,我得到的项目在当前上下文中不存在。我尝试在单元格填充之前直接使用它------HostPinger myTag=(HostPinger)item.tag;出于某种原因,它不喜欢那个上下文中的项目。我从来并没有让它工作过。实际上,我要走另一条路,使用Microsoft.Office.Interop.Excel创建工作簿。