25836916
25836916
Abstract—To solve the problem that report forms generated ideas and method to export data of database into Word
in information management system is not convenient for documents with texts and images.
modification and conservation, a method of generating Word
documents with texts and images automatically is put forward II. DESIGN IDEAS
based on COM components of Office. The realization of the In order to export data of database into Word document,
method is discussed in detail, which is chiefly used for in depth steps are as follow. First, we need to import COM
office function development. At present, the method has been components, create and open Office document in the program.
applied in the information management system of faculty Then read data or images of database and write down it into
member successfully to implement the automatic generation of
Word. At last, save the corresponding Word document and
Word documents. The operation practices show that the method
has high practical value and good generality.
release the memory of the Word object. The flow chart of
specific design idea is shown in Fig.1 below.
Keywords—C#; Word Documents; COM components Import COM components
generated documents.
It is the problem developers often have to face that how to
combine Word’s powerful document processing capacity with Write data Insert images
Microsoft.Office.Interop.Word.Document.Tables.Add to
insert tables.
Tables(Table)
d) After inserting a table into new Word document, we
need to determine the cell location of data will be written.
e) Read data from database, and choose different Sections(Section)
written ways according to different types of data.
f) Save the document through the method of
Fig.2. The structure of Word documents
Microsoft.Office.Interop.Word.Document.SaveAs.
III. IMPLEMENTATION OF THE KEY TECHNOLOGY Supposing someone would like to operate the Word
document, he/she should create a Word object first. As Word
A. Import COM Components is a controller and can’t be operated direct, we need save the
Suppose we would like to generate a Word document Word object into Document object. Then the Word document
automatically in Visual Studio 2008, we should accomplish it could be operated.
by using the “Microsoft Office 11.0 object library” COM
components of Office. Adding this component should operate Word.Application wordApp = new
in the environment of VS2008, and under the circumstance Word.ApplicationClass();
that the building project application under open, you should Word.Document wordDoc =
select the project menu and select the add reference, and then wordApp.Documents.Add(ref Nothing, ref Nothing, ref
choose COM from pop-up menu of add reference, select Nothing, ref Nothing);
“Microsoft Word 11.0 object library”, then click the OK
button. By this way, user can access or create a Word wordApp.Visible = true;
document in the project [2]. C. Insert and Set Table in Word
Following two kinds of classes are mainly involved in 1) Insert Table
encoding. The collection of tables is member of
Microsoft.Office.Interop.Word.Document, Microsoft.Office.
a) Microsoft.Office.Interop.Word.Application.Class is Tools.Word.Document , Selection and Range. It means we
the application class of Word and provides application can create a table in any context, and can insert table in
handing function to Word. designated range by using Add method of tables’ collection.
b) Microsoft.Office.Interop.Word.Document is the
document class of Word and provides document handing In order to insert table in Word, we should set the rows
function in proportion to specific disk files. and columns of table and the foot style of cell. These
B. Create Word Document operations are all set in Tables of document object wordDoc.
All the Word documents are made up of mounts of objects, Object missingValue = Type.Missing;
and these objects together with properties value determine the
structure and format of the documents, as shown in Fig.2 wordDoc.Tables.Add(wordApp.Selection.Range, row number
below. The object of Word represents an element of Word, of table, column number of table, ref missingValue, ref
Such as: document, paragraph, bookmarks or character. missingValue); //insert row and column information of table
Collection is an object, and property is a feature of the object The table will be added to tables’ collection of
or one aspect of this object’s operation. For example, Microsoft.Office.Interop.Word.Document when it is
Document’s properties are including name, content, condition, created.And then,you can use the Item property to refer to the
and so on [3]. table according to the Item number.
2) Merge Cells
Merge each cell as its position in table by using Merge
method.
wordDoc.Tables.Item(1).Cell(1,5).Merge(wordDoc.Tables
.Item(1).Cell(5,6));
313
The code above is merging the row from first to sixth of // DS, the image name of querying; wordDoc, Word
the fifth column in table. document object
3) Add Rows and Columns //flag, the number of images inserted to the document. For
Cells are organized into rows and columns in Microsoft example, flag=1, insert one image; flag =3, insert three
Office Word table. We can use the Add method of Rows images
object to add rows in table, so is the Column.
Object start = Type.Missing;
wordDoc.Tables.Item(1).Rows.Add(ref beforeRow);
Object end = Type.Missing;
wordDoc.Tables.Item(1).Columns.Add(ref
Word.Range P_Range = wordDoc.Range(ref start ,ref
beforeColumn);
end); //the location of images will be inserted
D. Add Texts in Cells Object P_Ranges = P_Range;
Each table is including one group of cells and each cell
object represents one cell in table. You refer to each cell by its object missingValue = Type.Missing;
location in the table and add texts into it, as well as apply the
string[] str = newstring[flag];
format settings.
for (int i = 0; i < DS.Tables[0].Rows.Count; i++)
wordDoc.Tables.Item(1).Cell(1,1).Range.Text = “name”;
{
Way introduced above is directly generating word
document of faculty information in program, and also for (int j = 0; j < flag;j++ )
inserting and setting the tables in Word as well as writing the
faculty information. This way is really safety but not flexible. {
Once the faculty information is changed, you need to rewrite str[j] = DS.Tables[0].Rows[i][j].ToString();
the program to design tables and regenerate the Word
document of faculty information. If the format of Word if (File.Exists(PicFilePath + "\\" + str[j]))
document generated is fixed,you can create a table to insert {
data in the word template and the style of table is set manually
outside the program. object LinkToFile = false;
Therefore,before the Word document of faculty object SaveWithDocument = true;
information can be generated in the application,you can save wordDoc.InlineShapes.AddPicture(PicFilePath
the fixed format of word as a Word document template, set + "\\" + str[j], ref LinkToFile, ref SaveWithDocument, ref
the format of it (such as font style, size, paging, picture size, P_Ranges);
etc.) and define Word application object and Word document
object in the program. After that you can extract the faculty’s }
information data from the database, and automatically calls }
the Word template document in the program, then input the
faculty information among them. }
E. Insert Picture to the Word Document wordDoc.Save();
Supposing the image is stored in a database. If you want to
}
insert the image in the cell, first you need to read the image
from the database and save the image to the PictureBox
control which is created dynamically, and then save it to the
specified path through the Image.Save() method of IV. CONCLUSION
PictureBox, and then use the Inline Shapes. AddPicture() In this paper, the author’s innovations are as follows: by
method to insert image under the specified path to the using the Visual Studio development platform and the COM
specified cell through the clipboard. components technology based on Office, combining with
Mentioned above is just adding one image to the specified Microsoft SQL Server 2005 implement the automatic
cell in the word, if you want to add multiple images of the generation of Word documents of faculty information.It is
database into the Word document, how to do? convenient for faculty annual assessment or title appraisal for
fixed format document. Users can not only modify and save
If all images are stored in local disk, first you need to the generated Word documents, and also can carry on the
query images’ path from the database and fill them to the page setup, print control, etc. Seeing from the application, the
DataSet,then call the ExportPic_To_Word() method, which Word documents generated automatically are illustrated and
will save the specified images to the Word document. widely useful, and the effect is ideal and worth promoting.
publicvoid ExportPic_To_Word(DataSet DS, int flag,
Word.Document wordDoc)
{
314
REFERENCES Management System of Test Questions Library and Test Papers. Journal
of Hebei Normal University of Science & Technology, vol. 23, pp.
[1] Ye Ming, Zhang Zheng. Development of Word report generation function 59-63, April 2009.
based on C#.netComputer Engineering and Application,vol. 44, pp.
104-106, September 2008. [3] Li Shengguang, Jia Lei, Wang Dezhou. Achieving Office Software
Automation Using C++Builder. Microcomputer Information, vol. 18,
[2] Chen Xiumin, Yan Zhongwen, Zhu Meining, Yang Yanping, Liu Aiyong, pp.64-66, October 2002.
Lin Zuo. The research of Some Key Technologies in .NET based
315