Excel doc内容到webservice

时间:2012-06-30 01:50:53

标签: wpf linq web-services excel rest

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

客户方:

    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 doc可以命名为任何我想要的打开文件对话框。里面的结构总是与姓氏相同的名字。

1 个答案:

答案 0 :(得分: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();
    }

注意:我已经在对Web服务的调用中删除了MessageBox,以确保如果列表很长,您不会对它感到烦恼,但如果您需要确认每个人员创建,您可以“unREM”它。在教导的同一行中,没有验证创建是否已成功发生。我需要更多细节来创建一个体面的验证过程。 同样非常重要的是,这不会验证您保存的人员是否已存在于列表中。如果多次重新运行此导入过程,它可能(可能会)创建重复的条目。

干杯