Excel文档内容到webservice

我有一个wpf员工创build窗口,我可以在其中创build名字,姓氏等基本信息,这将在我的REST Web服务中创build员工。 一个例子:

客户端:

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服务? 真的停留在代码或如何去做:/

只是寻找一种自动化的方式来添加工作人员,而不是手动input名称,但看到作为员工Excel文档可以命名我想要打开的文件对话框的任何东西。 里面的结构总是和名字相同。

首先,这里是我testing的Excel文件,其中包含您要导入的职员: 在这里输入图像说明

(如果名字为'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(); } 

注意:在调用Web服务的时候,我已经把MessageBox删除了,如果列表很长的话,确保你不会感到烦恼,但是如果你需要确认每个员工的创build,你可以自由的“unREM”。 在所教的同一行中,没有证实创造已经成功发生。 我需要更多的细节来创build一个体面的validation过程。 另外非常重要的是,这不会validation您保存的员工是否已经存在于列表中。 如果多次重新运行此导入过程,则可能(也可能会)创build重复的条目。

干杯