解析word文档并插入数据库

时间:2013-02-13 06:32:08

标签: asp.net c#-4.0

我有一个单词文件:

问题1:问题的内容 答案A: 答案B: 答案C: 答案D:

我想读取此文件并将数据插入我的数据库中 例如问题1 将进入问题列,相应的答案将进入答案列...... ...

string strPath = Server.MapPath("~/Test.doc");
// Request.PhysicalApplicationPath + "\\Test.doc"; 
FileStream fStream = new FileStream (strPath, FileMode.Open, FileAccess.Read); 
StreamReader sReader = new StreamReader(fStream); 
//TextBox2.Text = sReader.ReadToEnd(); 
string data1 = sReader.ReadToEnd(); 
sReader.Close(); 
Response.Write(data1);

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

string strPath = Server.MapPath("~/Test.doc");
using (FileStream fs = new FileStream(strPath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                {
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        if (line.Contains("question"))
                        {
                            //read content here
                        }
                    }
                }
            }