1. First through the program to generate report style HTML page, and then modify the HTML page suffix named Doc.
2. Customize the template files for Word documents, manipulate Word templates in C #, and generate new Word documents.
The first solution is simple, just change the file extension, but there are some problems, such as the loss of the generated Word document style. This may be an impossible solution for the customer. The second scenario is complex, and you need to call Office's Word component to manipulate Word through C # to build word. This method is similar to the background stitching data we have in C #. Although troublesome, but can be flexible customization, but only the operation of Word objects.
After repeated consideration: decided to use the second method to generate Word report documents.
Through their own practice, this demand is finally done, in the actual development process, encountered such a problem, fortunately, through the constant search network resources, the actual development of the situation, the problem has been solved. Now I am in the development process of some understanding and experience summed up:
Under the VS2008 platform, reference. net-microsoft.office.interop.word.12 so that you can manipulate Word objects in your program.
By simple execution, 80070005 errors were reported because of insufficient permissions and the need to change the operation rights of the. NET and IIS users in the DCOM configuration as follows: Workaround one:
1. Control Panel-"Management tools-" Component Services-"computer-" My Computer-"DCOM configuration-" After you locate the Microsoft Word document, click Properties to open the Properties dialog box for this application.
2. Click the Identification tab, and then select the Interactive user.
3. Click the Security tab, select Custom in the start and Activate permissions and access permissions groups, and then customize-> edit-> add asp.net account and iuser_ computer name.
4. Ensure that each user is allowed access, and then click OK.
5. Click OK to close DCOMCNFG.
If the above method does not resolve the problem, it should be a permission issue, please try the following method:
Using identity simulations in web.config, add <identity impersonate= "true" username= "your username" password= "password" in the <system.web> section/>
</system.web>
To solve the problem, start thinking about how to create a Word template file, in which Word's template file is actually added by bookmarks. That is, by creating a bookmark in a Word document and then getting all the bookmarks for the template file in the program, the document is generated by assigning a value to the bookmark.
The procedures in the process are as follows:
Declare an object for a word program → declare a Word document object → Get the current action Document object → get all the bookmarks in the document → Assign the database data to the corresponding bookmark → Save the document as a specified folder.
The following will analyze specific code implementations for agricultural plant test reports:
Copy Code code as follows:
Build Word program objects and Word document objects
Microsoft.Office.Interop.Word.Application Appword = new Application ();
Microsoft.Office.Interop.Word.Document doc = new Document ();
Object omissing = system.reflection.missing.value;//What is this thing, I still don't understand-_-
Open the template document and specify Doc's document type
Object objtemplate = Server.MapPath (P_templatepath);
Object objdoctype = wddocumenttype.wdtypedocument;
doc = (Document) appWord.Documents.Add (ref objtemplate, ref objfalse, ref objdoctype, ref objtrue);
Get all bookmarks in the template
Bookmarks ODF = doc. Bookmarks;
String[] Testtableremarks = {"Applyno", "Auditingdate", "Auditor", "Checkdate", "Checker"};
String[] Testtablevalues = {"Applyno", "Auditingdate", "Auditor", "Checkdate", "Checker",};
Loops all bookmarks and assigns a value to the bookmark
for (int oindex = 0; Oindex < testtableremarks.length; oindex++)
{
Obdd_name = WD + Testtableremarks[oindex];
Doc. Bookmarks.get_item (ref obdd_name). Range.Text = P_testreporttable.rows[0][testtablevalues [Oindex]]. ToString ()//The range is also an important object in Word, which is the area of the current action parameter
}
Step fourth generates word, saves the current document object as the specified path, and then closes the Doc object. Close Application
Object filename = Server.MapPath (p_savepath) + "\\Testing_" + DateTime.Now.ToShortDateString () + ". Doc";
Object miss = System.Reflection.Missing.Value;
Doc. SaveAs (ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, R EF Miss, ref miss, ref miss, ref miss, ref miss);
Object missingvalue = Type.Missing;
Object donotsavechanges = wdsaveoptions.wddonotsavechanges;
Doc. Close (ref donotsavechanges, ref missingvalue, ref missingvalue);
AppWord.Application.Quit (ref miss, ref miss, ref miss);
doc = null;
Appword = null;
This. Hid_showmessage.value = "Build Success!" ";
The above code is a process of generating word from a template file. is actually a process that replaces the bookmark content.
In the process of development, some data is dynamically increased, if I want to add a few rows of data in a table dynamically, can not be used to replace the bookmark, you need to use the program in the document page of the table to add rows.
To add a row to a table, there are two types of actions: one is that a table already exists in the Word template. One is that we add a Table object directly to the program.
In the first case, note that in the table you want to manipulate in the Word template, you cannot have vertically merged cells, or the program will not be able to get the current action object to cause the program to complain. The merging of cells, we can control in the program.
In the second case, we need to add the table directly through the program.
The code to generate the table is as follows:
1. Get a table that already exists in the document:
Microsoft.Office.Interop.Word.Table charactertable = doc. tables[2];//in the collection operation of the Document object, the starting point is starting from 1, not starting from 0, which needs to be noted here.
2. Create a table directly in your document, first get the location where you inserted the table, and then add the Table object:
Object oEndOfDoc = "\\endofdoc"; there are a number of predefined bookmarks in//word, which are not listed here.
object omissing = System.Reflection.Missing.Value;
Range wrdrng = doc. Bookmarks.get_item (ref oEndOfDoc). range;//gets the end position of the current document.
wrdRng.InsertAfter ("");//Insert a row, you cannot use wrdRng.InsertAfter (""), if you use this, you can not wrap, I do not know why.
Copy Code code as follows:
Object ocollapseend = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
Object opagebreak = microsoft.office.interop.word.wdbreaktype.wdpagebreak;//page break
Wrdrng.collapse (ref oCollapseEnd);
Wrdrng.insertbreak (ref opagebreak);//Insert a page
Wrdrng.collapse (ref ocollapseend);
wrdRng.InsertAfter ("picture Information");
WrdRng.Font.Size = 20;//Specifies the text size of the action object
WrdRng.Font.Bold = 1;//The bold body of the specified Action object: 1 is bold, 0 is normal
WrdRng.ParagraphFormat.Alignment = wdparagraphalignment.wdalignparagraphcenter;//The text layout of the specified action area: centered
// The above code means to find the current end position and then insert a page break, which is equivalent to jumping to a new page, writing "Picture information" at the top of the new page, and specifying the text size to be 20, in bold center display.
Wrdrng = doc. Bookmarks.get_item (ref oEndOfDoc). Range;
wrdRng.InsertAfter ("");
Wrdrng = doc. Bookmarks.get_item (ref oEndOfDoc). Range;
Wrdrng.insertparagraphafter ()//Inserts a paragraph with a 2-row-column table inserted on this paragraph.
Microsoft.Office.Interop.Word.Table newtable = doc. Tables.add (WRDRNG, 2, 1, ref omissing, ref omissing);
We can also format the table, and we're not going to list it here.
3. Below we analyze the operation of the table cell: merge, Split. This requires us to operate according to the actual form:
Get a specific cell (1,1) to get the cell of the first column in the first row
Cell cell = Doc. TABLES[1]. Cell (1,1);
Cell. range.text= "text"; Specifies that the contents of the current cell is text
In the Operation of table, add a new row:
Object BeforeRow = Doc. TABLES[1]. rows[2];//This row is to get to the second row first
Doc. TABLES[1]. Rows.Add (ref BeforeRow); the effect is similar to "Insert Row" on the second line of this table in Word, and the inserted new row is inserted into the previous line of the current row, in the same format as the row.
Merge cells: Feel like you're merging cells here. You just need to specify the starting cell and end cell that you want to merge, and then you can go through the merge operation.
Cell cell = Doc. TABLES[1]. Cell (IRow, 2);//Column Merge
Cell. Merge (Doc. TABLES[1]. Cell (IRow, 6));
Cell cell1 = doc. TABLES[1]. Cell (iRow-1, 1);//Row merge
Cell1. Merge (Doc. TABLES[1]. Cell (IRow + 1, 1));
The above operation is used in this program some knowledge, there are many things need to be familiar with, understanding.
In addition, in the process of testing the program found that when the implementation of a document generation, in the resource manager has always been Winword.exe process can not kill, the current solution is: direct kill process, the code is as follows:
Copy Code code as follows:
protected void killallprocess ()//kill all Winword.exe processes
{
System.diagnostics.process[] Myps;
Myps = System.Diagnostics.Process.GetProcesses ();
foreach (System.Diagnostics.Process p in Myps)
{
if (p.id!= 0)
{
String MyS = "Winword. EXE "+ P.processname +" ID: "+ p.id.tostring ();
Try
{
if (p.modules!= null)
if (P.modules.count > 0)
{
System.Diagnostics.ProcessModule pm = p.modules[0];
MyS + = "\ Modules[0]." FileName: "+ pm." FileName;
MyS + = "\ Modules[0]." ModuleName: "+ pm." ModuleName;
MyS + = "\ Modules[0]." Fileversioninfo:\n "+ PM. Fileversioninfo.tostring ();
if (PM. Modulename.tolower () = = "Winword.exe")
P.kill ();
}
}
Catch
{ }
Finally
{
}
}
}
}
So far, a Word document has been generated. The above for me in the development of the program encountered problems and solutions, there may be a lot of places are not fully considered, if the program in the development of word operations have a new understanding, and I welcome the communication, improve each other!
Below are some better excerpts on the Internet:
Create new word
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWORD.DOCUMENTS.ADD (ref omissing, ref omissing,
ref omissing, ref omissing);
To open a document:
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
Object fileName = @ "E:cccxcxxtestdoc.doc";
odoc = OWord.Documents.Open (ref fileName,
ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,
ref omissing, ref omissing, ref omissing, ref omissing, ref omissing,
ref omissing, ref omissing, ref omissing, ref omissing, ref omissing);
Import templates
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
Object fileName = @ "E:xxxccxtest.doc";
odoc = OWORD.DOCUMENTS.ADD (ref fileName, ref omissing,
ref omissing, ref omissing);
. Add a new table
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWORD.DOCUMENTS.ADD (ref omissing, ref omissing,
ref omissing, ref omissing);
Object start = 0;
Object end = 0;
Word.Range tablelocation = Odoc.range (ref start, ref end);
ODoc.Tables.Add (Tablelocation, 3, 4, ref omissing, ref omissing);
. Table Insert Row
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWORD.DOCUMENTS.ADD (ref omissing, ref omissing,
ref omissing, ref omissing);
Object start = 0;
Object end = 0;
Word.Range tablelocation = Odoc.range (ref start, ref end);
ODoc.Tables.Add (Tablelocation, 3, 4, ref omissing, ref omissing);
Word.table newtable = odoc.tables[1];
Object beforerow = Newtable.rows[1];
NEWTABLE.ROWS.ADD (ref BeforeRow);
. Cell Merging
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWORD.DOCUMENTS.ADD (ref omissing, ref omissing,
ref omissing, ref omissing);
Object start = 0;
Object end = 0;
Word.Range tablelocation = Odoc.range (ref start, ref end);
ODoc.Tables.Add (Tablelocation, 3, 4, ref omissing, ref omissing);
Word.table newtable = odoc.tables[1];
Object beforerow = Newtable.rows[1];
NEWTABLE.ROWS.ADD (ref BeforeRow);
Word.cell Cell = Newtable.cell (1, 1);
Cell. Merge (Newtable.cell (1, 2));
. Cell separation
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWord.Documents.Add (omissing,
ref omissing, ref omissing);
Object start = 0;
Object end = 0;
Word.Range tablelocation = Odoc.range (ref start, ref end);
ODoc.Tables.Add (Tablelocation, 3, 4, ref omissing, ref omissing);
Word.table newtable = odoc.tables[1];
Object beforerow = Newtable.rows[1];
NEWTABLE.ROWS.ADD (ref BeforeRow);
Word.cell Cell = Newtable.cell (1, 1);
Cell. Merge (Newtable.cell (1, 2));
Object rownum = 2;
Object columnnum = 2;
Cell. Split (ref rownum, ref columnnum);
Inserting by paragraph control
Copy Code code as follows:
object omissing = System.Reflection.Missing.Value;
Object oEndOfDoc = "\endofdoc";/**//* Endofdoc is a predefined bookmark */
//start Word and create a new docume Nt.
Word._application oword;
Word._document odoc;
oword = new Word.Application ();
oWord.Visible = true;
odoc = OWORD.DOCUMENTS.ADD (ref omissing, ref omissing,
ref omissing, ref omissing);
//insert A paragraph at the beginning of the document.
Word.Paragraph oPara1;
OPara1 = ODOC.CONTENT.PARAGRAPHS.ADD (ref omissing);
OPara1.Range.Text = "Heading 1";
OPara1.Range.Font.Bold = 1;
OPara1.Format.SpaceAfter =//24 pt spacing after paragraph.
OPara1.Range.InsertParagraphAfter ();