Hi PRA,
Check the example.
Word file Content
1. The capital of Tajikistan
#A)Dushanbe $B)Toshkand $C)Moscow $D)Tokyo $E)Minsk
2. The capital of Uzbekistan
$A)Dushanbe #B)Toshkand $C)Moscow $D)Tokyo $E)Minsk
3. The capital of Russia
$A)Dushanbe $B)Toshkand #C)Moscow $D)Tokyo $E)Minsk
4. The capital of Japan
$A)Dushanbe $B)Toshkand $C)Moscow #D)Tokyo $E)Minsk
5. The capital of Belorussia
$A)Dushanbe $B)Toshkand $C)Moscow $D)Tokyo #E)Minsk
Code
C#
public partial class Form1 : Form
{
OpenFileDialog ofd = new OpenFileDialog();
System.Data.DataTable dt = new System.Data.DataTable();
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
ofd.Filter = "All files(*.doc,*.rtf,*.docx)|*.doc;*.rtf;*.docx";
if (ofd.ShowDialog() != DialogResult.OK)
{
}
else
{
object filePath = ofd.FileName;
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
doc = app.Documents.Open(ref filePath, ref missing, ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
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);
dt.Columns.AddRange(new System.Data.DataColumn[]
{
new System.Data.DataColumn("QuestionId",typeof(int)),
new System.Data.DataColumn("QuestionDescription"),
new System.Data.DataColumn("OptionOne"),
new System.Data.DataColumn("OptionTwo"),
new System.Data.DataColumn("OptionThree"),
new System.Data.DataColumn("OptionFour"),
new System.Data.DataColumn("OptionFive"),
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++)
{
string type = string.Empty;
if (data[i].Contains("$") || data[i].Contains("#"))
{
type = "option";
if (i != 0)
{
if ((!data[i - 1].Contains("@")))
{
type = "new line option";
}
}
}
else
{
type = "question";
}
int optionCount = 2;
switch (type)
{
case "question":
dr[0] = rowNo;
dr[1] = data[i].Replace("@", "").Trim();
rowNo++;
break;
case "option":
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++;
}
}
break;
case "new line option":
int colIndex = 2;
foreach (string option in data[i].Split(new char[] { '#', '$' }))
{
if (!string.IsNullOrEmpty(option))
{
dr[colIndex] = option.Trim();
colIndex++;
if (data[i].Contains("#"))
{
for (int k = 0; k < data[i].Split('$').Length; k++)
{
if (data[i].Split('$')[k].Contains("#"))
{
switch (k)
{
case 0:
dr[7] = "OptOne";
break;
case 1:
dr[7] = "OptTwo";
break;
case 2:
dr[7] = "OptThree";
break;
case 3:
dr[7] = "OptFour";
break;
case 4:
dr[7] = "OptFive";
break;
default:
break;
}
}
}
}
}
}
break;
}
if (i != 0 && !data[i - 1].Contains("@"))
{
bool isNull = false;
for (int x = 0; x < dr.ItemArray.Length; x++)
{
object item = dr.ItemArray[x];
if (item == null || item == DBNull.Value)
{
isNull = true;
break;
}
}
if (!isNull)
{
dt.Rows.Add(dr);
totalQuestionCount++;
dr = dt.NewRow();
}
}
}
dGV.DataSource = dt;
}
}
private void btnImport_Click(object sender, EventArgs e)
{
MessageBox.Show(dt.Rows[0][0].ToString() + " " + dt.Rows[0][1].ToString() + " " + dt.Rows[0][2].ToString() + " " + dt.Rows[0][3].ToString());
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
VB.Net
Public Partial Class Form1
Inherits Form
Private ofd As OpenFileDialog = New OpenFileDialog()
Private dt As System.Data.DataTable = New System.Data.DataTable()
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
ofd.Filter = "All files(*.doc,*.rtf,*.docx)|*.doc;*.rtf;*.docx"
If ofd.ShowDialog() <> DialogResult.OK Then
Else
Dim filePath As Object = ofd.FileName
Dim app As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
Dim doc As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document()
Dim [readOnly] As Object = False
Dim isVisible As Object = True
Dim missing As Object = System.Reflection.Missing.Value
doc = app.Documents.Open(filePath, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
Dim data As List(Of String) = New List(Of String)()
For i As Integer = 0 To doc.Paragraphs.Count - 1
Dim temp As String = doc.Paragraphs(i + 1).Range.Text.Trim()
If temp <> String.Empty Then data.Add(temp)
Next
doc.Close(missing, missing, missing)
dt.Columns.AddRange(New System.Data.DataColumn() {New System.Data.DataColumn("QuestionId", GetType(Integer)), New System.Data.DataColumn("QuestionDescription"), New System.Data.DataColumn("OptionOne"), New System.Data.DataColumn("OptionTwo"), New System.Data.DataColumn("OptionThree"), New System.Data.DataColumn("OptionFour"), New System.Data.DataColumn("OptionFive"), New System.Data.DataColumn("CorrectAnswer")})
Dim totalQuestionCount As Integer = 0
Dim rowNo As Integer = 1
Dim dr As System.Data.DataRow = dt.NewRow()
For i As Integer = 0 To data.Count - 1
Dim type As String = String.Empty
If data(i).Contains("$") OrElse data(i).Contains("#") Then
type = "option"
If i <> 0 Then
If (Not data(i - 1).Contains("@")) Then
type = "new line option"
End If
End If
Else
type = "question"
End If
Dim optionCount As Integer = 2
Select Case type
Case "question"
dr(0) = rowNo
dr(1) = data(i).Replace("@", "").Trim()
rowNo += 1
Case "option"
For Each [option] As String In data(i).Split(";"c)
If Not String.IsNullOrEmpty([option]) Then
If [option].Contains("$") Then
dr(optionCount) = [option].Replace("$", "").Trim()
ElseIf [option].Contains("#") Then
dr(optionCount) = [option].Replace("#", "").Trim()
dr(6) = [option].Split(")"c)(1).Trim()
End If
optionCount += 1
End If
Next
Case "new line option"
Dim colIndex As Integer = 2
For Each [option] As String In data(i).Split(New Char() {"#"c, "$"c})
If Not String.IsNullOrEmpty([option]) Then
dr(colIndex) = [option].Trim()
colIndex += 1
If data(i).Contains("#") Then
For k As Integer = 0 To data(i).Split("$"c).Length - 1
If data(i).Split("$"c)(k).Contains("#") Then
Select Case k
Case 0
dr(7) = "OptOne"
Case 1
dr(7) = "OptTwo"
Case 2
dr(7) = "OptThree"
Case 3
dr(7) = "OptFour"
Case 4
dr(7) = "OptFive"
Case Else
End Select
End If
Next
End If
End If
Next
End Select
If i <> 0 AndAlso Not data(i - 1).Contains("@") Then
Dim isNull As Boolean = False
For x As Integer = 0 To dr.ItemArray.Length - 1
Dim item As Object = dr.ItemArray(x)
If item Is Nothing OrElse item = DBNull.Value Then
isNull = True
Exit For
End If
Next
If Not isNull Then
dt.Rows.Add(dr)
totalQuestionCount += 1
dr = dt.NewRow()
End If
End If
Next
dGV.DataSource = dt
End If
End Sub
Private Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show(dt.Rows(0)(0).ToString() & " " & dt.Rows(0)(1).ToString() & " " & dt.Rows(0)(2).ToString() & " " & dt.Rows(0)(3).ToString())
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.Close()
End Sub
End Class
Screenshot