以编程方式在Excel中插入checkbox

我有一个关于在C#中操作Excel环境的问题。 我是一名初学者程序员,我从来没有编写过与C#不同的环境,因此,我希望避免VBA提示。

我的程序有一个读出.docx文件的目录,将其转换为纯文本,并将其复制到RichTextBox的function,因此用户可以看到一个示例。 这是它的样子。

单击“进入Excel”button后,它将被复制到Excel,。 但是,这不是我完全想要的。 我希望看起来像这样 。 它目前的function几乎是一样的,除了没有任何checkbox。 只是将文本复制到Excel中。

无论如何要在每行之后“foreach”Excelcheckbox吗? 到目前为止,我尝试在每行之后插入“☐”符号。 它可以工作,但不可点击。 这是我的代码,如果它有任何帮助。

#region Checklist private void btOpenDocx_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog(this) == DialogResult.OK) { tbFile.Text = openFileDialog.FileName; rtbText.Clear(); } DocxToText dtt = new DocxToText(tbFile.Text); string txt = ""; try { txt = dtt.ExtractText(); txt = Regex.Replace(txt, @"\t", " "); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } using (StreamWriter writer = new StreamWriter(Application.StartupPath + "\\sandwich.txt", true)) { writer.WriteLine(txt); File.SetAttributes(Application.StartupPath + "\\sandwich.txt", FileAttributes.Hidden); } List<string> textLines = new List<string>(); string[] lines = File.ReadAllLines(Application.StartupPath + "\\sandwich.txt"); bool read = false; int enter = 0; foreach (string line in lines) { if (line == "Inhoud") { read = true; enter = 0; } else if (line == "") { if (enter != 2) { enter++; } else { read = false; } } if (read) { if (line != "") { if (line == "Inhoud") { enter = 0; rtbText.AppendText(line + "\r\n"); textLines.Add(line); } else { enter = 0; rtbText.AppendText(line + " \r\n"); textLines.Add(line); } } } File.Delete(Application.StartupPath + "\\sandwich.txt"); } } private void btExtract_Click(object sender, EventArgs e) { if (rtbText.Text == "") { MessageBox.Show("Nothing to copy."); } else { //Extracted text gets copied into Excel Clipboard.SetText(rtbText.Text); Microsoft.Office.Interop.Excel.Application xlexcel; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlexcel = new Excel.Application(); xlexcel.Visible = true; //Add a new a workbook xlWorkBook = xlexcel.Workbooks.Add(misValue); //Set Sheet 1 as the sheet you want to work with xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //Set your range Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1]; CR.Select(); xlWorkSheet.Paste(CR, false); } } private void btReset_Click(object sender, EventArgs e) { tbFile.Clear(); rtbText.Clear(); } #endregion