My program allows me to paste an excel that was copyed and add to a listview. I would like to know how I can check if the String is really a copy from a excel file.I found this solution(c# : Excel : Determine if a string is a valid datasheet Range?) but its not working.
String cp = Clipboard.GetText();
string[] stringSeparators = new string[] { "\r\n" };
string[] rows = cp.Split(stringSeparators, StringSplitOptions.None);
for (int i = 0; i < rows.Length - 1; i++){
ListViewItem tempT = new ListViewItem(rows[i].Split('\t'));
listView.Items.Add(tempT);
}
The solution in the link above suggests this function:
private bool IsRange(string refr) {
try {
Range(refr);
return true; // if no error occurred, refr is a valid range
}
catch {
return false; // not a valid range
}
}
The erro says:
'Microsoft.Office.Interop.Excel.Range' is a 'type' but is used like a 'variable'
Range()needs a sheet reference, i.e.asheet.Range()WorksheetClass asheetbut its not working.Clipboard.ContainsData(DataFormats.CommaSeparatedValue)and it worked even if the string has "\t" instead of comma.