Excel doc内容到webservice

Kir*_*ite 6 linq rest wpf excel web-services

我有一个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();
    }
Run Code Online (Sandbox Code Playgroud)

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);
    }
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我想从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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

所以我打开excel电子表格,然后我将如何获取内部内容并将其发送到Web服务?真的坚持代码或如何去做:/

只是寻找一种自动添加工作人员的方式,而不是手动输入名称,但看到人员excel doc可以命名为任何我想要的打开文件对话框.里面的结构总是与姓氏相同的名字.

JFT*_*TxJ 4

首先,这是我的测试 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();
    }
Run Code Online (Sandbox Code Playgroud)

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

干杯

  • 另请注意,按原样编写的我的代码将停止在 Excel 文件中“名字”列中不包含任何文本的第一行。 (2认同)