C#将combobox的值写入excel行

C#将combobox的值写入excel行,c#,C#,我有一个windows窗体应用程序,其中用户将自己的姓名(名字和姓氏)输入到组合框中。我有一个添加按钮,可以将他们键入的任何值添加到组合框中,并在下面列出我试图做的是获取组合框中列出的名称,并将它们以单独的行输入excel。我在excel文件中还有两列,分别是“名字”和“姓氏”,因此组合框中的名称需要拆分 以下是我的excel代码: if (!File.Exists(@"C:\gradsheet.xlsx")) { File.WriteAllBytes(@

我有一个windows窗体应用程序,其中用户将自己的姓名(名字和姓氏)输入到组合框中。我有一个添加按钮,可以将他们键入的任何值添加到组合框中,并在下面列出我试图做的是获取组合框中列出的名称,并将它们以单独的行输入excel。我在excel文件中还有两列,分别是“名字”和“姓氏”,因此组合框中的名称需要拆分

以下是我的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);
        }


    }
下面是我的代码,它将名字和姓氏分开,但我不知道如何使组合框中的每个项目都这样做:

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];
        }

听起来在您的情况下,您想要组合框中的每个项目,所以请使用组合框上的“项目”属性

foreach (object name in cboStudent.Items)
{
      ...
}
如果您只需要项目列表中的选定项目,可以使用“SelectedItem”属性

或者

由于您可能希望将每个项目添加到工作簿中的不同单元格中,因此可以使用for循环,这样您就可以为该单元格使用整数

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; 

}
for(int x=0;x
好的,我解决了,这是代码,谢谢你们的帮助

//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;
            }
//学生姓名
对于(int x=0;x
不管有多少帖子说使用com interop操作office文档是一个多么糟糕的主意,总有人在做这件事。@Brook为什么,出于好奇?@Brook…这可能是个糟糕的主意,但编程却出人意料地简单。我坦率地承认,它有它的缺点,但也有它的优点。边界6……问题到底是什么?它不清楚(至少对我来说)您在这里想要完成什么。@Yatrix:因为它需要安装excel(并获得许可),因为它启动了一个全新的excel实例,因为这些excel实例通常很难正常关闭,留给您一台运行着15个excel.exe实例的机器,最后,也是最重要的,因为还有更多的轻量级选项@我的问题是如何在组合框中获取所有值,并将每个值输出到excel中的单独单元格中。例如,用户输入3个名称“John Do”、“John Smith”和“Chuck Norris”。我想让这些名字进入单元格A8、A9和A10,依此类推,看看他们输入了多少个名字。如果可能的话,我需要将名字和姓氏分开,第一个名字在A列,第二个名字在B列。正确,我想要组合框中的所有项目,但如何将它们分别发送到各自的行中?我想我需要excel.range之类的东西。@gene S,你的第二个循环可以工作,但是,它只是将当前选定的项目添加到每个单元格中,而不是单独添加到每个单元格中。好的,我的问题是我在原始问题的第二个代码框中使用的拆分代码。它仅在组合框中获取当前选定项的名称,并仅拆分该名称。我需要它来拆分组合框中的每个名称。您知道如何拆分组合框中的所有项目,而不是仅使用字符串fullname=cboStudent.text;