C#逐行填充excel单元格

嘿,我正在试图创build这个程序,将input发送到Excel文件,并坚持如何将input存储在下一行。

到目前为止,我可以从我的文本框中获取string到excel下的第二行,但是我想让用户能够添加多行。

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; // -- Workstation Information -- 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[500, 500]; saNames[0, 0] = txtUsername1.Text; saNames[0, 1] = txtWorkName1.Text; saNames[0, 2] = cbxManufac.Text; saNames[0, 3] = cbxMachType.Text; saNames[0, 4] = txtModel.Text; saNames[0, 5] = txtSerial.Text; saNames[0, 6] = txtCPU.Text; saNames[0, 7] = cbxRAM.Text; saNames[0, 8] = cbxOS.Text; saNames[0, 9] = txtVersion.Text; saNames[0, 10] = txtMcstOffice.Text; saNames[0, 11] = txtRecomend.Text; saNames[0, 12] = txtComments.Text; oSheet.get_Range("A2", "L1000").Value2 = saNames; oWB.SaveAs("discoSurvey"); 

这是我的保存button目前的样子。 我想创build一个添加button,让他们不断添加信息到这个Excel工作表。 我如何将他们移动到下一行?

嗯,

你需要findExcel文件中的第一个空行来添加你的下一行,对吧?

所以你可以使用build议的for循环search下一个空行,然后使用find的rowindex来插入你的值。

我刚才看得更清楚

  string[,] saNames = new string[500, 500]; saNames[0, 0] = txtUsername1.Text; saNames[0, 1] = txtWorkName1.Text; saNames[0, 2] = cbxManufac.Text; saNames[0, 3] = cbxMachType.Text; saNames[0, 4] = txtModel.Text; saNames[0, 5] = txtSerial.Text; saNames[0, 6] = txtCPU.Text; saNames[0, 7] = cbxRAM.Text; saNames[0, 8] = cbxOS.Text; saNames[0, 9] = txtVersion.Text; saNames[0, 10] = txtMcstOffice.Text; saNames[0, 11] = txtRecomend.Text; saNames[0, 12] = txtComments.Text; 

虽然我不喜欢500×500数组 – 你只需要在你的数组中查询null,并使用该索引来插入值。 但是你需要保持你的数组不能重新定义它所有的时间..把它放在一个字段也许。