private void MenuItemExportToCsv_Click(object sender, EventArgs e)
        {
            try {
                HtmlElement table = null;
                try {
                    table = this.webBrowser.Document.GetElementById(this.tablename);
                    if (table == null)
                    {
                        throw new Exception();
                    }
                }
                catch (System.Exception ex) {
                    string fmt = ResourceCulture.GetString("MSGBOX_TXT_TABLE_NOT_FOUND");
                    string txt = string.Format(fmt, this.tablename);
                    this.msgbox.ShowMessageDialog(txt, "error");
                    if (thr != null)
                    {
                        thr.Append(ex.StackTrace);
                    }
                    return;
                }

                SaveFileDialog dlg = new SaveFileDialog();
                dlg.InitialDirectory = this.initdir;
                dlg.Filter           = "csv file|*.csv";
                dlg.RestoreDirectory = false;
                dlg.FilterIndex      = 1;
                dlg.Title            = ResourceCulture.GetString("MENU_TOOL_EXPORT_TO_CSV");
                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string filename = initdir = dlg.FileName;
                if (!Regex.IsMatch(dlg.FileName.ToLower(), @".csv$"))
                {
                    filename = initdir += ".csv";
                }

                HtmlElement script = this.webBrowser.Document.GetElementById("_#@#_embedded_stript_id_check_if_element_visible");
                if (script == null)
                {
                    script = this.webBrowser.Document.CreateElement("script");
                    script.SetAttribute("type", "text/javascript");
                    script.SetAttribute("id", "_#@#_embedded_stript_id_check_if_element_visible");
                    script.SetAttribute("text", this.embededScript);
                    this.webBrowser.Document.Body.AppendChild(script);
                }

                HtmlElementCollection trs = table.GetElementsByTagName("tr");

                CsvStreamReader csv = new CsvStreamReader();
                csv.OpenCsvFile(filename);

                object[] args     = new object[1];
                string[] tagnames = { "a", "b", "p", "font", "h1", "h2", "h3", "h4", "h5", "h6", "input", "select", "span", "textarea" };

                foreach (HtmlElement tr in trs)
                {
                    if (!tr.Parent.Parent.Equals(table))
                    {
                        continue;
                    }
                    HtmlElementCollection tds = tr.GetElementsByTagName("td");
                    LinkedList <string>   lls = new LinkedList <string>();
                    foreach (HtmlElement td in tds)
                    {
                        if (!td.Parent.Equals(tr))
                        {
                            continue;
                        }

                        string id    = td.GetAttribute("id");
                        string newId = "#@#_temp_id_123456";
                        if (id != null)
                        {
                            newId = id + "_#@#_temp_id_123456";
                        }
                        td.SetAttribute("id", newId);
                        args[0] = newId;
                        object obj = this.webBrowser.Document.InvokeScript("__getInnerText", args);
                        td.SetAttribute("id", id);

                        string   innerText = (obj == null ? "" : obj.ToString());
                        string[] texts     = innerText.Split('\t');
                        for (int j = 0; j < texts.Length; j++)
                        {
                            lls.AddLast(texts[j]);
                        }
                    }
                    csv.WriteCsvFile(lls);
                }
                csv.CloseCsvFile();
            }
            catch (System.Exception ex) {
                string txt = ResourceCulture.GetString("MSGBOX_TXT_EXPORT_FAIL");
                this.msgbox.ShowMessageDialog(txt, "error");
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
            }
        }
        private void MenuItemImportFromCsv_Click(object sender, EventArgs e)
        {
            try {
                HtmlElement table = null;
                try {
                    table = this.webBrowser.Document.GetElementById(this.tablename);
                    if (table == null)
                    {
                        throw new Exception();
                    }
                }
                catch (System.Exception ex) {
                    string fmt = ResourceCulture.GetString("MSGBOX_TXT_TABLE_NOT_FOUND");
                    string txt = string.Format(fmt, this.tablename);
                    this.msgbox.ShowMessageDialog(txt, "error");
                    if (thr != null)
                    {
                        thr.Append(ex.StackTrace);
                    }
                    return;
                }

                OpenFileDialog ofdlg = new OpenFileDialog();
                ofdlg.InitialDirectory = this.initdir;
                ofdlg.Filter           = "csv file|*.csv";
                ofdlg.RestoreDirectory = false;
                ofdlg.FilterIndex      = 1;
                ofdlg.Title            = ResourceCulture.GetString("MENU_TOOL_IMPORT_FROM_CSV");
                if (ofdlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string filename = ofdlg.FileName;
                initdir = filename;

                HtmlElement script = this.webBrowser.Document.GetElementById("_#@#_embedded_stript_id_check_if_element_visible");
                if (script == null)
                {
                    script = this.webBrowser.Document.CreateElement("script");
                    script.SetAttribute("type", "text/javascript");
                    script.SetAttribute("id", "_#@#_embedded_stript_id_check_if_element_visible");
                    script.SetAttribute("text", this.embededScript);
                    this.webBrowser.Document.Body.AppendChild(script);
                }

                object[] args = new object[1];

                HtmlElementCollection trs = table.GetElementsByTagName("tr");
                CsvStreamReader       csr = new CsvStreamReader(filename);

                for (int i = 0, m = 0; i < trs.Count && m < csr.RowCount; i++)
                {
                    if (!trs[i].Parent.Parent.Equals(table))   // trs[i].Parent.TagName == "TBODY";
                    {
                        continue;
                    }
                    bool validRow             = false;
                    HtmlElementCollection tds = trs[i].GetElementsByTagName("td");
                    for (int j = 0, n = 0; j < tds.Count && n < csr.ColCount; j++)
                    {
                        HtmlElementCollection hec = tds[j].Children;
                        if (hec == null)
                        {
                            continue;
                        }
                        for (int k = 0; k < hec.Count; k++)
                        {
                            // Only input, select and textarea can be filled with value.
                            string tagname = hec[k].TagName.ToLower();
                            if (tagname != "input" && tagname != "select" && tagname != "textarea")
                            {
                                continue;
                            }
                            // If the element is a input, then the type of it must be text.
                            if (tagname == "input" && hec[k].GetAttribute("type").ToLower() != "text")
                            {
                                continue;
                            }
                            // Check if the object is visible.
                            string id    = hec[k].GetAttribute("id");
                            string newId = "#@#_temp_id_123456";
                            if (id != null)
                            {
                                newId = id + "_#@#_temp_id_123456";
                            }
                            args[0] = newId;
                            hec[k].SetAttribute("id", newId);
                            object obj = this.webBrowser.Document.InvokeScript("__isVisible", args);
                            hec[k].SetAttribute("id", id);

                            if (obj == null || obj.ToString() == "none")
                            {
                                continue;
                            }

                            // Elements must be editable.
                            if (!hec[k].Enabled)
                            {
                                ++n;
                                continue;
                            }
                            // Copy text to elements' inner area.
                            if (tagname == "input" || tagname == "textarea")
                            {
                                hec[k].InnerText = csr[m + 1, n + 1].ToString();
                                validRow         = true;
                            }
                            else  // select
                            {
                                foreach (HtmlElement opt in hec[k].GetElementsByTagName("option"))
                                {
                                    string text = csr[m + 1, n + 1].ToString();
                                    validRow = true;
                                    if (opt.InnerText == text)
                                    {
                                        opt.SetAttribute("selected", "selected");
                                        break;
                                    }
                                }
                            }
                            ++n;
                        }
                    }
                    if (validRow)
                    {
                        ++m;
                    }
                }
            }
            catch (System.Exception ex) {
                string txt = ResourceCulture.GetString("MSGBOX_TXT_IMPORT_FAIL");
                this.msgbox.ShowMessageDialog(txt, "error");
                if (thr != null)
                {
                    thr.Append(ex.StackTrace);
                }
            }
        }