im programming in C# and i need to add a simple phrase on the last page of each ms-word doc.
i have tried this
Word.Paragraph paragraph;
int totalPages;
Word.Range range;
range = doc.Content.Duplicate;
totalPages = range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
range = range.GoTo(Word.WdGoToItem.wdGoToPage, Type.Missing, totalPages, Type.Missing);
paragraph = doc.Paragraphs.Add(range);
paragraph.Range.Font.Name = "Arial";
paragraph.Range.Font.Size = 7;
paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.Text = encrypt;
but this code are adding a pharse in the last but one page .
How about something like this (you may need to fix the C# syntax):
doc.Content.InsertParagraphAfter();
Word.Paragraph paragraph = doc.Paragraphs(doc.Paragraphs.Count());
paragraph.Range.Font.Name = "Arial";
paragraph.Range.Font.Size = 7;
paragraph.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.Text = encrypt;
Related
using Word = Microsoft.Office.Interop.Word;
public static void CreateWord()
{
Word.Application objWord = new Word.Application();
Word.Document objDoc = objWord.Documents.Add();
Word.Paragraph objPara;
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Bold text aligned center";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objPara.Range.Bold = 1;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned left";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned center\nwith something on the next line";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objPara = objDoc.Paragraphs.Add();
objPara.Range.Text = "Regular text aligned left, with some Bold text here\nand some regular text on next line";
objPara.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objPara.Range.Bold = 0;
objPara.Range.InsertParagraphAfter();
objWord.Visible = true;
objWord.WindowState = Word.WdWindowState.wdWindowStateNormal;
}
The code above creates a Word document and writes text to it, but it looks like this:
I'm assuming that text in Microsoft.Office.Interop.Word is not supposed to act like that, or text should be added in some other way, but I'm failing to find any good information on how to do this.
So, my question is - how to add text from c# to a newly created word document with the right properties, so the result looks like this:
P.S. tips about creating tables using .Interop.Word would also be appreciated.
The key to working with content in Word documents is to understand how to work with Range objects. There's more than one way to achieve a goal of this nature. The following code snippet is my preference: collapsing the Range for each step (think of it like pressing the right-arrow key to "collapse" a selection to a point), rather than mixing Add, InsertAfter and similar methods.
For each change of formatting the Range must be "collapsed". So if you have bold in the middle of a line, that's a separate step. If numerous paragraphs should have the same formatting, they can be combined.
Environment.NewLine and \n can be used interchangeably.
Generally, assign text content, then format the Range.
object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
doc.Paragraphs.Add();
Word.Range objRange = doc.Content;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Bold text aligned center" + Environment.NewLine;
objRange.Bold = 1;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned left" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objRange.Bold = 0;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned center\nwith something on the next line" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
objRange.Bold = 0;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Regular text aligned left, with some ";
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "Bold text here";
objRange.Bold = 1;
objRange.Collapse(ref oCollapseEnd);
objRange.Text = "\nand some regular text on next line" + Environment.NewLine;
objRange.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
objRange.Bold = 0;
I've been working with ClosedXML for the past 24 hours, I feel as if I've gotten everything out of it I need except for one bug. I have an existing workbook that has 3 sheets in it currently. Similar data will be on each sheet but it will be different values. I've attached my pseudo code below (Specific variable names have been changed to protect the innocent).
The code below is inside a foreach loop as I'm looping thru a large set of SQL tables. The problem is, ClosedXML or something I have done is writing the same data to all sheets and overwriting the sheets in front and behind it. I feel perhaps I've done something obvious but I've been staring at the same code for awhile and can no longer see the glaring error. Any help would be appreciated!
//Workbook = new workbook and filepath are initialized outside of the loop
var worksheet = workbook.Worksheet(1);
//This is used to help me identify what column it is in, it references an array of alphabet characters later
int column = 0;
if(loopCt <= 2)
{
worksheet.Cell("A1").Value = "Identifier";
worksheet.Cell("B1").Value = "XYZ Day 1";
worksheet.Cell("C1").Value = "XYZ Day 2";
worksheet.Cell("D1").Value = "XYZ Day 3";
worksheet.Cell("E1").Value = "XYZ Day 4";
worksheet.Cell("F1").Value = "XYZ Day 5";
worksheet.Cell("G1").Value = "XYZ Day 6";
worksheet.Cell("H1").Value = "XYZ Day 7";
worksheet.Cell("I1").Value = "XYZ Day 8";
worksheet.Cell("J1").Value = "XYZ Day 9";
worksheet.Cell("K1").Value = "XYZ Day 10";
worksheet.Cell("L1").Value = "XYZ Weekly Total";
}
worksheet.Cell($"A{loopCt}").Value = item.identifier;
for (int i = 0; i < XYZDaily.Count(); i++)
{
column += 1;
worksheet.Cell($"{alphabet[column]}{loopCt}").Value = XYZDaily[i];
}
worksheet.Cell($"L{loopCt}").Value = XYZWeek;
workbook.Save();
//QRS Export
var worksheetQRS = workbook.Worksheet(2);
int columnQRS = 0;
if (QRSCt <= 2)
{
worksheetQRS.Cell("A1").Value = "Identifier";
worksheetQRS.Cell("B1").Value = "QRS Day 1";
worksheetQRS.Cell("C1").Value = "QRS Day 2";
worksheetQRS.Cell("D1").Value = "QRS Day 3";
worksheetQRS.Cell("E1").Value = "QRS Day 4";
worksheetQRS.Cell("F1").Value = "QRS Day 5";
worksheetQRS.Cell("G1").Value = "QRS Day 6";
worksheetQRS.Cell("H1").Value = "QRS Day 7";
worksheetQRS.Cell("I1").Value = "QRS Day 8";
worksheetQRS.Cell("J1").Value = "QRS Day 9";
worksheetQRS.Cell("K1").Value = "QRS Day 10";
worksheetQRS.Cell("L1").Value = "QRS Weekly Total Test";
}
worksheetQRS.Cell($"A{loopCt}").Value = item.Identifier;
for (int i = 0; i < QRSDaily.Count(); i++)
{
columnQRS += 1;
worksheetQRS.Cell($"{alphabet[columnQRS]}{loopCt}").Value = QRSDaily[i];
}
worksheetQRS.Cell($"L{loopCt}").Value = QRSWeek;
workbook.Save();
TL:DR;
Writing data to an existing spreadsheet with multiple sheets,
Written inside a foreach loop
Problem: I'm attempting to target one sheet at a time, write data, move on to the next sheet and write data, but its writing the same data to all sheets within the workbook and I cant find anything in the documentation about this particular scenario.
Thanks for reading,
You are always using the first Worksheet in the Workbook:
var worksheet = workbook.Worksheet(1);
^
You need to use a variable for the worksheet number or use the workbook.AddWorksheet() method. This is covered under Creating Multiple Worksheets in the documentation.
I create instance of Document class
Microsoft.Office.Interop.Word.Document
How i can insert table and text into special position in Document ?
For example after or before one bookmark
To add text in word doc, you can use
Word.Range rng = this.Application.ActiveDocument.Range(0, 0);
rng.Text = "New Text";
for table:
Word.Range tableLocation = this.Range(ref start, ref end);
this.Tables.Add(tableLocation, 10, 11);
check this link for text and table.
Set rangeStart = ActiveDocument.Bookmarks("YourFirstBookmark").Range
Set rangeEnd = ActiveDocument.Bookmarks("YourEndBookmark").Range
var range= Range(rangeStart.Start, rangeEnd.End)
In my C# windows Form application I use below code. it works fine. but I need to add line space for this paragraph.
var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 13, iTextSharp.text.Font.UNDERLINE, BaseColor.BLUE);
List<Anchor> anchor = new List<Anchor>();
foreach (string tName in templateName)
{
Anchor anch = new Anchor(tName, linkFont);
anch.Reference = "#" + tName;
anchor.Add(anch);
}
Paragraph templateData = new Paragraph();
templateData.Alignment = Element.ALIGN_LEFT;
for (int z = 0; z < anchor.Count; z++)
{
templateData.Add(anchor[z]);
templateData.Add(" , ");
}
output of this code is below.
Output of above code
if I use following code nothing changed.
Paragraph templateData = new Paragraph();
templateData.Alignment = Element.ALIGN_LEFT;
templateData .SetLeading(15, 1);
How can I fix this issue and add line space for this paragraph ?
Thanks
Change value of Y in:
templateData.SetLeading(15, 10); //'1' to '10' or whatever you want
well I have a mini program changing all excel cells beginning with ='C:\, but I have a problem. I use range.replace for do this, but my program work wrong, because it don't change all the cells contain ='C:\, only change the first find it and I don't know for what reason.
My code is :
foreach (Excel.Worksheet sheet in xlWorkBook.Sheets)
{
string sheetName = sheet.Name;
Console.WriteLine(sheetName);
//seleccion rango activo
range = sheet.UsedRange;
//leer las celdas
int rows = range.Rows.Count;
int cols = range.Columns.Count;
//Excel.Range startCell = sheet.Cells[1, 1];
//Excel.Range endCell = sheet.Cells[rows, cols];
sheet.Range["A1:J1000"].Replace(#"='C:\", #"='T:\");
//range = sheet.UsedRange;
// leer las celdas
//int rows = range.Rows.Count;
//int cols = range.Columns.Count;
//}
// liberar
releaseObject(sheet);
}
You need to do another foreach-Loop for every cell in the selected range.
I have no way of testing this right now, but this should work:
object whatToReplace = "what you want to replace";
object replacement = "what you want to replace it with";
foreach(Range cell in range)
{
cell.Replace(whatToReplace, replacement);
}