Reputation: 165
I have a form that looks like this. My goal is to have the 13 textboxes/comboboxes to be added into an excel sheet. Also, I have 2 buttons below this which didnt show up in the picture called 'Save' and 'Add' - Save is pretty self explanatory. Add is to save the input from the UserForm and put it into the excel row with the appropriate headers, Clear the UserForm, and finally, prep the user to input data again that will go into the line below. Here is my code so far within the add 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;
}
The issue I am having is that my add button doesn't save the input into the excel cells, it disappears, I am not sure what I am doing wrong so I decided to ask people who perhaps have come across this issue before.
Also I seem to start at row "A3" instead of "A2" a little odd
Upvotes: 0
Views: 1054
Reputation: 165
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;
}
I fixed it :o
Upvotes: 1
Reputation: 23732
I am not an expert in this excel acrobatics but you could try this
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;
}
}
Upvotes: 1