如何将TextBox值input(从UserForm)保存到工作表上的单元格?

在这里输入图像说明

我有一个看起来像这样的表单。 我的目标是将13个文本框/combobox添加到Excel表单中。 此外,我有2个button下面这个没有出现在名为“保存”和“添加”的图片 – 保存是非常自我解释。 Add是保存来自UserForm的input,并将其放入带有相应标题的Excel行中,清除UserForm,最后让用户再次input数据,这些数据将进入下面的行。 这里是我的代码到目前为止在添加button:

public partial class Form1 : Form { Microsoft.Office.Interop.Excel.Application oXL; Microsoft.Office.Interop.Excel._Workbook oWB; Microsoft.Office.Interop.Excel._Worksheet oSheet; Microsoft.Office.Interop.Excel.Range oRng; int num; public Form1() { oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add("")); oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet; InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { num++; oSheet.Cells[1, 1] = "UserName"; oSheet.Cells[1, 2] = "Workstation Name"; oSheet.Cells[1, 3] = "Manufacturer"; oSheet.Cells[1, 4] = "Model"; oSheet.Cells[1, 5] = "Serial"; oSheet.Cells[1, 6] = "CPU"; oSheet.Cells[1, 7] = "RAM"; oSheet.Cells[1, 8] = "OS"; oSheet.Cells[1, 9] = "Version"; oSheet.Cells[1, 10] = "Microsoft Office"; oSheet.Cells[1, 11] = "Recommendations"; oSheet.Cells[1, 12] = "Comments"; oSheet.get_Range("A1", "L1").Font.Bold = true; oSheet.get_Range("A1", "L1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; string[,] saNames = new string[100, 13]; saNames[num, 0] = txtUsername1.Text; saNames[num, 1] = txtWorkName1.Text; saNames[num, 2] = cbxManufac.Text; saNames[num, 3] = cbxMachType.Text; saNames[num, 4] = txtModel.Text; saNames[num, 5] = txtSerial.Text; saNames[num, 6] = txtCPU.Text; saNames[num, 7] = cbxRAM.Text; saNames[num, 8] = cbxOS.Text; saNames[num, 9] = txtVersion.Text; saNames[num, 10] = txtMcstOffice.Text; saNames[num, 11] = txtRecomend.Text; saNames[num, 12] = txtComments.Text; oSheet.get_Range("A2", "L1000").Value = saNames; } 

我遇到的问题是,我的添加button不保存input到Excel单元格中,它消失了,我不知道我在做什么错误,所以我决定问以前遇到过这个问题的人。

另外,我似乎开始行“A3”而不是“A2”有点奇怪

我不是这个杂技专家,但你可以试试这个

 public partial class Form1 : Form { Microsoft.Office.Interop.Excel.Application oXL; Microsoft.Office.Interop.Excel._Workbook oWB; Microsoft.Office.Interop.Excel._Worksheet oSheet; Microsoft.Office.Interop.Excel.Range oRng; int num = 1; public Form1() { oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add("")); oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet; InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // here you create the Headers once the Form is loaded initHeaders(); } // a Method to create the Headers in the file private void initHeaders() { oSheet.Cells[1, 1] = "UserName"; oSheet.Cells[1, 2] = "Workstation Name"; oSheet.Cells[1, 3] = "Manufacturer"; oSheet.Cells[1, 4] = "Model"; oSheet.Cells[1, 5] = "Serial"; oSheet.Cells[1, 6] = "CPU"; oSheet.Cells[1, 7] = "RAM"; oSheet.Cells[1, 8] = "OS"; oSheet.Cells[1, 9] = "Version"; oSheet.Cells[1, 10] = "Microsoft Office"; oSheet.Cells[1, 11] = "Recommendations"; oSheet.Cells[1, 12] = "Comments"; oSheet.get_Range("A1", "L1").Font.Bold = true; oSheet.get_Range("A1", "L1").VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter; } private void button2_Click(object sender, EventArgs e) { num++; // you actually need only a one dimensional array string[] saNames = new string[13]; saNames[0] = txtUsername1.Text; saNames[1] = txtWorkName1.Text; saNames[2] = cbxManufac.Text; saNames[3] = cbxMachType.Text; saNames[4] = txtModel.Text; saNames[5] = txtSerial.Text; saNames[6] = txtCPU.Text; saNames[7] = cbxRAM.Text; saNames[8] = cbxOS.Text; saNames[9] = txtVersion.Text; saNames[10] = txtMcstOffice.Text; saNames[11] = txtRecomend.Text; saNames[12] = txtComments.Text; // Try to increment just the position in the file string startposition = "A" + num.toString(); oSheet.get_Range(startposition , "L1000").Value = saNames; } } 
 private void button2_Click(object sender, EventArgs e) { int _lastRow = oSheet.Range["A" + oSheet.Rows.Count].End[Microsoft.Office.Interop.Excel.XlDirection.xlUp].Row + 1; oSheet.Cells[_lastRow, 1] = txtUsername1.Text; oSheet.Cells[_lastRow, 2] = txtWorkName1.Text; oSheet.Cells[_lastRow, 3] = cbxManufac.Text; oSheet.Cells[_lastRow, 4] = cbxMachType.Text; oSheet.Cells[_lastRow, 5] = txtModel.Text; oSheet.Cells[_lastRow, 6] = txtSerial.Text; oSheet.Cells[_lastRow, 7] = txtCPU.Text; oSheet.Cells[_lastRow, 8] = cbxRAM.Text; oSheet.Cells[_lastRow, 9] = cbxOS.Text; oSheet.Cells[_lastRow, 10] = txtVersion.Text; oSheet.Cells[_lastRow, 11] = txtMcstOffice.Text; oSheet.Cells[_lastRow, 12] = txtRecomend.Text; oSheet.Cells[_lastRow, 13] = txtComments.Text; } 

我修好了:o