如何从字典向电子表格添加值? [英] How to add values to a spreadsheet from a dictionary?

查看:123
本文介绍了如何从字典向电子表格添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板电子表格文档,其中有两个列,即服务器名称和IP地址。
如何填充电子表格,以便每个字典键都位于服务器列中的自己的单元格中,相应的值位于IP列中的单元格旁边?



我正在使用EPPlus库,但找不到任何主题。



以下是我发现和尝试的,但是它的列表<使用(ExcelPackage package = new ExcelPackage(_fileInfo))
{
ExcelWorksheet工作表= package.Workbook.Worksheets p>

  [1]; (int i = 0; i< listOfIPs.Count; i ++)



工作表Cells [i + 2,1] .Value = listOfIPs [i] ;
}

package.Save();
}


解决方案

我不熟悉EPPlus因此,我不知道如何获得对活动表单的引用 - 您需要弄清楚这一点,虽然一旦完成了纯C#,一些关于VBA模型的知识,您可以轻松地避免任何迭代将您的字典的内容获取到电子表格中:

  //创建一个示例字典并填写
Dictionary<字符串,字符串> myCol = new Dictionary< string,string>();
myCol.Add(server1,ip1);
myCol.Add(server2,ip2);

//获取对活动工作表的引用
//这可能会根据您使用的框架而有所不同
工作表ws = Globals.ThisWorkbook.ActiveSheet作为工作表;

//创建一个范围变量
范围myRange;

//将键转移到列A
myRange = ws.get_Range(A1);
myRange.get_Resize(myCol.Keys.ToArray()。Count(),1).Value =
ws.Parent.Parent.Transpose(myCol.Keys.AsEnumerable()。ToArray());

//将值转换为列B
myRange = ws.get_Range(B1);
myRange.get_Resize(myCol.Values.ToArray()。Count(),1).Value =
ws.Parent.Parent.Transpose(myCol.Values.AsEnumerable()。ToArray());

按预期调试结果







$ b $使用EPPlus我想你可以这样做(未经测试)

  using(ExcelPackage package = new ExcelPackage ))
{
ExcelWorksheet工作表= package.Workbook.Worksheets.Add(test);

workheet.Cells [A1]。LoadFromCollection(myColl,true,OfficeOpenXml.Table.TableStyles.Medium);

package.Save();
}

有关VBA Collections迭代的更多详细信息,并打印到Sheet @ vba4all.com


I have a template spreadsheet document that has two columns, Server Name and IP Address. How can I populate the spreadsheet so that each dictionary key goes in its own cell in the Server column and the corresponding value goes in the cell next to it in the IP column?

I am using the EPPlus library but couldn't find anything on the topic.

Below is what I found and tried, but its for lists

using (ExcelPackage package = new ExcelPackage(_fileInfo))
{
    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

    for (int i = 0; i < listOfIPs.Count; i++)
    {
        worksheet.Cells[i + 2, 1].Value = listOfIPs[i];
    }

    package.Save();
}

解决方案

I am not familiar with EPPlus, therefore I am not sure how you get the reference to the active sheet - you need to figure out this bit though once that's done pure C# with a bit of knowledge about VBA model and you can easily avoid any iterations to get contents of your dictionary to a spreadsheet:

// create a sample dictionary and fill it
Dictionary<string, string> myCol = new Dictionary<string, string>();
myCol.Add("server1", "ip1");
myCol.Add("server2", "ip2");

// grab a reference to the active Sheet
// this may vary depending on what framework you are using
Worksheet ws = Globals.ThisWorkbook.ActiveSheet as Worksheet;

// create a Range variable
Range myRange;

// Transpose the keys to column A
myRange = ws.get_Range("A1");
myRange.get_Resize(myCol.Keys.ToArray().Count(),1).Value = 
                 ws.Parent.Parent.Transpose(myCol.Keys.AsEnumerable().ToArray());

// transpose the Values to column B
myRange = ws.get_Range("B1");
myRange.get_Resize(myCol.Values.ToArray().Count(), 1).Value = 
               ws.Parent.Parent.Transpose(myCol.Values.AsEnumerable().ToArray());

Debugging results as expected


With EPPlus I think you can do it like this (untested)

using (ExcelPackage package = new ExcelPackage(file))
{
    ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test");

    worksheet.Cells["A1"].LoadFromCollection(myColl, true, OfficeOpenXml.Table.TableStyles.Medium);

    package.Save();
}

More details on VBA Collections iterations and printing to Sheet @ vba4all.com

这篇关于如何从字典向电子表格添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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