对于大文本内容文件,单词到记事本转换的换行符问题 [英] Line break issue for word to notepad conversion for large text content file

查看:76
本文介绍了对于大文本内容文件,单词到记事本转换的换行符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文本内容大.doc / .docx时,每行后都没有换行符。



假设行是:



WORD文件:



a对于那些行业或资本目前从事这项工作的人来说,这是一笔巨大的永久性损失。

突然失去就业,即使是进口八万两千头烟草的船只,也就是超过英国消费的
,可能一个人感觉非常明智。这是商业系统所有规定的不幸影响!他们不仅将非常危险的疾病引入身体政治状态,而且通常很难补救的疾病,而不会发生这种情况。



NOTEPAD文件显示为:



a对于那些行业或资本目前从事这项工作的大部分人来说,这是一笔巨大的永久性损失。即使是进口八万两千头烟草的船只突然失去工作,这些烟草超过了英国的消费量,也可能感觉非常合理。这是商业体系所有规定的不幸影响!他们不仅将非常危险的疾病引入身体的政治状态,而且通常很难补救的疾病,而不会发生这种情况。



我尝试了什么:



private void processWordToNotepad()

{

string strNotepadPath;

string strFileExtension;





lblStatus.Text =Working ..;

int FilesCount = 0,TotalFiles = 0;



if(txtSource.Text.Length == 0 || txtDestination.Text.Length == 0)

{

lblStatus.Text =错误;

MessageBox.Show(请选择源和目标文件文件夹!,错误!,MessageBoxButtons .OK,MessageBoxIcon.Exclamation);

lblStatus.Text =Ready ..;

return;

}



if(FileNames == null)

{

MessageBox.Show(没有要处理的文件。,错误,MessageBoxButtons.OK,MessageBoxIcon.Exclamation) ;

lblStatus.Text =准备好;

返回;

}





if(dpSaveAs.SelectedIndex == -1)

{

MessageBox.Show(选择另存为类型,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);

返回;

}

其他

{

strFileExtension = dpSaveAs.Text;

}



试试

{

strFileExtension = dpSaveAs.SelectedItem.ToString();

TotalFiles = FileNames.Count();

myWord.Application app = new myWord .Application();



foreach(文件名中的字符串strFileName)

{

string strText;

lblStatus.Text =处理+ FilesCount ++ +of+ TotalFiles +files ..;

strFileExtension = dpSaveAs.Text;



strNotepadPath = string.Concat(StrDestinationPath,\\,Path.GetFileNameWithoutExtension(Path.GetFileName(strFileName)),strFileExtension);



FileStream fsOverwrite = new FileStream(strNotepadPath,FileMode.Create);

StreamWriter strmWrite = null;

switch(dpEncoding.Text)

{

caseASCII:

strmWrite = new StreamWriter(fsOverwrite,Encoding.ASCII);

break;

caseBigEndianUnicode:

strmWrite = new StreamWriter(fsOverwrite,Encoding.BigEndianUnicode);

break;

caseDefault:

strmWrite = new StreamWriter(fsOverwrite,Encoding.Default);

break;

caseUnicode :

strmWrite = new StreamWriter(fsOverwrite,Encoding.Unicode);

break;

caseUTF32:

strmWrite = new StreamWriter(fsOverwrite,Encoding.UTF32);

break;

caseUTF7:

strmWrite = new StreamWriter( fsOverwrite,Encoding.UTF7);

休息;

caseUTF8:

strmWrite = new StreamWriter(fsOverwrite,Encoding.UTF8);

break;

}



strText = ReadTextFromDocument(strFileName,app);

strText = strText.Replace(\ v,Environment。 NewLine); //\ n);

strmWrite.Write(strText);

strmWrite.Close();

fsOverwrite .Close();

}

app.Quit();

MessageBox.Show(流程已完成。 + TotalFiles +文件已转换,已成功完成,MessageBoxButtons.OK,MessageBoxIcon.Information);

}



catch(例外ex)

{

MessageBox.Show(ex.Message +\ n RESTART APPLICATION AND TRY AGAIN,Error,MessageBoxButtons.OK,MessageBoxIcon.Error );

返回;

}

终于

{

txtDestination.Text =;

txtSource.Text =;

lblStatus.Text =Ready ..;

dpSaveAs.SelectedItem = null ;

}

}



私有字符串ReadTextFromDocument(string strPath,myWord.Application app)

{

试试

{

myWord.Document doc;

对象缺失= Type.Missing;

对象readOnly = true;



对象路径= strPath;

doc = app.Documents.Open(ref path,ref missing,ref readOnly,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref缺少,参考缺失);

string text = doc.Content.Text;

doc.Close();



返回文字;

}

catch(例外情况)

{

throw ex;

}

}

when there is larges textual content .doc / .docx it does not get line break after each line.

Suppose lines are :

WORD FILE:

a great permanent loss to the greater part of those whose industry or capital is at present engaged in it. The
sudden loss of the employment even of the ships which import the eighty-two thousand hogsheads of tobacco,
which are over and above the consumption of Great Britain, might alone be felt very sensibly. Such are the unfortunate
effects of all the regulations of the mercantile system! They not only introduce very dangerous disorders
into the state of the body politic, but disorders which it is often difficult to remedy, without occasioning

NOTEPAD FILE SHOWS AS:

a great permanent loss to the greater part of those whose industry or capital is at present engaged in it. The sudden loss of the employment even of the ships which import the eighty-two thousand hogsheads of tobacco,which are over and above the consumption of Great Britain, might alone be felt very sensibly. Such are the unfortunate effects of all the regulations of the mercantile system! They not only introduce very dangerous disorders into the state of the body politic, but disorders which it is often difficult to remedy, without occasioning

What I have tried:

private void processWordToNotepad()
{
string strNotepadPath;
string strFileExtension;


lblStatus.Text = "Working..";
int FilesCount = 0, TotalFiles = 0;

if (txtSource.Text.Length == 0 || txtDestination.Text.Length == 0)
{
lblStatus.Text = "Error";
MessageBox.Show("Please Select the Source and Destination Files Folders!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
lblStatus.Text = "Ready.. ";
return;
}

if (FileNames == null)
{
MessageBox.Show("There were no files to process.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
lblStatus.Text = "Ready";
return;
}


if (dpSaveAs.SelectedIndex == -1)
{
MessageBox.Show("Select Save as Type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
strFileExtension = dpSaveAs.Text;
}

try
{
strFileExtension = dpSaveAs.SelectedItem.ToString();
TotalFiles = FileNames.Count();
myWord.Application app = new myWord.Application();

foreach (string strFileName in FileNames)
{
string strText;
lblStatus.Text = "Processing " + FilesCount++ + " of " + TotalFiles + " files..";
strFileExtension = dpSaveAs.Text;

strNotepadPath = string.Concat(StrDestinationPath, "\\", Path.GetFileNameWithoutExtension(Path.GetFileName(strFileName)), strFileExtension);

FileStream fsOverwrite = new FileStream(strNotepadPath, FileMode.Create);
StreamWriter strmWrite = null;
switch (dpEncoding.Text)
{
case "ASCII":
strmWrite = new StreamWriter(fsOverwrite, Encoding.ASCII);
break;
case "BigEndianUnicode":
strmWrite = new StreamWriter(fsOverwrite, Encoding.BigEndianUnicode);
break;
case "Default":
strmWrite = new StreamWriter(fsOverwrite, Encoding.Default);
break;
case "Unicode":
strmWrite = new StreamWriter(fsOverwrite, Encoding.Unicode);
break;
case "UTF32":
strmWrite = new StreamWriter(fsOverwrite, Encoding.UTF32);
break;
case "UTF7":
strmWrite = new StreamWriter(fsOverwrite, Encoding.UTF7);
break;
case "UTF8":
strmWrite = new StreamWriter(fsOverwrite, Encoding.UTF8);
break;
}

strText = ReadTextFromDocument(strFileName, app);
strText = strText.Replace("\v", Environment.NewLine);// "\n");
strmWrite.Write(strText);
strmWrite.Close();
fsOverwrite.Close();
}
app.Quit();
MessageBox.Show("Process Completed. " + TotalFiles + " files converted", "Successfully Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n RESTART APPLICATION AND TRY AGAIN", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
txtDestination.Text = "";
txtSource.Text = "";
lblStatus.Text = "Ready..";
dpSaveAs.SelectedItem = null;
}
}

private string ReadTextFromDocument(string strPath, myWord.Application app)
{
try
{
myWord.Document doc;
object missing = Type.Missing;
object readOnly = true;

object path = strPath;
doc = app.Documents.Open(ref path, ref missing, ref readOnly, 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 text = doc.Content.Text;
doc.Close();

return text;
}
catch (Exception ex)
{
throw ex;
}
}

推荐答案

在Word中,换行符只发生在段落的末尾,而不是在段落的内部逐行的段落。如果页面尺寸发生变化,这允许重新流动文本。



记事本,打开Word Wrap,做同样的事情,除了它的页面是记事本窗口的宽度。
In Word, line breaks only happen at the end of a paragraph, not inside the paragraph on a line-by-line basis. This allows for re-flowing of text if the page dimensions change.

Notepad, with Word Wrap turned on, does the same thing, except it's "page" is the width of the Notepad window.


这篇关于对于大文本内容文件,单词到记事本转换的换行符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆