Hi! I used below code:
protected void Import(object sender, EventArgs e)
{
Application word = new Application();
Document doc = new Document();
string filePath = Server.MapPath("~/Files/Test.doc");
object missing = System.Type.Missing;
object fileName = filePath;
doc = word.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
if (temp != string.Empty)
data.Add(temp);
}
doc.Close(ref missing, ref missing, ref missing);
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("Id",typeof(int)),
new System.Data.DataColumn("Question"),
new System.Data.DataColumn("Answer1"),
new System.Data.DataColumn("Answer2"),
new System.Data.DataColumn("Answer3"),
new System.Data.DataColumn("Answer4"),
new System.Data.DataColumn("CorrectAnswer")
});
int totalQuestionCount = 0;
int rowNo = 1;
System.Data.DataRow dr = dt.NewRow();
for (int i = 0; i < data.Count; i++)
{
if (!data[i].Contains("$"))
{
dr[0] = rowNo;
dr[1] = data[i].Replace("@", "").Trim();
rowNo++;
}
else
{
int optionCount = 2;
foreach (string option in data[i].Split(';'))
{
if (!string.IsNullOrEmpty(option))
{
if (option.Contains("$"))
{
dr[optionCount] = option.Replace("$", "").Trim();
}
else if (option.Contains("©"))
{
dr[optionCount] = option.Replace("©", "").Trim();
dr[6] = option.Split(')')[1].Trim();
}
optionCount++;
}
}
}
if ((i - 1) % 2 == 0 && i != 0)
{
dt.Rows.Add(dr);
totalQuestionCount++;
dr = dt.NewRow();
}
}
((_Application)word).Quit();
gvQuestions.DataSource = dt;
gvQuestions.DataBind();
}
But I want read from ms word and insert into database.
@1.The capital of India.
$A) Dushanbe;$B) Moscow;©C) Delhi;$D) Kabul;
@2.The capital of Tajikistan.
©A) Dushanbe;$B) Moscow;$C) Delhi;$D) Kabul;
@3.The capital of Afganistan.
$A) Dushanbe;
$B) Moscow;
$C) Delhi;
©D) Kabul;
Output result:
Id
|
QuestionAnswer
|
1
|
@1.The capital of India. $A) Dushanbe;$B) Moscow;©C) Delhi;$D) Kabul;
|
2
|
@2.The capital of Tajikistan.©A) Dushanbe;$B) Moscow;$C) Delhi;$D) Kabul;
|
3
|
@3.The capital of Afganistan. $A) Dushanbe;$B) Moscow;$C) Delhi; ©D) Kabul;
|
Who is you can help me?