Note that there are some explanatory texts on larger screens.

plurals
  1. POC# write values of combobox to excel rows
    primarykey
    data
    text
    <p>I have a windows form application where a user inputs their name (First and Last name) into a combobox. I have a add button that adds whatever value they type in to the combobox and lists it below. <strong>What i'm trying to do is take the names listed in the combobox and input them into excel in separate rows.</strong> I also have 2 columns in the excel file with "First name" and "Last Name" so the name in the combo box needs to be split. </p> <p>here is my code for my excel: </p> <pre><code>if (!File.Exists(@"C:\gradsheet.xlsx")) { File.WriteAllBytes(@"C:\gradesheet.xlsx", Properties.Resources.gradesheet); } // if there is no title selected if (String.IsNullOrEmpty(cboSelect.Text)) { MessageBox.Show("Please make sure a category is selected.", "No Subject Found Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } object oOpt = System.Reflection.Missing.Value; Excel.Application excelApp = new Excel.ApplicationClass(); Excel.Workbook wBook; string myPath = @"C:\gradesheet.xlsx"; wBook = excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet wSheet = (Excel.Worksheet)wBook.Worksheets[1]; wBook.Worksheets.get_Item(1);//get worksheet number wSheet.Name = cboSelect.Text;//define name //put name in excel(THIS IS WHERE I NEED THE COMBOBOX ITEMS IN EXCEL) excelApp.Cells[8, 1] = firstName; excelApp.Cells[8, 2] = lastName; //Subject Name to cell excelApp.Cells[5, 1] = cboSelect.Text; //these are some cleanup calls that I found in another example.. excelApp.AlertBeforeOverwriting = false; excelApp.DisplayAlerts = false; excelApp.Save(); excelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); GC.Collect(); GC.WaitForPendingFinalizers(); DialogResult result; result = MessageBox.Show("Would you like to open the gradesheet?", "gradesheet", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { string gradesheet = @"C:\gradesheet.xlsx"; Process.Start(gradesheet); } } </code></pre> <p>Here is my code that splits the first and last name but im not sure how to make it do it for every item in the combobox: </p> <pre><code>string fullName = cboStudent.Text; string firstName; string lastName; string[] parts = fullName.Split(new string[] { ", " }, StringSplitOptions.None); if (parts.Length == 1) { parts = fullName.Split(' '); if (parts.Length == 1) { lastName = fullName; firstName = ""; } else { lastName = parts[1]; firstName = parts[0]; } } else { lastName = parts[0]; firstName = parts[1]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload