0

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'
4
  • A Range() needs a sheet reference, i.e. asheet.Range() Commented Oct 14, 2015 at 13:05
  • How do I create the variable asheet?I tried WorksheetClass asheet but its not working. Commented Oct 14, 2015 at 13:16
  • IIRC you need a real sheet and hence a real workbook. I would look for an alternate method. Commented Oct 14, 2015 at 13:29
  • I tried using Clipboard.ContainsData(DataFormats.CommaSeparatedValue) and it worked even if the string has "\t" instead of comma. Commented Oct 14, 2015 at 13:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.