Exemplo n.º 1
0
        private void ImportWordFile()
        {
            ImportLog.Clear();
            String filename = WordFilePath.Text;

            if (string.IsNullOrWhiteSpace(filename))
            {
                MessageBox.Show("请先选择导入文件");
                return;
            }

            //载入Word文档
            Document document = new Document(filename);

            Section section = document.Sections[0];
            int     i       = 1;

            foreach (Spire.Doc.Table table in section.Tables)
            {
                ImportLog.AppendText("导入表格" + i + ",自动查找匹配模式开始...\r\n");
                string[] cls        = getDocColumsFromDocTable(table);
                int      matchindex = getMatchIndex(cls);
                if (matchindex < 0)
                {
                    ImportLog.AppendText("查找匹配模式失败,请添加合适的匹配模式\r\n");
                }
                else
                {
                    ImportLog.AppendText("查找匹配模式成功,匹配模式为:" + MatchListBox.Items[matchindex].ToString() + "\r\n");

                    ImportTableData(matchindex, table);
                }
                i++;
            }


            //Spire.Doc.Table table = section.Tables[0] as Spire.Doc.Table;
            ////实例化StringBuilder类
            //StringBuilder sb = new StringBuilder();

            ////遍历表格中的段落并提取文本
            //foreach (TableRow row in table.Rows)
            //{
            //    foreach (TableCell cell in row.Cells)
            //    {
            //        foreach (Paragraph paragraph in cell.Paragraphs)
            //        {
            //            sb.AppendLine(paragraph.Text);
            //        }
            //    }
            //}
            //ImportLog.Text = sb.ToString();
        }
Exemplo n.º 2
0
        private void ImportTableData(int matchindex, Spire.Doc.Table table)
        {
            ImportLog.AppendText("开始导入数据\r\n");
            string strInsertHead = getinsertsqlhead(matchindex);

            string[] insertsql = new string[ColumBox.Items.Count];
            for (int i = 0; i < ColumBox.Items.Count - 1; i++)
            {
                insertsql[i] = "''";
            }
            string[]   cls = getDocColumsFromDocTable(table);
            List <int> AccesscolumsIndex = getAccesscolumsIndex(matchindex, cls);

            string shortFileName = WordFilePath.Text.Substring(WordFilePath.Text.LastIndexOf('\\') + 1);

            for (int i = 1; i < table.Rows.Count; i++)
            {
                string strInsert = strInsertHead;

                for (int j = 0; j < AccesscolumsIndex.Count; j++)
                {
                    if (AccesscolumsIndex[j] >= 0)
                    {
                        string cellcontent = "";
                        foreach (Paragraph paragraph in table.Rows[i].Cells[j].Paragraphs)
                        {
                            cellcontent += paragraph.Text + " ";
                        }

                        insertsql[AccesscolumsIndex[j]] = "'" + cellcontent.Trim() + "'";
                    }
                }
                for (int k = 0; k < ColumBox.Items.Count; k++)
                {
                    strInsert += insertsql[k] + ",";
                }

                strInsert += "'" + shortFileName + "' )";

                bool flag = MyDataSet.Add(strInsert);
                if (flag)
                {
                    ImportLog.AppendText("导入第" + i + "条数据成功," + strInsert + "\r\n");
                }
                else
                {
                    ImportLog.AppendText("导入第" + i + "条数据失败," + strInsert + "\r\n");
                }
            }
            ImportLog.AppendText("导入数据完成\r\n");
            freshTableTree();
        }