2012-06-30 36 views
6

我有一個wpf員工創建窗口,我可以在其中創建名字,姓氏等基本信息,這會在我的REST Web服務中創建員工。舉個例子:Excel文檔內容到webservice

客戶端:

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服務端:

#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); 
    } 

所有在那邊很好,但是我希望 「進口」 的員工從Excel電子表格的詳細信息,不確定如果導入是正確的單詞,但我想採取在這樣的電子表格中包含的名字和姓氏,並將它們添加到客戶端wpf應用程序的Web服務。

我該怎麼辦呢?我有我的打開文件對話框:

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; 
     } 
    } 

所以,我打開我的excel電子表格,然後我將如何去走的是內部內容,並將其發送到Web服務?真的停留在代碼或如何去關於它:/

只是尋找一種自動添加工作人員的方式,而不是手動輸入名稱,但作爲員工Excel文檔可以命名爲任何我想要的打開文件對話框。裏面的結構將始終是相同的名字,然後是姓氏。

+0

這絕不會回答你的問題......但將數據存儲在數據庫中會不會容易得多? – Pynner

回答

4

首先,這裏是包含要導入的工作人員我的測試Excel文件: enter image description here

(列「A」,如果第一個名字,列「B」是姓和列「C」是密碼...)

好了,假設你的代碼中調用Web服務的作品,這裏是我的版本的Import_Click方法(和通用方法來保存新員工):

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]; // This is assuming that the list of staff is in the first worksheet 

       string vFirstName = "temp"; 
       string vLastName = "temp"; 
       string vPassword = "temp"; 
       int vIndex = 1; 

       while (vFirstName != "") 
       { 
        // Change the letters of the appropriate columns here! 
        // In my example, 'A' is first name, 'B' is last name and 'C' is the 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); 
      } 
      finally { 
       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(); 
    } 

注意:我刪除了c中的MessageBox所有這些都可以通過Web服務來確保如果列表很長時您不會對它感到煩惱,但是如果您需要確認每個員工的創建情況,您可以自由地「取消」這個服務。在所教的同一行中,沒有證實創建已成功發生。我需要更多細節來創建一個體面的驗證過程。 同樣非常重要的是,這不會驗證您保存的員工是否已經存在於列表中。如果多次重新運行此導入過程,它可能(也可能會)創建重複條目。

乾杯

+1

哇真的很好!你明白我所問的一切,並以優雅的方式雄辯地回答! +1人! –

+1

大聲笑我的榮幸。 此外,我意識到有一個小內存(或資源管理)問題。不是什麼大問題,但是如果要多次執行此過程,則可能需要去除其餘的「EXCEL.EXE」進程保持打開狀態。我只是注意到了它。 – JFTxJ

+2

此外,請注意,我的代碼按照原樣編寫,將停止在excel文件的第一行,其中不包含「first name」列中的任何文本。 – JFTxJ

相關問題