0% found this document useful (0 votes)
2 views4 pages

25836916

The document presents a method for automatically generating Word documents with text and images using C# and Office COM components, aimed at improving the convenience of report generation in information management systems. It details the process of importing COM components, creating Word documents, inserting tables, and adding data from a database. The approach has been successfully implemented in a faculty information management system, demonstrating high practical value and generality.

Uploaded by

Đỗ Ngọc Mai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

25836916

The document presents a method for automatically generating Word documents with text and images using C# and Office COM components, aimed at improving the convenience of report generation in information management systems. It details the process of importing COM components, creating Word documents, inserting tables, and adding data from a database. The approach has been successfully implemented in a faculty information management system, demonstrating high practical value and generality.

Uploaded by

Đỗ Ngọc Mai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

International Conference on Electronic Science and Automation Control (ESAC 2015)

Research and Implementation of Word Documents


Generation Based on C#
Cai Xiaoyan1, Shen Wei2, Li Longteng3, Ge Yu4, Lu Lina5, Zeng Li6
1.Wuhan Ordnance Non-commissioned Officer Academy of PLA, Wuhan, China
2. Powerchina Hubei Electric Engineering Corporation, Wuhan, China
Email:[email protected]

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

Create and open the Word document


I. INTRODUCTION
The generation of reports is fairly significant link in the
process of development of database application system. Determine the cell location of data will be written
Transferring data of database into fine reports for users is a
basic need for database application system. Although mounts
of development tools of application program have their own
reports generation system, Word,as output tool of application Read data from database
system,has many irreplaceable advantages. Firstly, it can create
documents with images, texts and tables. Secondly, it can
generate more complex reports, which are including
multi-level nested tables and diagonal tables, and so on. Data Image path Images

Thirdly, users can customize and modify the document


templates [1]. Fourthly, users do further processing for
generated documents. Fifthly, users can save and publish the Save to the specified path

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

database application system, to replace traditional report


output and automatically create Word documents with texts
and images. For example, in the information management Save the Word document with texts and images

system of faculty member, we need to combine C# and SQL


Server 2005 database to generate a Word document with
information including the personal information, teaching Release file object
information, edited material information, published paper
information and scientific research information, etc. During Fig.1. The flow chart of generating Word document
the process of design and development, I have found that we
could solve this problem effectively with Office COM As can be seen from above, generated Word document is
components provided by Microsoft. Based on the design and divided into several steps as follow.
development of faculty member’s information management
system, the paper discusses the key technology of
development of information system deeply, and the specific

© 2015. The authors - Published by Atlantis Press 312


a) COM components provide C# with basic functions Application
and properties of Word document. After importing, the
classes and interfaces in the namespaces of
Microsoft.Office.Core and Microsoft.Office.Interop.Word can Documents(Document)
be used.
b) Create and open a new Word document by using Add
and Open method of Paragraphs
Microsoft.Office.Interop.Word.Application.Documents
interface.
c) Using the method of Shapes(Shape)

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

You might also like