有没有可能知道一个给定的文本中有多少可以放入一个表格单元格?本例将所有内容放在第一个单元格中,我需要将其一分为二,而无需调整第一个单元格的大小或更改字体.

using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
        string filename = "example.docx";
        string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor";

        var wordApp = new Word.Application { Visible = false };
        string path = Path.Combine(Directory.GetCurrentDirectory(), filename);
        var doc = wordApp.Documents.Open(path, ReadOnly: false, Visible: true);
        doc.Activate();
        var table = doc.Tables[1];


        // todo: truncate text by the cell's size
        table.Cell(1, 1).Range.Text = text;

        string text2 = "";
        // todo: put the remainder of the truncated text to text2
        table.Cell(2, 1).Range.Text = text2;


        doc.Save();
        object missing = Missing.Value;
        doc.Close(ref missing, ref missing, ref missing);
        wordApp.Quit(ref missing, ref missing, ref missing);
    }
}

推荐答案

@macropod让我想到了一个肮脏的黑客(并不是说有任何干净的方式来处理Word):只需不断将文本推入单元格,直到其高度发生变化.这对我来说已经足够了,但你也可以判断宽度.

我使用下一行的Y位置来确定前一行的高度.因此,作为一种限制,下面应该至少多出一行.此外,还应判断页码.

using System.Diagnostics;
using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
        string filename = "example.docx";
        string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor";
        int row = 1;
        int column = 1;


        var wordApp = new Word.Application { Visible = true };
        string path = Path.Combine(Directory.GetCurrentDirectory(), filename);
        var doc = wordApp.Documents.Open(path, ReadOnly: false, Visible: true);
        doc.Activate();
        var table = doc.Tables[1];


        // Add as much text to the cell (row,column) as possible without changing the height of the cell.

        // Row's bottom Y coordinate can be determined by the position of the next row and its page number.
        Debug.Assert(table.Rows.Count > row, "Need at least 1 more row below");
        table.Rows[row + 1].Select();
        float oldBottom = wordApp.Selection.Information[Word.WdInformation.wdVerticalPositionRelativeToPage];
        int oldPage = wordApp.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];

        // Binary-searching the length of the longest fitting substring
        int min = 0;
        int max = text.Length;
        int length = 0;
        while (min < max)
        {
            length = (min + max) / 2;
            table.Cell(row, column).Range.Text = text.Substring(0, length);
            float bottom = wordApp.Selection.Information[Word.WdInformation.wdVerticalPositionRelativeToPage];
            float page = wordApp.Selection.Information[Word.WdInformation.wdActiveEndPageNumber];
            if (page > oldPage || bottom > oldBottom)
                max = length - 1;
            else
                min = length + 1;
        }


        // Don't split words
        while (length > 0 && char.IsLetterOrDigit(text[length - 1]))
            length--;
        table.Cell(row, column).Range.Text = text.Substring(0, length);


        doc.Save();
        object missing = Missing.Value;
        doc.Close(ref missing, ref missing, ref missing);
        wordApp.Quit(ref missing, ref missing, ref missing);
    }
}

Csharp相关问答推荐

如何打印已添加到List的Linq值,而不是C#中的:System.Collections.Generic.List ' 1[System.Int32]?

. NET 8 HttpClient post参数将其情况更改为camel'

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

Elasticsearch:当我try 使用c#将嵌套对象添加到filter中时出现问题

Azure Function应用(. NET 8)不将信息记录到应用洞察

为什么任务需要在内部使用ManualResetEventSlim?

XUNIT是否使用测试数据的源生成器?

需要在重新启动ApplicartionPool或IIS后启动/唤醒API的帮助

如何向事件添加成员

GODOT 4向C#中的字符串参数发送信号以等待

我什么时候应该在Dapper中使用Connection.OpenAsync?

C#使用相同内存的多个数组

当我手动停止和关闭系统并打开时,Windows服务未启动

数据库操作预计影响1行,但实际影响0行; after _dbContext.SaveChanges();

为什么我的属性即使没有显式地设置任何[必需]属性,也会显示验证?

SharpZipLib在文件名前加上目录名,生成tar.gz

SqlException:无法打开数据库.升级到Dotnet 8后-数据库兼容性版本-非EFCore兼容性级别

我想我必须手动使用res1(字符串形式的PowerShell哈希表)

无法将.Net Framework 4.8.1升级到.Net 7

Avalonia MVVM数据模板