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