我有一个返回额外列的存储过程。我无法控制存储过程。我想使用以下表单生成我的工作表:
ws.Cells.LoadFromDataTable(dt, true, OfficeOpenXml.Table.TableStyles.Light8)
如何输出我想要的列?
我试图弄清楚Linq查询,但问题是列名不可访问,所以我无法指定我想要的列。
SqlConnection cx = new SqlConnection(util.GetConnectionString());
SqlCommand cmd = new SqlCommand("StoredRept", cx);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ta = new SqlDataAdapter();
ta.SelectCommand = cmd;
DataTable dt = new DataTable();
ta.Fill(dt);
FileInfo newFile = new FileInfo("c:\temp");
ExcelPackage epp = new ExcelPackage(newFile);
var ws = epp.Workbook.Worksheets.Add("WS");
// here is where I would like to copy certain columns off into another data table but I haven't been able to figure out how
ws.Cells.LoadFromDataTable(dt, true, OfficeOpenXml.Table.TableStyles.Light8);
非常感谢任何帮助。
答案 0 :(得分:2)
如果从表中删除某些列是唯一的问题,那么很容易解决。尝试这样的事情:
// array of parents
const p = [
{
name: 'SBRF IANUA EFS CC Integration',
expand: false,
check: 'N',
jsonDiffPath: '/repo/Dynamics/Business Service/SBRF IANUA EFS CC Integration',
changeCount: 1,
items: [
{
name: 'CRP-37920',
expand: false,
check: 'N',
jsonDiffPath: null,
changeCount: 1,
items: [],
op: 'MODIFY',
oldSnapshot: '723012',
newSnapshot: '948053',
myChange: false
}
],
id: 'F5ZGK4DPF5CHS3TBNVUWG4ZPIJ2XG2LOMVZXGICTMVZHM2LDMUXVGQSSIYQESQKOKVASARKGKMQEGQZAJFXHIZLHOJQXI2LPNY======',
objectId: '30263',
group: 'repo',
category: 'Dynamics',
type: 'Business Service'
}
];
// define a function that describes how a child should merge with its parent
function merge(p) {
// p is the parent object
// c are all of p's children defined in items
const child = p['items'];
// on the item that we return we only want keys that appear in the parent
// create an array for the flattened results
const f = [];
child.forEach(el => {
let keys = Object.keys(p);
// clone the parent object
let c = Object.assign({}, p);
// basically unset the items since we are already iterating of them
child['items'] = [];
// now iterate over all of the childrens attrs described by the parents parameters
keys.forEach(k => {
// the childs value for each key
const val = el[k];
if (val !== undefined && val !== null) {
// handle your special cases in here, such as the name
if (k === 'name') {
c['name'] = '[' + el['name'] + '] ' + c['name'];
} else {
// default behavior for the non specific cases
c[k] = el[k]
}
}
});
// add the object to the array of flattened children
f.push(c);
});
return f;
}
// iterate over all of the parents and their children
// to do this we want to apply a map to all of the parents
const m = p.map(el => merge(el));
您可以使用以下方法按名称查找列并将其删除:
var dt = new DataTable();
dt.Columns.Add("First", typeof(string));
dt.Columns.Add("Second", typeof(string));
dt.Columns.Add("Third", typeof(string));
dt.Columns.Add("Fourth", typeof(string));
dt.Columns.Add("Fifth", typeof(string));
for (var i = 1; i < 6; i++)
{
dt.Rows.Add($"First {i}", $"Second {i}", $"Third {i}",$"Fourth {i}",$"Fifth {i}");
}
//dt.Dump();//If you have linqpad this is handy to dump table to output
//REMOVE THE COLUMNS.
dt.Columns.RemoveAt(1);
dt.Columns.RemoveAt(2);
//dt.Dump();//again in linqpad this dumps table with remaining 3 columns
这是获取所需列的列表的linq查询。
var col=dt.Columns["Second"];
dt.Columns.Remove(col);
请注意如何使用列索引或名称从行对象中获取值。
答案 1 :(得分:2)
您可以使用ScrollViewer
代替IEnumerable<T>
加载电子表格吗?如果是这样的话:
DataTable
如果您需要var someColumns = dt
.AsEnumerable()
.Select(row => new
{
When = row.Field<DateTime>("When"),
What = row.Field<string>("What"),
HowMany = row.Field<int>("ColumnNameInDatabase"),
});
,可以通过this或this等任何方法将DataTable
转换为{。}}。
坦率地说,我回答了你的问题,但IMO你手头有一个更大的问题,那就是使用那些过时的IEnumerable
对象而不是DataTable
。例如,如果您考虑使用IEnumerable<T>
库,则可以执行此操作:
Dapper
我看到您正在使用util.GetConnectionString()
.Query("StoredRept", commandType: CommandType.StoredProcedure) // Extension method from Dapper
.Select(dyn => new
{
When = (DateTime)dyn.When,
What = (string)dyn.What,
HowMany = (int)dyn.ColumnNameInDatabase
})
库进行Excel操作。它可以加载EPPlus
个数据,但不限于IEnumerable<T>
。