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/