将制表符分隔的值从剪贴板复制到datagridview

时间:2018-07-23 20:47:04

标签: c#

从应用程序复制剪贴板时,剪贴板中的原始数据看起来像这样

Trade Info

NE62 -- NE62

Symbol      Side        Quantity        Avg Price       ClientAcct      

ESU8        BUY     100     2809.2500       35199008        
FLT.V       SELL        15,000      1.7040      tB324aV     
TRST.TO     SELL        4,850       7.1500      tB324aVV        
YGR.TO      SELL        5,200       5.3806      tB324aV 

string s = Clipboard.GetText();

output(s) >> Trade Info\r\n\r\nNE62 -- NE62\r\n\r\nSymbol\t\tSide\t\tQuantity\t\tAvg Price\t\tClientAcct\t\t\r\n\r\nESU8\t\tBUY\t\t100\t\t2809.2500\t\t35199008\t\t\r\nFLT.V\t\tSELL\t\t15,000\t\t1.7040\t\tPRBCOAV\t\t\r\nTRST.TO\t\tSELL\t\t4,850\t\t7.1500\t\tPRBCOAV\t\t\r\nYGR.TO\t\tSELL\t\t5,200\t\t5.3806\t\tPRBCOAV\t\t\r\n"

我使用正则表达式删除了第一行(贸易信息NE62-NE62),因为它对于每个用户都是可变的。

然后,我使用以下字符串动态分配列。每个用户都有不同的列长,具体取决于他们所需的信息

string columnarray = s.Substring(0, s.LastIndexOf("\t\t\r\n\r\n"));

output(columnarray) >> Symbol\t\tSide\t\tQuantity\t\tAvg Price\t\tClientAcct\t\t\r\n\r

string[] ColArray = Regex.Split(columnarray, "\t\t");
foreach (string b in ColArray)
{
    d.Columns.Add(b);
}

剩余的字符串存储在另一个变量中,该变量要填充到数据表中,这是我在弄清楚时遇到的问题。以下是我需要填写到数据表中的剩余值。

首选的解决方案是遍历字符串,每次按\ t \ t时将其拆分并放在同一行中,并在按\ r \ n时转至下一行。< / strong>

ESU8\t\tBUY\t\t100\t\t2809.2500\t\tB324aV\t\t\r\nFLT.V\t\tSELL\t\t15,000\t\t1.7040\t\tB324aV\t\t\r\nTRST.TO\t\tSELL\t\t4,850\t\t7.1500\t\tB324aV\t\t\r\nYGR.TO\t\tSELL\t\t5,200\t\t5.3806\t\tB324aV\t

    private void button1_Click(object sender, EventArgs e)
    {
        DGVUpload.Rows.Clear();
        DGVUpload.Columns.Clear();
        string sepo = "\t";
        string s = Clipboard.GetText();

        s = Regex.Replace(s, "[A-Za-z]{5}[ ][A-Za-z]{4}[\\r][\\n][\\r][\\n][A-Za-z]{2}[0-9]{2}[ ][-][-][ ][A-z]{2}[0-9]{2}[\\r][\\n][\\r][\\n]", "");

        string columnarray = s.Substring(0, s.LastIndexOf("\t\t\r\n\r\n"));

        string dataArray = s.Substring(s.LastIndexOf("\t\t\r\n\r\n"));
        dataArray = Regex.Replace(dataArray, "[\\t][\\t][\\r][\\n][\\r][\\n]", "");


        //string[] bitts = (s.Trim('\t')).Split(sepo.ToCharArray());
        //string[] bitts2 = bitts.Where(x => !string.IsNullOrEmpty(x)).ToArray();
        string[] ColArray = Regex.Split(columnarray, "\t\t");
        string[] DatArray = Regex.Split(dataArray, "\t\t");

        DataTable d = new DataTable();
        foreach (string b in ColArray)
        {
            d.Columns.Add(b);
        }

        DGVUpload.DataSource = d;


    }

理想的结果应如下所示。

Ticker | Action | Quantity | Price     | Acc Number
---------------------------------------------------
ESU8   | BUY    | 100      | 2809.2500 | 900000 
FLT.V  | SELL   | 15,000   | 1.7040    | B324aV 
TRT.TO | SELL   | 4,850    | 7.1500    | f23AAA 
YGR.TO | SELL   | 5,200    | 5.3806    | 42ABBB

3 个答案:

答案 0 :(得分:1)

如果我正确理解了以下代码可以解决的问题:

public Form1()
{
    InitializeComponent();
    dataGridView1.DataSource = CreateTable();
}

private DataTable CreateTable()
{
    var str = "ESU8\t\tBUY\t\t100\t\t2809.2500\t\tB324aV\t\t\r\nFLT.V\t\tSELL\t\t15,000\t\t1.7040\t\tB324aV\t\t\r\nTRST.TO\t\tSELL\t\t4,850\t\t7.1500\t\tB324aV\t\t\r\nYGR.TO\t\tSELL\t\t5,200\t\t5.3806\t\tB324aV\t";

    DataTable dt = new DataTable();

    dt.Columns.Add("Ticker", typeof(string));
    dt.Columns.Add("Action", typeof(string));
    dt.Columns.Add("Quantity", typeof(double));
    dt.Columns.Add("Price", typeof(double));
    dt.Columns.Add("Acc Number", typeof(string));

    str = str.Replace("\t\t", " ");
    str = str.Replace(",", ".");
    str = str.Replace("\r\n", "");

    var r = str.Split();

    int rows = r.Length / 5;
    for (int i = 0; i < rows; i++)
    { 
        dt.Rows.Add(
            r[i * 5 + 0], 
            r[i * 5 + 1],                                  
            double.Parse(r[i * 5 + 2]),
            double.Parse(r[i * 5 + 3]),
            r[i * 5 + 4]);
    }

    return dt;
}

输出:

enter image description here

答案 1 :(得分:1)

side-menu.js

答案 2 :(得分:0)

所以我设计了一个似乎效果很好的解决方案。我使用了一个解析器来解析字符串,并在格式化字符串后将其添加到dataTable中,以便其解析友好。只是将其发布在这里,以防将来其他人需要帮助。

string[] ColArray = Regex.Split(columnarray, "\t\t");
string[] DatArray = Regex.Split(dataArray, "\t\t");
DatArray = DatArray.Where(x => !string.IsNullOrEmpty(x)).ToArray();

//Join all the split string using (+)
string datstring = string.Join("+", DatArray);

//Remove the additional (+) symbol infront of the \r\n\ to match array length        
datstring = Regex.Replace(datstring, @"[+\\r\\]\B", "");
DataTable d = new DataTable();

foreach (string b in ColArray{
    d.Columns.Add(b);                
    }
// use a parser to parse through the string and add the items to your datatable
using (var reader = new StringReader(datstring))
    {
        TextFieldParser parser = new TextFieldParser(reader)
        { HasFieldsEnclosedInQuotes = false, Delimiters = new string[] { "+" } };
        while(!parser.EndOfData)
        {
            var drow = d.NewRow();
            drow.ItemArray = parser.ReadFields();
            d.Rows.Add(drow);
        }
     }
DGV.Datasource = d;