【问题标题】:How to write to Excel cell next to selected cell?如何写入所选单元格旁边的 Excel 单元格?
【发布时间】:2016-09-06 04:21:35
【问题描述】:

我的程序从 Excel 文档中读取数据(姓名)并将它们显示在 CheckListBox 上,供用户选择他们的姓名。然后用户必须从一系列 RadioButtons 中选择一种颜色。如何让程序在包含其姓名的单元格旁边的单元格中写入他们选择的颜色?

以下是我的程序的摘录:

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 文件,因为它可以将名称传输到另一个表单。 提前致谢。

【问题讨论】:

    标签: c# excel office-interop


    【解决方案1】:

    此答案将帮助您获取当前单元格:

    How to get current or focussed cell value in Excel worksheet using C#

    只需将 1 添加到 rng.Column,它就会为您提供当前单元格旁边的单元格。

    然后你可以为文本或背景分配你想要的颜色:

    【讨论】:

    • 我使用您建议的代码编辑了程序并实现了它。但是,该程序似乎没有拾取活动单元格,因为它只是写入第一行的第二列。
    • 听起来 rgn.row 属性没有赋值?
    • 也许,我相信这可能是 ExcelApp.ActiveCell 的事实;没有选择用户从 CheckListBox 中选择其名称的单元格。
    • 如果你调试,你能看到rgn.row和rgn.column被分配了吗?
    • 我运行了调试,我可以确认它们都在接收值。更具体地说, rgn.row 总是接收 1 而 rgn.column 总是接收 2(这很好)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2018-06-04
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多