C# 将组合框的值写入 Excel 行
本文关键字:Excel 组合 | 更新日期: 2023-09-27 18:35:51
我有一个Windows表单应用程序,用户在其中将他们的名字(名字和姓氏)输入到组合框中。我有一个添加按钮,可以将他们键入的任何值添加到组合框中,并在下面列出。我要做的是获取组合框中列出的名称,并将它们以单独的行输入到 excel 中。我在 excel 文件中还有 2 列"名字"和"姓氏",因此组合框中的名称需要拆分。
这是我的 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];
}
好的,我解决了它,这是代码,谢谢你们的帮助!
//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;
}
听起来在您的情况下,您希望组合框中的每个项目,因此请使用组合框上的"项目"属性...
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;
}