Reputation: 387
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. What i'm trying to do is take the names listed in the combobox and input them into excel in separate rows. 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.
here is my code for my excel:
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);
}
}
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:
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];
}
Upvotes: 0
Views: 1392
Reputation: 387
OK i solved it here is the code, Thanks for your help guys !
//Student Names
for (int x = 0; x < cboStudent.Items.Count; x++)
{
string fullName = cboStudent.Items[x] as string;
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];
}
excelApp.Cells[8 + x, 1] = firstName;
excelApp.Cells[8 + x, 2] = lastName;
}
Upvotes: 1
Reputation: 2793
Sounds like in your case you want every item in the combobox you so use the the "Items" property on the combobox...
foreach (object name in cboStudent.Items)
{
...
}
If you only want the selected item from a list of items you can use the "SelectedItem" property.
Or...
Since you may want to add each item to a different cell in the workbook you can use the for loop so you have an integer number to use for the cell as well...
for (int x = 0; x < comboBox1.Items.Count; x++)
{
object name = comboBox1.Items[x];
// Split name here
excelApp.Cells[8+x, 1] = firstName;
excelApp.Cells[8+x, 2] = lastName;
}
Upvotes: 0