wpf - Excel doc contents to webservice -


i have wpf staff creation window in can create basic information first name, last name etc creates staff in rest web service. example:

client side:

    private void createstaffmember_click(object sender, routedeventargs e)     {         string uri = "http://localhost:8001/service/staff";         stringbuilder sb = new stringbuilder();         sb.append("<staff>");         sb.appendline("<firstname>" + this.textbox1.text + "</firstname>");         sb.appendline("<lastname>" + this.textbox2.text + "</lastname>");         sb.appendline("<password>" + this.passwordbox1.password + "</password>");         sb.appendline("</staff>");         string newstudent = sb.tostring();         byte[] arr = encoding.utf8.getbytes(newstudent);         httpwebrequest req = (httpwebrequest)webrequest.create(uri);         req.method = "post";         req.contenttype = "application/xml";         req.contentlength = arr.length;         stream reqstrm = req.getrequeststream();         reqstrm.write(arr, 0, arr.length);         reqstrm.close();         httpwebresponse resp = (httpwebresponse)req.getresponse();         messagebox.show("staff creation: status " + resp.statusdescription);         reqstrm.close();         resp.close();     } 

web service side:

    #region post      [operationcontract]     [webinvoke(method = "post", bodystyle = webmessagebodystyle.bare, requestformat = webmessageformat.xml, responseformat = webmessageformat.xml, uritemplate = "/staff")]     void addstaff(staff staff);      #endregion      public void addstaff(staff staff)     {         staff.staffid = (++ecount).tostring();         staff.salt = generatesalt();         byte[] passwordhash = hash(staff.password, staff.salt);         staff.password = convert.tobase64string(passwordhash);         staffmembers.add(staff);     } 

all fine on side, im looking "import" staff details excel spreadsheet, not sure if import correct word want take first names , last names contained in such n such spreadsheet , add them web service client side wpf application.

how go it? have open file dialog:

    private void import_click(object sender, routedeventargs e)     {         microsoft.win32.openfiledialog dlg = new microsoft.win32.openfiledialog();          // show open file dialog box         nullable<bool> result = dlg.showdialog();          // process open file dialog box results         if (result == true)         {             // open document             string filename = dlg.filename;         }     } 

so open excel spread sheet how go taking inner contents , sending web service? stuck on code or how go :/

just looking automated way of adding staff members rather manually typing names, seeing staff excel doc named wanted open file dialog box. structure inside same first name last name.

first, here test excel file contains staff want import: enter image description here

(column 'a' if first name, column 'b' last name , column 'c' password...)

ok, assuming code calling web service works, here version of import_click method (and generic method save new staff):

    private void import_click(object sender, routedeventargs e)     {         microsoft.win32.openfiledialog dlg = new microsoft.win32.openfiledialog();          // show open file dialog box         nullable<bool> result = dlg.showdialog();          // process open file dialog box results         if (result == true)         {             // open document             string filename = dlg.filename;              microsoft.office.interop.excel.application vexcelobj = new microsoft.office.interop.excel.application();             try             {                 workbook theworkbook = vexcelobj.workbooks.open(filename, type.missing, true);                  worksheet sheet = theworkbook.worksheets[1];  // assuming list of staff in first worksheet                  string vfirstname = "temp";                 string vlastname = "temp";                 string vpassword = "temp";                 int vindex = 1;                  while (vfirstname != "")                 {                     // change letters of appropriate columns here!                       // in example, 'a' first name, 'b' last name , 'c' password                     vfirstname = sheet.get_range("a" + vindex.tostring()).value.tostring();                     vlastname = sheet.get_range("b" + vindex.tostring()).value.tostring();                     vpassword = sheet.get_range("c" + vindex.tostring()).value.tostring();                      this.savenewstaff(vfirstname, vlastname, vpassword);                      vindex++;                  }             }             catch (exception ex)             {                 messagebox.show("error processing excel file : " + ex.message);             }             {                 vexcelobj.quit();             }         }     }      private void savenewstaff(string firstname, string lastname, string password) {         string uri = "http://localhost:8001/service/staff";         stringbuilder sb = new stringbuilder();         sb.append("<staff>");         sb.appendline("<firstname>" + firstname + "</firstname>");         sb.appendline("<lastname>" + lastname + "</lastname>");         sb.appendline("<password>" + password + "</password>");         sb.appendline("</staff>");         string newstudent = sb.tostring();         byte[] arr = encoding.utf8.getbytes(newstudent);         httpwebrequest req = (httpwebrequest)webrequest.create(uri);         req.method = "post";         req.contenttype = "application/xml";         req.contentlength = arr.length;         stream reqstrm = req.getrequeststream();         reqstrm.write(arr, 0, arr.length);         reqstrm.close();         httpwebresponse resp = (httpwebresponse)req.getresponse();         //messagebox.show("staff creation: status " + resp.statusdescription);         reqstrm.close();         resp.close();     } 

note: have remed out messagebox in call web service make sure not annoyed if list long, free "unrem" if need confirmation every staff creation. in same line of taught, there not validation creation has occurred successfully. need more details create decent validation process. important, not validate if staff saving exists in list. if re-run import procedure multiple times, may (and will) create duplicate entries.

cheers


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -