我正在尝试从文本框中复制数据并将其设置在Excel文件中的特定单元格上,该文件具有2个工作表,第1个工作表名为“ PM”,第2个工作表命名为“ 4-10-2018”,该过程成功完成但在第二张纸上无法正确显示数据。 我检查了我的代码很多次,但没有找到解决此问题的方法。
代码:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet xlsht = new Microsoft.Office.Interop.Excel.Worksheet();
string path = @"D:\test.xlsx";
xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["PM"];
xlsht.Cells[11, 2] = UserNameTxt.Text + "@rasatop.com";
xlsht.Cells[11, 4] = UserNameTxt.Text;
xlsht.Cells[14, 2] = SerialTxt.Text;
xlsht.Cells[16, 2] = WLANMacTxt.Text;
xlsht.Cells[16, 3] = LANMacTxt.Text;
xlsht.Cells[16, 4] = IPTxt.Text;
xlsht.Cells[14, 5] = ComputerTxt.Text;
xlsht.Cells[16, 5] = BarcodeTxt.Text;
xlsht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
xlsht.Cells[18, 4] = VGATxt.Text;
xlsht.Cells[18, 3] = RAMTxt.Text;
xlsht.Cells[27, 4] = OSTxt.Text;
xlsht.Cells[5, 4] = System.DateTime.Today;
xlsht.Cells[26, 4] = System.DateTime.Today;
xlsht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
xlApp.Visible = true;
Microsoft.Office.Interop.Excel.Worksheet xlsht2 = new Microsoft.Office.Interop.Excel.Worksheet();
xlsht2 = xlApp.Application.Workbooks.Open(path).Worksheets["4-10-2018"];
xlsht2.Cells[4, 2] = System.DateTime.Today;
xlsht2.Cells[6, 2] = UserNameTxt.ToString();
xlsht2.Cells[6, 4] = ComputerTxt.ToString();
xlsht2.Cells[6, 5] = BarcodeTxt;
答案 0 :(得分:0)
在某些地方,您将调用TextBox控件的.ToString()方法到电子表格单元格中,而不是读取其UserNameTxt。 Text 属性,该属性将包含该值。
答案 1 :(得分:0)
这是您的问题UserNameTxt.ToString()
。您必须获取Text
属性以获取TextBox值。将UserNameTxt.ToString()
转换为UserNameTxt.Text