Closed Zt-freak closed 1 month ago
Hi, you can use AddCarriageReturn(
) method this way:
string text = "E\nF\nG\nH";
var lines = text.Split("\n");
run.SetText(lines[0]);
for(int i = 1; i < lines.Length; i++)
{
run.AddCarriageReturn();
run.AppendText(lines[i]);
}
@Bykiev this solution using AddCarriageReturn
appears to work, except within tables.
@Zt-freak, you can use this code for tables:
var table = doc.Tables.First();
var row = table.GetRow(0);
var cell = row.GetCell(0);
foreach (var p in cell.Paragraphs)
{
foreach (var r in p.Runs)
{
if (!string.IsNullOrWhiteSpace(r.Text))
{
var lines2 = r.Text.Split(new string[] { "\\n" }, StringSplitOptions.None);
r.SetText(lines2[0]);
for (int i = 1; i < lines2.Length; i++)
{
r.AddCarriageReturn();
r.AppendText(lines2[i]);
}
}
}
}
@Bykiev
@Zt-freak, you can use this code for tables:
var table = doc.Tables.First(); var row = table.GetRow(0); var cell = row.GetCell(0); foreach (var p in cell.Paragraphs) { foreach (var r in p.Runs) { if (!string.IsNullOrWhiteSpace(r.Text)) { var lines2 = r.Text.Split(new string[] { "\\n" }, StringSplitOptions.None); r.SetText(lines2[0]); for (int i = 1; i < lines2.Length; i++) { r.AddCarriageReturn(); r.AppendText(lines2[i]); } } } }
Sadly, this doesn't work on my end, it only removes the "\n"
s from the paragraph
Attache your code and file please
@Zt-freak, the code looks good, but the document contains multiple runs in the paragraph. I've opened the document with WPS Office, selected the second cell and cleared the format, after this there is only 1 run. But with MS Word after clearing the format getting 10 runs. As it's not related to NPOI, the best way will be concat multiple runs in paragraph in one string and then split it with new lines symbol.
@Bykiev Please ignore the behavior of WPS Office. It's usually different from Microsoft Word.
Hi @tonyqus , @Bykiev , @Zt-freak ,
I was looking into this and I was wondering if I could suggest an alternate solution to the problem. Basically our solution works as follows, we define a template in a word document and then merge it with a dataset to get the final document. So we define a set of placeholders in word which the software then replaces with the action value from the data set. This means that our software solution is basically one big search and replace exercise. So for example I in my word document would like:
The software would then have to replace the text “$replace_text$” and “$replace_cell_text$” with a given text string, in my example I want to replace the tag with the value “Regel1\nRegel2\nRegel3”
I created a small unit test:
If i run a unittest npoi generates the following output
As suggested by @Bykiev we would need to break each part up into a separate run, but turns out to be rather difficult. if i create a word document and add three lines and then look at the xml i see that word has created a run for each line.
I checked the openxml documentation (Office Open XML (OOXML) - Word Processing - Text) and an enter can also be encoded in xml as “
If run my unit test with this modification i do get the result i am looking for:
The xml now looks like for the top part
And the table cell looks like:
I would like to hear your thoughts on the matter and change i propose.
@pbvs, look good, would you like to contribute?
@Bykiev Sure no problem, I will create the change as described above, do some additional testing and create a pull request when finished.
NPOI Version Used
2.6.2
File Type
Use Case
I want to be able to add line endings to a document, however every line ending
\n
in the string I want to add gets converted into a space.I tested with both
XWPFParagraph.ReplaceText()
andXWPFRun.SetText()
, both of them share this\n
to spaces conversion behaviour.Description
I got an example program which calls both
XWPFParagraph.ReplaceText()
andXWPFRun.SetText()
:The document contains one paragraph containing the text:
The resulting document will contain the following text:
While the desired result would be: