1.Preface
Please read the following article before reading
Http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html
Two additional attachments
1,Docx. dll
2,Docx open-source documentation(Because the original author is very lazy, the development documentation is written to v1.0.0.10. This is something I have found for a long time. It may not be displayed when I open it. Right-click it and it seems that there is an option like harm, it's not a virus, you know)
Docx original author is, forget what name, give a connection, support original http://docx.codeplex.com/
2,DocxMain advantages
The latest docx component version v1.0.0.12 has the following features:
1. Supports inserting, deleting, and replacing texts in files. All text formats, such as font, underline, and highlight, are supported.
2. Supports inserting images, hyperlinks, tables, headers and footers, and custom attributes.
3. Supports jquery-like chained writing, which facilitates programming and development.
4. You can use
5. lightweight and fast reading and writing. I have used aspose and WPS for development and have not performed specific test analysis, but I can feel that the speed is much faster.
3,DocxDisadvantages
1. The functions are not perfect, but they can meet the needs of the masses.
2. Offline 2013 is not a skill. It can be opened only in offline or later versions. has some problems.
4My Development Environment
1. win7
2. vs2010
5, Implemented functions-Insert table data
Today, let's talk about how to insert data into a table in Word. This should also be a relatively practical function. It can be said to add good information to the above.
Ke, I want to say two more words here. First of all, I need to use it in my actual project. I will not use an Excel worksheet according to the project's needs. Otherwise, I will use npoi, then I took a lot of detours, from aspose components to WPS components. After two weeks of hard work, I finally gave up them. aspose was too slow and bloated, WPS occasionally has bugs (which may be my technical problem). In the end, it is recommended that you use this lightweight docx component. The following is the question.
As experienced programmers know, loop traversal is involved in table data. That's right, I implemented this function to traverse.
The following code and comments are provided: (Note that I inserted data on the basis of the template)
String path; stringdocname; // first, use using to put the code of the initialization and loading template in brackets: Using (docxdocx = docx. load (docpath) // {// Replace the string mark I made in the document with the data (such as "project_name") docx IN THE able. replacetext ("project_name", DT. rows [0] ["projectname"]. tostring (); docx. replacetext ("leader_installer", DT. rows [0] ["engineer"]. tostring (); docx. replacetext ("production_info", DT. rows [0] ["moduleid"]. tostring (); docx. replacetext ("project_duration", "y no t"); docx. replacetext ("report_date", datetime. now. tostring ("yyyy-mm-dd hh: mm: SS"); docx. replacetext ("current_session", datetime. now. tostring ("y no t"); // instantiate a table class in docx (used to insert Table data) tablenewtable = docx. tables [1]; // The second table in the template is used here. // instantiate a formatting class in docx (used to format data) and add some formats, the font is red formattingformating = new formatting (); formating. fontcolor = system. drawing. color. red; // formating. spacing = 200; // The table for (INT I = 0; I <DT. rows. count; I ++) {// Insert the field name newtable in row I. rows [I]. cells [0]. insertparagraph (DT. rows [I] ["name"]. tostring (), false, formating); // Insert the field titlenewtable in row I. rows [I]. cells [1]. insertparagraph (DT. rows [I] ["title"]. tostring (), false, formating); // append a newtable row to the end of the table. insertrow ();} docname = "/" + datetime. now. tostring ("yyyy-mm-dd") + "project daily report.docx"; Path = folders. generatefiles. project + docname; Path = newwebservice (). server. mappath (PATH); // save it as docx. saveas (PATH );}
Although the above method is simple, the disadvantage is that
The formatting class is not fully formatted, And the Row Height cannot be controlled. You must control the row in the row class.
After research, the following is another method, which is a little more complex than the above example, but can control the format more flexibly,
Stringpath; stringdocname; using (docxdocx = docx. load (docpath) {docx. replacetext ("project_name", DT. rows [0] ["projectname"]. tostring (); docx. replacetext ("leader_installer", DT. rows [0] ["engineer"]. tostring (); docx. replacetext ("production_info", DT. rows [0] ["moduleid"]. tostring (); docx. replacetext ("project_duration", "y no t"); docx. replacetext ("report_date", datetime. now. tostring ("yyyy-mm-DDHH: mm: SS"); docx. replacetext ("current_session", datetime. now. tostring ("y no t"); // dictionary <string, customproperty> lists = docx. customproperties; tablenewtable = docx. tables [1]; for (INTI = 0; I <DT. rows. count; I ++) {// obtain row I rowrow = newtable. rows [I]; // sets the high row of row I. height = 30d; // set the vertical center row in column 1st of row I. cells [0]. verticalalignment = verticalignment. center; // obtain the first section of row I, which is basically the cell in column 1st of row I, I think the paragraph should also be a separate grid, which is equivalent to a section in the cell.) paragraphparagraph = row. paragraphs [0]; // put the data with the database field name in the section, and add the red paragraph. append (DT. rows [I] ["name"]. tostring ()). color (system. drawing. color. red); // The following is repeated, and the row is the same. cells [1]. verticalalignment = verticalignment. center; paragraphparagraph1 = row. paragraphs [1]; paragraph1.append (DT. rows [I] ["title"]. tostring (); paragraph1.color (system. drawing. color. red); newtable. insertrow ();} docname = "/" + projectdaily report.docx "; Path = newwebservice (). server. mappath (PATH); docx. saveas (PATH );}
After reading the code, you will find that the formatting class is omitted here, because I find that I cannot use it, and I haven't found out how to use it for a long time... If you have any good methods, please do not give me any further information. What's wrong with us?