I have uploaded a file into server. How can I use c# to read the contents and display it. I used string builder to extract the content, and display it in a multiline textbox.
The code I used is :-
string[] readText = File.ReadAllLines(path);
StringBuilder strbuild = new StringBuilder();
foreach (string s in readText)
{
strbuild.Append(s);
strbuild.AppendLine();
}
txtPreview.Text = strbuild.ToString();
The problem with this is that, some kind of additional un-readable character are displayed at the top and bottom, maybe some kind of encrypted text. How to remove those charactes, and show only the contents ?
Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj);
doc.Activate();
string Doc_Content = doc.Content.Text;
string str = Doc_Content;
var words = str.Split(new char[] { ' ', ':', '\r', '\t' });
for (int i = 0; i < words.Length; i++)
{
string val1 = words[i].ToString();
}
UPDATE: I am using Microsoft Interop library, and I am able to show the contents of the word document into the multiline text box.
I created a string variable str to hold all content of the word file. And an array word[] to store the words. The problem I am facing now is :- Read the words. If the first word is "hello", I need to read the second and third word. If the first word is "hello" and second word is "world", I need to read the third and fourth words. Other wise, I need to read the first and second words. How can this be done?
.DOCXfile? If so it is a Package (zipped xml) so you cannot read it as text like that. You will have to use the OpenXml libraries within .NET. Here is a 'basics' tutorial codeproject.com/Articles/20998/…. There are some useful snippets in here too. stackoverflow.com/questions/1142830/…. NPOI is also an option npoi.codeplex.com (which also handles.doc) - CodeBeard