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

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

问题描述

我有有两列,服务器名称和IP地址模板,电子表格文档。
我如何填充表格让每个字典键进入自己的单元格中的服务器列和相应的价值远远在小区旁边的IP列?



我使用的是EPPlus库,但找不到的话题什么。



下面是我发现和尝试,但其名单

 使用(ExcelPackage包=新ExcelPackage(_fileInfo))
{
ExcelWorksheet工作表= package.Workbook.Worksheets [1];

的for(int i = 0; I< listOfIPs.Count;我++)
{
worksheet.Cells [I + 2,1]。价值= listOfIPs [I] ;
}

package.Save();
}


解决方案

我不熟悉EPPlus ,所以我不知道你怎么弄引用到活动工作表 - 你需要尽管一旦这样做纯粹的C#有一些关于VBA模型的知识弄清楚这一点,你可以很容易地避免任何重复让你的字典的内容对电子表格:

  //创建一个样本字典并填写
字典<字符串,字符串> myCol =新词典<字符串,字符串>();
myCol.Add(服务器1,IP1);
myCol.Add(服务器2,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的());



调试结果如预期








随着EPPlus我认为你可以做这样的(未经测试)

 使用(ExcelPackage包=新ExcelPackage(文件))
{
ExcelWorksheet工作表= package.Workbook.Worksheets.Add(测试);

worksheet.Cells [A1] LoadFromCollection(MYCOLL,真实,OfficeOpenXml.Table.TableStyles.Medium);

package.Save();
}

在VBA集合迭代和印刷板材@的 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天全站免登陆