我的程序从Excel文档中读取数据(名称),并将它们显示在CheckListBox上,供用户选择其名称。然后,用户必须从一系列RadioButton中选择一种颜色。如何让程序在包含其名称的单元格旁边的单元格中写入他们选择的颜色?
以下是我的计划摘录:
public void ColorChoice()
{
ColorGreen.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
ColorYellow.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
ColorRed.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
ColorBlue.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton Color = sender as RadioButton;
if (Color == null)
{
MessageBox.Show("No Color Selected");
return;
}
// Ensure that the RadioButton.Checked property
// changed to true.
if (Color.Checked)
{
// Keep track of the selected RadioButton by saving a reference
// to it
string SelectedColor = Color.Text;
//This is where it will write the color to the Excel File
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelBook = ExcelApp.Workbooks.Open(@"C:\\Users\\Blayne\\Desktop\\Prototype3\\Prototype1\\Prototype1\\Save Folder\\Students.xlsx", 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false, 1, 0);
Excel._Worksheet ExcelSheet = (Excel._Worksheet)ExcelBook.Sheets[1];
}
}
public void ImportStudentDetails()
{
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelBook = ExcelApp.Workbooks.Open(@"C:\\Users\\Blayne\\Desktop\\Prototype3\\Prototype1\\Prototype1\\Save Folder\\Students.xlsx", 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false, 1, 0);
Excel._Worksheet ExcelSheet = (Excel._Worksheet)ExcelBook.Sheets[1];
NameListBox.CheckOnClick = true;
NameListBox.Name = "NameListBox";
NameListBox.TabIndex = 1;
NameListBox.SelectionMode = SelectionMode.One;
NameListBox.ThreeDCheckBoxes = true;
NameListBox.DisplayMember = "Name";
NameListBox.Items.Add(ExcelSheet.Cells[2, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[3, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[4, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[5, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[6, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[7, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[8, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[9, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[10, 1].text);
NameListBox.Items.Add(ExcelSheet.Cells[11, 1].text);
Controls.Add(NameListBox);
}
public class SaveName
{
public static void SaveNameData(object obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
}
public class Data
{
private string name1;
public string Name1
{
get { return name1; }
set { name1 = value; }
}
}
private void StartButton_Click(object sender, EventArgs e)
{
var SNames = new List<string>();
foreach (var c in NameListBox.CheckedItems)
{
SNames.Add(c.ToString());
}
string SName = string.Join(",", SNames.ToArray());
try
{
Data info = new Data();
info.Name1 = SName;
Login.SaveName.SaveNameData(info, "Name.xml");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Hide();
Form2 f2 = new Form2();
f2.Closed += (s, args) => this.Close();
f2.Show();
}
public static void SaveStudentData(object data, string directory)
{
using (TextWriter writer = new StreamWriter(directory, true))
{
XmlSerializer sr = new XmlSerializer(data.GetType());
sr.Serialize(writer, data);
writer.Close();
}
}
忽略我的程序将选择的名称写入xml文件,因为它可以将名称转移到另一个表单。 提前谢谢。
答案 0 :(得分:0)
这个答案将帮助您获得当前的单元格:
How to get current or focussed cell value in Excel worksheet using C#
只需将1添加到rng.Column,它将为您提供当前单元格旁边的单元格。
然后您可以将所需的颜色指定为文本或背景: