如何使用EPPLUS语法编写此Excel Interop语法 [英] How do I write this Excel Interop syntax in EPPLUS syntax
问题描述
有人向我建议了一种通过使用对象使其更易于管理的方式来改进代码的方法:
Someone suggested to me a method to improve my code by making it more manageable through the use of objects:
string[,] values = new string[15, 35]; //or objects
values[7, 7] = "2016";
values[7, 28] = drag24;
values[7, 33] = drag25;
values[10, 8] = digit1;
values[10, 11] = digit2;
// etc.
Range range = WS.Range[WS.Cells[1, 1], WS.Cells[15, 35]];
range.Value = values;
他的建议,但是由于我从互操作性转到了EPPLUS,以下语法不再起作用。
His suggestion, but since I moved from interop to EPPLUS, the following syntax no longer works.
Range range = WS.Range[WS.Cells[1, 1], WS.Cells[15, 35]];
在EPPLUS语法中,这种工作形式是什么样的?
What would a working form of this look like in EPPLUS Syntax?
对此进行替换并不能解决问题,而是替换了工作表中的所有内容:
Replacing it with this didnt work out right and replaced everything in the sheet:
ExcelRange range = WS.Cells["A1: AH106"]
也许是这样吗?
= sheet.Cells["A1:,12:12,14:14"]
推荐答案
不确定这是否正是您的意思,但这是一个带有自定义对象和数组的简单示例:
Not sure if this is exactly what you mean, but here's a simple example with a custom object, and arrays:
测试对象:
class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
}
使用自定义对象和数组填充工作表:
Populating a sheet with the custom object, and arrays:
IEnumerable<TestObject> objectList = new List<TestObject>()
{
{ new TestObject() {Id = 0, Name = "zero" } },
{ new TestObject() {Id = 1, Name = "one" } }
};
var values = new List<object[]>()
{
new string[] { "one", "two" },
new string[] { "three", "four" }
};
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("Sheet1");
// note second parameter gives you headings
sheet.Cells["A1"].LoadFromCollection<TestObject>(objectList, true);
sheet.Cells["A4"].LoadFromArrays(values);
File.WriteAllBytes(OUTPUT, package.GetAsByteArray());
}
这篇关于如何使用EPPLUS语法编写此Excel Interop语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!