拷贝字典到剪贴板为Excel

时间:2023-01-30 09:43:45

I am trying to write a tool which will compare two string tables and find values which are in one but not the other. I have got the app so that it is getting all these values correctly and I store them in a Dictionary<string, string> and use that to display it on a GridView in my XAML.

我正在尝试编写一个工具,该工具将比较两个字符串表,并找到其中一个中的值,而不是另一个中的值。我有了这个应用程序,它可以正确地获取所有这些值,我将它们存储在Dictionary 中,并使用它在我的XAML中的GridView中显示它。 ,>

I then found out that you have to manually add the copy code to the GridView so I thought I would just have a button to copy the entire contents of the dictionary, but now I am having a problem that when I try to copy it into excel it only produces one column of values and I need the key to be one column and the value to be another.

然后我发现你必须手动添加代码复制到GridView所以我想我只会有一个按钮来复制字典的全部内容,但现在我有一个问题,当我试着将它复制到excel只产生一列值的和我需要的关键一列和价值是另一个。

Here is my copy code so far:

这是我的复制代码:

        if (ToTranslate.Count != 0)
        {
            var sb = new StringBuilder();
            foreach (var item in ToTranslate)
            {
                sb.Append(item.Key + ", ");
                sb.AppendLine(item.Value);
            }

            System.Windows.Clipboard.SetData(DataFormats.Text, sb.ToString());
        }

I have tried lots of other things such as item.string which the puts them in square brackets and still doesn't work. I have also tried trimming the square brackets but this still doesn't work.

我试过很多东西,比如物品。把它们放在方括号里,仍然不起作用。我也试过修剪方括号,但还是不行。

I would have thought that it would have picked up the comma as a seperator like it does when you open up a csv but it doesn't.

我本以为它会像打开csv时那样把逗号作为分隔符,但它不是。

I don't know if this will help but this is my xaml

我不知道这是否有用,但这是我的xaml

        <ListView ItemsSource="{Binding Results}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Key}" />
                    <GridViewColumn Header="Text" Width="Auto" DisplayMemberBinding="{Binding Value}" />                        
                </GridView>
            </ListView.View>
        </ListView>

1 个解决方案

#1


1  

Excel likes the tab character as separator. To import CSV you need the import assistent of Excel and that thing only opens in case you open a file that contains the CSV data.

Excel喜欢标签字符作为分隔符。要导入CSV,您需要Excel的导入助手,它只在打开包含CSV数据的文件时打开。

Using the tab character is working well how ever.

使用制表符可以正常工作。

#1


1  

Excel likes the tab character as separator. To import CSV you need the import assistent of Excel and that thing only opens in case you open a file that contains the CSV data.

Excel喜欢标签字符作为分隔符。要导入CSV,您需要Excel的导入助手,它只在打开包含CSV数据的文件时打开。

Using the tab character is working well how ever.

使用制表符可以正常工作。