c# - 提取Word文档数据并插入SQL数据库

标签 c# sql asp.net scripting ms-word

Word文档示例

A 1. Name of House: Aasleagh Lodge
Townland: Srahatloe
Near: Killary Harbour, Leenane
Status/Public Access: maintained, private fishing lodge
Date Built: 1838-1850, burnt 1923, rebuilt 1928
Description: Large Victorian country house. Original house 6-bay, 2-storey, 3-bay section on right is higher; after fire house was reduced in size giving current three parallel- hipped roof bays. 
Associated Families: Lord Sligo; rented - Hon David Plunkett ; Capt W.E. and Constance Mary Phillips; James Leslie Wanklyn M.P. for Bradford; Walter H. Maudslay; Ernest Richard Hartley; Alice Marsh, Lord and Lady Brabourne; Western Fisheries Board; Inland Fisheries Ireland.

有没有办法插入标题后面的数据,例如在 word 文档中存在“Townland”的位置我希望将其后的数据插入到数据库中的列中,在本例中为“Srahatloe”。我想从 Word 文档中提取所有这些数据,它用于我正在构建的网站,所有信息都存储在 Word 文档中,但我需要将文本添加到数据库中,而不需要复制和粘贴,因为文档非常大(70,000 多个字)是否有我可以用来执行此操作的脚本?

源代码

var wordApp = new Microsoft.Office.Interop.Word.Application();
            var wordDoc = wordApp.Documents.Open(@"C:\Users\mhoban\Documents\Book.docx");
            var txt = wordDoc.Content.Text;
            var regex = new Regex(@"(Townland\: )(.+?)[\r\n]");
            var allMatches = regex.Matches(txt);
            foreach (Match match in allMatches)
            {
                var townValue = match.Groups[2].Value;

                // Insert values into database
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                SqlCommand com = new SqlCommand();

                com.CommandText = "INSERT INTO Houses (Townland) VALUES (@town)";

                com.Parameters.Add("@town", SqlDbType.NVarChar).SqlValue = townValue;

                com.Connection = con;

                con.Open();

                com.ExecuteNonQuery();

                con.Close();
            }

最佳答案

为 RegEx 呐喊。像这样的东西应该能让你工作:

var wordApp = new Microsoft.Office.Interop.Word.Application();
var wordDoc = wordApp.Documents.Open(pathToYourDocument);
var txt = wordDoc.Content.Text;
    var regex = new Regex(@"(Townland\: )(.+?)[\r\n]");
    var allMatches = regex.Matches(txt);
    foreach (Match match in allMatches)
    {
        var townValue = match.Groups[2].Value;
        //townValue now holds "Srahatloe"
        //do your magic
    }

关于c# - 提取Word文档数据并插入SQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892033/

相关文章:

sql - 跟踪 Rails 3 SQL 查询

SQL WHERE条件为列表,列类型为数组

asp.net - 在 ASP.NET MVC 中引用 Javascript 的正确方法?

c# - 将整数作为 varbinary 发送到 SQL 触发器

c# - 我可以通过 Lucene 在 Orchard 中搜索/索引自定义数据源吗?

c# - 有没有办法迭代 Unity/c# 中的通用值列表?

asp.net - ACL到底是什么意思? (分享点)

c# - 如何从 Azure 云服务的代码中获取服务名称?

mysql - 通过减去日期之间的值来更新列

c# - 为基于声明的安全性实现自定义属性