以编程方式在Excel中插入复选框

时间:2014-01-15 10:08:28

标签: c# excel checkbox docx

我有一个关于在C#中操作Excel环境的问题。我是一名初学程序员,我从未在与C#不同的环境中进行编程,因此,我请求避免使用VBA提示。

我的程序有一个函数,可以读出.docx文档的目录,将其转换为纯文本,然后将其复制到RichTextBox中,这样用户就可以看到一个例子。这就是它的样子。

After you click the "Into Excel" button, it gets copied to Excel,。 但是,这不是我完全想要的。我希望看起来like this。它目前所做的几乎完全相同,除了它根本没有复选框。只需将文本复制到Excel中。

在每一行之后是否有“foreach”Excel复选框? 到目前为止,我尝试在每行后插入“☐”符号。它有效,但它不可点击。 这是我的代码,如果它有任何帮助。

        #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

0 个答案:

没有答案