.doc 的显示格式错误
基本上我想在 WWW 中显示 .doc 文件,我尝试通过下面的代码获取数据 我可以从 .doc 获取数据,但输出不是预期的格式。
.doc 内的数据
一个
B
C
输出结果
AB C
我的预期结果是
一个
B
C
protected void Button2_Click(对象发送者,EventArgs e) {
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
// File Path
string strFilePath = @"C:\Users\Roy\Desktop\fypdoc.doc";
// Create obj filename to pass it as paremeter in open
object objFile = strFilePath; object objNull = System.Reflection.Missing.Value;
object objReadOnly = true;//Open Document
Microsoft.Office.Interop.Word.Document Doc = wordApp.Documents.Open(ref objFile, ref objNull, ref objReadOnly, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
// To read each line consider each line as paragraph Docuemnt
int i = 1;
foreach (Microsoft.Office.Interop.Word.Paragraph objParagraph in Doc.Paragraphs)
{
try
{
p1.InnerHtml += Doc.Paragraphs[i].Range.Text.Replace(Environment.NewLine,"</br>");
}
catch (Exception ex) { throw ex; } i++;
} // close document and Quit Word ApplicationDoc.Close(ref objNull, ref objNull, ref objNull);
wordApp.Quit(ref objNull, ref objNull, ref objNull);
}
basically i would like to display .doc file in WWW, i've tried to fetch data by code below
i can fetch data from .doc but the output is not expected format.
data inside .doc
A
B
C
output result
A B C
my expected result is
A
B
C
protected void Button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
// File Path
string strFilePath = @"C:\Users\Roy\Desktop\fypdoc.doc";
// Create obj filename to pass it as paremeter in open
object objFile = strFilePath; object objNull = System.Reflection.Missing.Value;
object objReadOnly = true;//Open Document
Microsoft.Office.Interop.Word.Document Doc = wordApp.Documents.Open(ref objFile, ref objNull, ref objReadOnly, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
// To read each line consider each line as paragraph Docuemnt
int i = 1;
foreach (Microsoft.Office.Interop.Word.Paragraph objParagraph in Doc.Paragraphs)
{
try
{
p1.InnerHtml += Doc.Paragraphs[i].Range.Text.Replace(Environment.NewLine,"</br>");
}
catch (Exception ex) { throw ex; } i++;
} // close document and Quit Word ApplicationDoc.Close(ref objNull, ref objNull, ref objNull);
wordApp.Quit(ref objNull, ref objNull, ref objNull);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用换行符,而是尝试保留段落的语义值:
Instead of using line breaks, try to preserve the semantic value of paragraphs:
尝试将
替换为
。Try replacing
</br>
with<br />
.我认为这里的问题是您正在阅读 3 个段落,但在将它们传输到 html 时却没有在它们之间添加行。
I think that the problem here is that you're reading in 3 paragraphs and yet not adding adding lines between them when transferring them to your html.
看起来您正在循环遍历每个段落,并且如果您希望每个段落都是由换行符分隔的文本块,那么它们本身将不包含任何换行符。
连接文本后添加 br 或使用段落标签。
It looks like you are looping through each Paragraph, and if you are expecting each paragraph to be a block of text seperated by linebreaks then they won't contain any line breaks themselves.
Either add the br after concatenating your text or use Paragraph tags.