1. First generate a report-style HTML page through the program, and then modify the suffix name of the HTML page to DOC.
2. Customize the template file of the WORD document, operate the WORD template in C#, and generate a new WORD document.
The first solution is simple. You only need to change the file extension, but there are some problems, such as the loss of the generated WORD document style. This may be an unpassable solution for customers. The second solution is relatively complex, and requires calling OFFICE's WORD component to operate WORD through C# to generate WORD. This method is similar to our background splicing data in C#. Although it is troublesome, it can be customized flexibly, just to operate WORD objects.
After repeated consideration, it was decided to use the second method to generate the WORD report document.
Through my own practice, this requirement has finally been solved. In the actual development process, I encountered various problems. Fortunately, by constantly searching for network resources and combining the actual development situation, the problems have been solved. Let me summarize some of my understanding and experience during the development process:
Under the VS2008 platform, refer to .net-.12, so that you can use WORD objects to operate in the program.
Through simple execution, an error of 80070005 was reported. This error is because the permissions are insufficient, and the operation permissions of .net and IIS users need to be changed in the DCOM configuration. The specific modification process is as follows: Solution 1:
1. Control Panel -> Management Tools -> Component Services -> Computer -> My Computer -> DCom Configuration -> After finding the Microsoft Word document, click Properties to open the properties dialog box of this application.
2. Click the Identification tab and select Interactive User.
3. Click the "Security" tab, select "Custom" in the "Start and Activate Permissions" and "Access Permissions" groups, and then Customize->Edit->Add Account and IUSER_Computer Name.
4. Make sure each user is allowed to access and click OK.
5. Click OK to close DCOMCNFG.
If the above method cannot solve the problem, it should be a permission problem. Please try the following method:
Use identity simulation in the <identity impersonate="true" userName="your username" password="password"/> in the <> section
</>
After solving the above problem, I began to consider how to create a WORD template file. The WORD template file is actually used to add content through bookmarks. That is, by creating bookmarks in the WORD document, then obtaining all bookmarks of the template file in the program, and generating the document by assigning values to the bookmarks.
The operation process in the program is as follows:
Declare the object of the WORD program → Declare a WORD document object → Get the current operation document object → Get all bookmarks of the document → Assign database data to the corresponding bookmark → Save the document as a specified folder.
The following will analyze the specific code implementation for agricultural plant test reports:
//Generate WORD program object and WORD document object
appWord = new Application();
doc = new Document();
object oMissing = ;//I never figured out what this is-_-
//Open the template document and specify the document type of doc
object objTemplate = (p_TemplatePath);
object objDocType = ;
doc = (Document)(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//Get all bookmarks in the template
Bookmarks odf = ;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//Cycle all bookmarks and assign values to the bookmarks
for (int oIndex = 0; oIndex < ; oIndex++)
{
obDD_Name = WD + testTableremarks[oIndex];
.get_Item(ref obDD_Name). = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//The Range is also a very important object in WORD, which is the area where the current operation parameters are located.
}
//Step 4 Generate word, save the current document object as the specified path, and then close the doc object. Close the application
object filename = (p_SavePath) + "\\Testing_" + () + ".doc";
object miss = ;
(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = ;
object doNotSaveChanges = ;
(ref doNotSaveChanges, ref missingValue, ref missingValue);
(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
this.Hid_ShowMessage.Value = "Generated successfully!";
The above code is a process of generating WORD through template files. In fact, it is just a process of replacing the bookmark content.
During the development process, some data is dynamically added. If I want to add a few rows of data to a table, I cannot use the method of replacing the bookmarks. I need to add rows to the table on the document page through the program.
There are two types of operation when adding rows to a table: one is that a table already exists in the WORD template. One is that we directly add a table object to the program.
In the first case, it is necessary to note that in the table to be operated in the WORD template, there cannot be vertically merged cells, otherwise the program cannot obtain the current object to be operated, resulting in the program error. The merge of cells can be controlled in the program.
In the second case, we need to directly add the table through the program.
The code for generating the table is as follows:
1. Get the existing table in the document:
characterTable = [2];//In the collection operation of document object, the starting point starts from 1, not from 0, so you need to pay attention to it here.
2. Generate a table directly in the document. First, you need to get the location where the table is inserted, and then add the table object:
object oEndOfDoc = "\\endofdoc";//There are many predefined bookmarks in WORD, so I won't list them all here.
object oMissing = ;
Range wrdRng = .get_Item(ref oEndOfDoc).Range;//Get the end position of the current document.
(" ");//Insert a line, it cannot be used here (""). If you use this, you cannot be used in line, and I don't know why.
object oCollapseEnd = ;
object oPageBreak = ;//page break
(ref oCollapseEnd);
(ref oPageBreak);//Insert a page
(ref oCollapseEnd);
("Picture Information");
= 20;//Specify the text size of the operation object
= 1;//Specify the bold text of the operation object: 1 is bold text, 0 is normal
= ;//Specify the text layout of the operation area: centered and aligned
//The above code means: find the current end position, and then insert a page break, which is equivalent to jumping to a new page, writing the text "picture information" at the top of the new page, and specifying the text size to be 20, and displaying in bold in the center.
wrdRng = .get_Item(ref oEndOfDoc).Range;
(" ");
wrdRng = .get_Item(ref oEndOfDoc).Range;
();//Insert a paragraph, insert a table with 2 rows and one column on this paragraph.
newTable = (wrdRng, 2, 1, ref oMissing, ref oMissing);
We can also format the table, we will not list it one by one here.
3. Let’s analyze the operations on the cells of the table: merge, split. This requires us to operate according to the actual table:
//Get a specific cell (1, 1), get the cells in the first row and the first column.
Cell cell = [1].Cell(1,1);
="Text";//Specify the content of the current cell as Text
In the Table operation, add a new line:
object beforeRow = [1].Rows[2];//This line is to get the second line first
[1].(ref beforeRow);//The effect is similar to performing the [insert row] operation on the second row of this table in WORD. The inserted new row will be inserted into the previous row of the current row, and the format is also consistent with this row.
//Merge cells: It feels like merging cells here is quite stupid. You only need to specify the starting and ending cells you want to merge, and then use Merge operations.
Cell cell = [1].Cell(iRow, 2);//Column merge
([1].Cell(iRow, 6));
Cell cell1 = [1].Cell(iRow - 1, 1);//Line Merge
([1].Cell(iRow + 1, 1));
The above operations are some of the knowledge points used in this program, and there are many things that need to be familiar with and understood.
In addition, during the testing process of the program, it was found that after the document generation was executed, there were always processes in the resource manager that could not be killed. The current solution is: directly kill the process, the code is as follows:
protected void killAllProcess() // Kill all processes
{
[] myPs;
myPs = ();
foreach ( p in myPs)
{
if ( != 0)
{
string myS = "" + + " ID:" + ();
try
{
if ( != null)
if ( > 0)
{
pm = [0];
myS += "\n Modules[0].FileName:" + ;
myS += "\n Modules[0].ModuleName:" + ;
myS += "\n Modules[0].FileVersionInfo:\n" + ();
if (() == "")
();
}
}
catch
{ }
finally
{
}
}
}
}
So far, a WORD document has been generated. The above is the problems and solutions I encountered in this program development. There may be many things that are not fully considered. If you have a new understanding of WORD operations in program development, you are welcome to communicate with me and improve each other!
Below are some good excerpts online:
Create a new Word
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
Open the document:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
object fileName = @"E:";
oDoc = (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
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
object fileName = @"E:";
oDoc = (ref fileName, ref oMissing,
ref oMissing, ref oMissing);
.Add a new table
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
.Table insert rows
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
.Cell merge
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
.Cell separation
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = ( oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
object Rownum = 2;
object Columnnum = 2;
(ref Rownum, ref Columnnum);
Control insertion through paragraph
object oMissing = ;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
oPara1;
oPara1 = (ref oMissing);
= "Heading 1";
= 1;
= 24; //24 pt spacing after paragraph.
();
2. Customize the template file of the WORD document, operate the WORD template in C#, and generate a new WORD document.
The first solution is simple. You only need to change the file extension, but there are some problems, such as the loss of the generated WORD document style. This may be an unpassable solution for customers. The second solution is relatively complex, and requires calling OFFICE's WORD component to operate WORD through C# to generate WORD. This method is similar to our background splicing data in C#. Although it is troublesome, it can be customized flexibly, just to operate WORD objects.
After repeated consideration, it was decided to use the second method to generate the WORD report document.
Through my own practice, this requirement has finally been solved. In the actual development process, I encountered various problems. Fortunately, by constantly searching for network resources and combining the actual development situation, the problems have been solved. Let me summarize some of my understanding and experience during the development process:
Under the VS2008 platform, refer to .net-.12, so that you can use WORD objects to operate in the program.
Through simple execution, an error of 80070005 was reported. This error is because the permissions are insufficient, and the operation permissions of .net and IIS users need to be changed in the DCOM configuration. The specific modification process is as follows: Solution 1:
1. Control Panel -> Management Tools -> Component Services -> Computer -> My Computer -> DCom Configuration -> After finding the Microsoft Word document, click Properties to open the properties dialog box of this application.
2. Click the Identification tab and select Interactive User.
3. Click the "Security" tab, select "Custom" in the "Start and Activate Permissions" and "Access Permissions" groups, and then Customize->Edit->Add Account and IUSER_Computer Name.
4. Make sure each user is allowed to access and click OK.
5. Click OK to close DCOMCNFG.
If the above method cannot solve the problem, it should be a permission problem. Please try the following method:
Use identity simulation in the <identity impersonate="true" userName="your username" password="password"/> in the <> section
</>
After solving the above problem, I began to consider how to create a WORD template file. The WORD template file is actually used to add content through bookmarks. That is, by creating bookmarks in the WORD document, then obtaining all bookmarks of the template file in the program, and generating the document by assigning values to the bookmarks.
The operation process in the program is as follows:
Declare the object of the WORD program → Declare a WORD document object → Get the current operation document object → Get all bookmarks of the document → Assign database data to the corresponding bookmark → Save the document as a specified folder.
The following will analyze the specific code implementation for agricultural plant test reports:
Copy the codeThe code is as follows:
//Generate WORD program object and WORD document object
appWord = new Application();
doc = new Document();
object oMissing = ;//I never figured out what this is-_-
//Open the template document and specify the document type of doc
object objTemplate = (p_TemplatePath);
object objDocType = ;
doc = (Document)(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//Get all bookmarks in the template
Bookmarks odf = ;
string[] testTableremarks = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker"};
string[] testTablevalues = { "ApplyNo", "AuditingDate", "Auditor", "CheckDate", "Checker",};
//Cycle all bookmarks and assign values to the bookmarks
for (int oIndex = 0; oIndex < ; oIndex++)
{
obDD_Name = WD + testTableremarks[oIndex];
.get_Item(ref obDD_Name). = p_TestReportTable.Rows[0][testTablevalues [oIndex]].ToString();//The Range is also a very important object in WORD, which is the area where the current operation parameters are located.
}
//Step 4 Generate word, save the current document object as the specified path, and then close the doc object. Close the application
object filename = (p_SavePath) + "\\Testing_" + () + ".doc";
object miss = ;
(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = ;
object doNotSaveChanges = ;
(ref doNotSaveChanges, ref missingValue, ref missingValue);
(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
this.Hid_ShowMessage.Value = "Generated successfully!";
The above code is a process of generating WORD through template files. In fact, it is just a process of replacing the bookmark content.
During the development process, some data is dynamically added. If I want to add a few rows of data to a table, I cannot use the method of replacing the bookmarks. I need to add rows to the table on the document page through the program.
There are two types of operation when adding rows to a table: one is that a table already exists in the WORD template. One is that we directly add a table object to the program.
In the first case, it is necessary to note that in the table to be operated in the WORD template, there cannot be vertically merged cells, otherwise the program cannot obtain the current object to be operated, resulting in the program error. The merge of cells can be controlled in the program.
In the second case, we need to directly add the table through the program.
The code for generating the table is as follows:
1. Get the existing table in the document:
characterTable = [2];//In the collection operation of document object, the starting point starts from 1, not from 0, so you need to pay attention to it here.
2. Generate a table directly in the document. First, you need to get the location where the table is inserted, and then add the table object:
object oEndOfDoc = "\\endofdoc";//There are many predefined bookmarks in WORD, so I won't list them all here.
object oMissing = ;
Range wrdRng = .get_Item(ref oEndOfDoc).Range;//Get the end position of the current document.
(" ");//Insert a line, it cannot be used here (""). If you use this, you cannot be used in line, and I don't know why.
Copy the codeThe code is as follows:
object oCollapseEnd = ;
object oPageBreak = ;//page break
(ref oCollapseEnd);
(ref oPageBreak);//Insert a page
(ref oCollapseEnd);
("Picture Information");
= 20;//Specify the text size of the operation object
= 1;//Specify the bold text of the operation object: 1 is bold text, 0 is normal
= ;//Specify the text layout of the operation area: centered and aligned
//The above code means: find the current end position, and then insert a page break, which is equivalent to jumping to a new page, writing the text "picture information" at the top of the new page, and specifying the text size to be 20, and displaying in bold in the center.
wrdRng = .get_Item(ref oEndOfDoc).Range;
(" ");
wrdRng = .get_Item(ref oEndOfDoc).Range;
();//Insert a paragraph, insert a table with 2 rows and one column on this paragraph.
newTable = (wrdRng, 2, 1, ref oMissing, ref oMissing);
We can also format the table, we will not list it one by one here.
3. Let’s analyze the operations on the cells of the table: merge, split. This requires us to operate according to the actual table:
//Get a specific cell (1, 1), get the cells in the first row and the first column.
Cell cell = [1].Cell(1,1);
="Text";//Specify the content of the current cell as Text
In the Table operation, add a new line:
object beforeRow = [1].Rows[2];//This line is to get the second line first
[1].(ref beforeRow);//The effect is similar to performing the [insert row] operation on the second row of this table in WORD. The inserted new row will be inserted into the previous row of the current row, and the format is also consistent with this row.
//Merge cells: It feels like merging cells here is quite stupid. You only need to specify the starting and ending cells you want to merge, and then use Merge operations.
Cell cell = [1].Cell(iRow, 2);//Column merge
([1].Cell(iRow, 6));
Cell cell1 = [1].Cell(iRow - 1, 1);//Line Merge
([1].Cell(iRow + 1, 1));
The above operations are some of the knowledge points used in this program, and there are many things that need to be familiar with and understood.
In addition, during the testing process of the program, it was found that after the document generation was executed, there were always processes in the resource manager that could not be killed. The current solution is: directly kill the process, the code is as follows:
Copy the codeThe code is as follows:
protected void killAllProcess() // Kill all processes
{
[] myPs;
myPs = ();
foreach ( p in myPs)
{
if ( != 0)
{
string myS = "" + + " ID:" + ();
try
{
if ( != null)
if ( > 0)
{
pm = [0];
myS += "\n Modules[0].FileName:" + ;
myS += "\n Modules[0].ModuleName:" + ;
myS += "\n Modules[0].FileVersionInfo:\n" + ();
if (() == "")
();
}
}
catch
{ }
finally
{
}
}
}
}
So far, a WORD document has been generated. The above is the problems and solutions I encountered in this program development. There may be many things that are not fully considered. If you have a new understanding of WORD operations in program development, you are welcome to communicate with me and improve each other!
Below are some good excerpts online:
Create a new Word
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
Open the document:
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
object fileName = @"E:";
oDoc = (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 the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
object fileName = @"E:";
oDoc = (ref fileName, ref oMissing,
ref oMissing, ref oMissing);
.Add a new table
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
.Table insert rows
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
.Cell merge
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
.Cell separation
Copy the codeThe code is as follows:
object oMissing = ;
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = ( oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
tableLocation = (ref start, ref end);
(tableLocation, 3, 4, ref oMissing, ref oMissing);
newTable = [1];
object beforeRow = [1];
(ref beforeRow);
cell = (1, 1);
((1, 2));
object Rownum = 2;
object Columnnum = 2;
(ref Rownum, ref Columnnum);
Control insertion through paragraph
Copy the codeThe code is as follows:
object oMissing = ;
object oEndOfDoc = "\endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new ();
= true;
oDoc = (ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
oPara1;
oPara1 = (ref oMissing);
= "Heading 1";
= 1;
= 24; //24 pt spacing after paragraph.
();