通过从datagrid到excel的复制和粘贴传输数据,我没有遇到什么问题。
特别是来自我的用户控件,我在一个datagrid列中使用了它。文本列没问题。
我有这个代码用于复制行。
/// <summary>
/// Method which will copy entire row.
/// </summary>
private void CopyRow(object obj)
{
var datagrid = obj as System.Windows.Controls.DataGrid;
var currentRow = datagrid.SelectedIndex + 1;
Dictionary<ENotifiedTruckFlags, string> transf = new Dictionary<ENotifiedTruckFlags, string>();
string value = "";
foreach (var e in _entityEnhancedNotifiedTruck.EFlags)
{
switch (e)
{
case ENotifiedTruckFlags.CheckRecordHasBreak:
value = "Ridic má narízenou prestávku";
break;
case ENotifiedTruckFlags.CheckWeighingOn:
value = "Režim kontrolního vážení";
break;
case ENotifiedTruckFlags.NoTaraWeighted:
value = "Chybí tárové vážení";
break;
case ENotifiedTruckFlags.SPZProblem:
value = "Provedte kontrolu SPZ";
break;
case ENotifiedTruckFlags.TimeProblem:
value = "Provedte kontrolu ložné plochy";
break;
}
transf.Add(e, value);
}
var myStringBuilder = new StringBuilder();
foreach (KeyValuePair<ENotifiedTruckFlags, string> pair in transf)
{
myStringBuilder.AppendFormat("{0}", pair.Value);
myStringBuilder.Append("," + " ");
}
datagrid.SelectAllCells();
datagrid.ClipboardCopyMode = DataGridClipboardCopyMode.ExcludeHeader;
ApplicationCommands.Copy.Execute(null, datagrid);
string result = (string)Clipboard.GetData(DataFormats.Text);
datagrid.UnselectAllCells();
Clipboard.SetText($"{result}\t{myStringBuilder}\t ");
}
结果参数将导致我的文本列完美地排列在一边,但是在我的字符串构建器中,我有我的用户控件以及它在新行下面给出的文本,但是我希望将此特定文本与结果相邻,而不是在新的一行。
感谢。