.doc 的显示格式错误

发布于 2025-01-01 11:12:05 字数 1707 浏览 0 评论 0原文

基本上我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安静 2025-01-08 11:12:05

不要使用换行符,而是尝试保留段落的语义值:

p1.InnerHtml += "<p>" + Doc.Paragraphs[i].Range.Text + "</p>";

Instead of using line breaks, try to preserve the semantic value of paragraphs:

p1.InnerHtml += "<p>" + Doc.Paragraphs[i].Range.Text + "</p>";
南街九尾狐 2025-01-08 11:12:05

尝试将
替换为

Try replacing </br> with <br />.

巷子口的你 2025-01-08 11:12:05

我认为这里的问题是您正在阅读 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.

甩你一脸翔 2025-01-08 11:12:05

看起来您正在循环遍历每个段落,并且如果您希望每个段落都是由换行符分隔的文本块,那么它们本身将不包含任何换行符。

连接文本后添加 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文