gpt4 book ai didi

wpf - Excel 文档内容到 Web 服务

转载 作者:行者123 更新时间:2023-12-02 06:24:37 24 4
gpt4 key购买 nike

我有一个 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();
}

网络服务端:

    #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 电子表格,然后如何获取内部内容并将其发送到网络服务?真的被代码或如何处理它所困:/

只是寻找一种自动添加员工的方式,而不是手动输入姓名,但由于员工 Excel 文档可以在打开文件对话框中命名为我想要的任何名称。内部结构将始终是相同的名字然后姓氏。

最佳答案

首先,这是我的测试 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”它。在同一行教学中,没有验证创建是否已成功发生。我需要更多细节来创建一个像样的验证过程。同样非常重要的是,这不会验证您要保存的员工是否已存在于列表中。如果您多次重新运行此导入过程,它可能(并且可能会)创建重复的条目。

干杯

关于wpf - Excel 文档内容到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com