We're sorry Aspose doesn't work properply without JavaScript enabled.

# Excel2Word

I need to be able to insert a document into another document (bounded by section breaks to preserve formatting). How should I do this? I tried to modify ConvertPictureToShape (from the Excel2Word sample application) but that doesn’t seem to work and I don’t think it will give me the result I am looking for.
Any Suggestions?

Hello!
Thank you for your interest in Aspose.Words.
That’s not clear what you are trying to achieve. If you’d like to append one MS Word document to another or concatenate several documents please refer to this documentation article:
https://docs.aspose.com/words/net/insert-and-append-documents/
Let me know if this helps and feel free to ask any other questions.
Regards,

I am sorry for the confusion.
Here’s our process…
Our current processing using .Net to write Word reports using Microsoft VSTO…
We create a report in Excel with tabular data only including formatting (bold, merge, borders, etc). There are no pictures or OLE Objects in the Excel.
I have also written an Excel to Word converter using VSTO that converts the Excel file to Word. While converting the report from Excel to Word, I search the document for the “markers” that indicate the filename of a Word document I am supposed to insert at that location in the report. If I find one of these, I insert a section-break at that location, the contents of the Word document and another section break. Then I continue looking for more markers of other documents that are to be inserted.
Here’s a pseudo-code that shows what I want to accomplish…
Open Excel file with report contents already created
"Copy" Report contents from Excel
Create new “main” Word doc
"Paste" Report contents into “main” Word doc
Search for First “Word doc Marker” in "main"
While “Word doc Marker” is found
{
Remove "Word doc Marker"
Insert Section Break into “main” Word doc
Read Word File “Word doc Marker” into "inserted Word doc
Insert “inserted” Word doc into “main” Word doc
Insert Section Break
Search for Next "Word doc Marker"
}
Save “main” Word File

Hi
https://docs.aspose.com/words/java/insert-and-append-documents/
Also if marker is just plain text you can use ReplaceEvaluator. See the following link for more information.
https://docs.aspose.com/words/net/find-and-replace/
Also there is a demo for Excel2Word conversion. See the following link:
https://releases.aspose.com/words/net
Best regards.

Excellent!
One more question. How do I search for a marker which has a ‘name’ attached to it so I can use the name to go to my database and get the document contents. Please see the red text in the attached excel spreadsheet as an example…

Oops, attached wiorng file. Try this one.

Hi
Thank you for additional information. Here is sample code:

public void Test056()
{
// Open document
// The document is output document of Xls2Doc convertor
Document doc = new Document(@"Test056\in.doc");
Regex regex = new Regex("WBSDESC");
doc.Range.Replace(regex, new ReplaceEvaluator(Replace056), false);
doc.Save(@"Test056\out.doc");
}
private ReplaceAction Replace056(object sender, ReplaceEvaluatorArgs e)
{
// Get match Run
Run run = (Run)e.MatchNode;
// Matched node is child of Table Row
Row row = (Row)run.GetAncestor(NodeType.Row);
// We should split table and insert document between sub tables
// Create new table
Table newTable = new Table(e.MatchNode.Document);
// Copy rows after matched row to the new table
while (row.NextSibling != null)
{
newTable.AppendChild(row.NextSibling);
}
// We should insert the newly created table after original table
// But first we should insert empty paragraph
Paragraph par = new Paragraph(e.MatchNode.Document);
row.ParentTable.ParentNode.InsertAfter(par, row.ParentTable);
// And insert table
par.ParentNode.InsertAfter(newTable, par);
// Now we can insert insert document
// Open corresponding document
// I assume that you will use information from
// run.Text to get document from DB. Here I just open document
Document doc = new Document(@"Test056\test.doc");
InsertDocument(par, doc);
// Also we should remove matched row
row.Remove();
return ReplaceAction.Skip;
}


Best regards.

Thank you for your help. I am getting closer.
I have attached 3 files to help describe my situation.
The file report.xls is the base report. I run the Excel2Word converter with my Modifications to insert the file WBSDESC.doc after row 6 of the spreadsheet. The output file is report.doc.
As you can see I am getting the document inserted.
The problem I am having is that the margins on the latter half of the report changed because of the inserted docuement. How do I isolate the inserted document, so it’s formatting and margins and so forth do not mess up the rest of the target document? I tried using section breaks to fix this, but it is not working.
Here’s the code I am using to insert the sub-report document…

private void AppendDoc(Document dstDoc, Document srcDoc)
{
// insert a section break before the appended docs to preserve formatting
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakContinuous);
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
foreach (Section srcSection in srcDoc)
{
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
// Now the new section node can be appended to the destination document.
dstDoc.AppendChild(dstSection);
}
// insert a section break after the appended docs to preserve formatting
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakContinuous);
}


- John

Hi

private void AppendDoc(Document dstDoc, Document srcDoc)
{
// insert a section break before the appended docs to preserve formatting
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakContinuous);
Section currentSect = builder.CurrentSection;
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
foreach (Section srcSection in srcDoc)
{
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
// Now the new section node can be appended to the destination document.
dstDoc.InsertBefore(dstSection, currentSect);
}
}


I highlighted my changes.
Hope this helps.
Best regards.

Sweet!!! That worked perfectly.
One more question. Before each table is a paragraph marker. Is there any way to suppress it?
See attached doc. (May need to turn on All Formatting Marks in Tools Options?

Another question related to this. After I insert the sub-report documetn, I need to know if there is enough space left on the last page to fit more of the main report. How do I determine the amount of space left (in inches maybe) on the last page after I have instered a doc. If there is not st least 1 inch of space, I was thinking I would insert a hard page-break.
- John

Hi

1. There is no way to turn of these options using Aspose.Words because these options are not stored in the document.
2. Unfortunately there is no way to determine the amount of space left on the page. Aspose.Words document represents content and formatting of a document, not its layout into lines and pages. This feature is called pagination and it is not released yet. Please see FAQ for more information.
Best regards,

Hello,
Now I am trying to convert some VSTO code to Aspose.cells:
I used to do this in VSTO…

Excel.Worksheet wks = ...; // get excel worksheet
object oRange = (object)("A1:C3");
wks.get_Range(oRange, Type.Missing).NumberFormat = "#,##0";


How do I do that in Aspose.Cells?
Thanks,
- John

Hi
Thanks for your inquiry. I think you should ask this question in Aspose.Cells forum.
Best regards.

Ok,
Thanks to your help, I am 95% there. I have a couple of last little things that need to be resolved and I could use your suggestions.
Please take a look at the xls and doc files attached to this post. The doc was created by the converterXls2Doc class. Notice how the doc file has not preserved the stretch to fit wide options that are in place in the xls file. Also, I need all the columns in the word doc to have the same widths as they are in the Excel file.
Can you suggest how to fix this?
Thanks again.
- John

Hi
Thanks for your inquiry. In the Xls2Doc I used the following formula to calculate width and height of cell

wordSize = excelSize * 0.75


Try using ConvertUtil to do the same.

wordSize = ConvertUtil.PixelToPoint(excelSize);


I hope this helps to resolve problem with cell widths.
Regarding “stretch to fit wide options” it is not quite clear for me what you mean. Could you change the word document to show me what the proper output should be?
Best regards.

Sorry about that, I should have included it the first time.
- John

Hi
Thanks for your inquiry. The differences occur because from Aspose.Cells width and height are returned in Pexels so we need to convert pixels to points. I tried to use ConvertUtil but get the same result. You can try using some coefficient to solve this.
Best regards.

Hi, I am lcsvb. We just got our official license, so I am using my corporate id instead.
My 1 remaining issue is a little complicated…
I need to be able to insert page-breaks into the converted report as well as word documents which serve as sub reports to the main report. Earlier in this thread you showed me how to insert the documents, and I have that part working. But now I need to be able to insert page breaks at known row numbers from the excel spread sheet. I know the row numbers for each of the related page-breaks and sub-reports. The page-breaks and documents can be inserted at any location in the document, like this…
---------------- start of document ----------------------
table part
--------------- page-break -------------
table part
------------ sub-report word document
table part
----------- page-break --------------
table part
---------- end of document ------------
Thoughts?

Hi
Thanks for your inquiry. You should split tables and then insert PageBreak between sub tables. For example see the following code:

// Open document and create documentBuilder
Document doc = new Document(@"Test084\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get table from document
Table tab = doc.FirstSection.Body.Tables[0];
// Let's insert page break after 5th row
// We should create new table
Table subTable = (Table)tab.Clone(false);
// Also we should create empty paragraph
// And insert it directly after table
Paragraph par = new Paragraph(doc);
tab.ParentNode.InsertAfter(par, tab);
// Now we should insert new table after thsi paragraph
par.ParentNode.InsertAfter(subTable, par);
// Cut all rows after 5th row and insert them into the sub table
while (tab.IndexOf(tab.LastRow) != 4)
{
subTable.Rows.Insert(0, tab.LastRow);
}
// And now we can insert Page Break
// we should move DocumentBuilder cursor to the paragraph
// between sub tables
builder.MoveTo(par);
builder.InsertBreak(BreakType.PageBreak);
// Save document
doc.Save(@"Test084\out.doc");


Hope this helps.
Best regards.