A little idea of exporting word with itext in Java

Source: Internet
Author: User

These days in the project team only do a few things, mainly or not to seriously say. My part is to be responsible for exporting Word documents with itext this piece, before seeing the big guys make the Excel part feel very surprised, like just touch html like feeling. But after all, their task is to be done well, so after efforts to achieve a bit of results.

First Previous

Next is the code, the code word is not easy, there are things to do ~ ~

After using Ajax in the background section from the foreground, using the Itext method to export word, it is important to note that the two parts of Document.close and Out.close, without close, are prone to errors.


public void Exportjdyword (HttpServletResponse response,httpservletrequest request,string xsid) throws unsupportedencodingexception {
for (int c=1;c<=2;c++) {
String s = "Select C_username from c_kdxsyb where c_xs_id =?" and C_xs_rq = ' "+c+" ";
list<map<string,object>> username = jt.queryforlist (s,xsid);
set<string> set = new hashset<string> ();
for (int i=0;i<username.size (); i++) {
Map <string, object> map = Username.get (i);
Object Vala = map.get (username);
if (vala!=null) {String v = vala.tostring ();
Set.add (v);}
}
Response.setcontenttype ("Application/msword");//download
String Agent = Request.getheader ("User-agent"). toLowerCase ();
String codedfilename = Java.net.URLEncoder.encode ("Supervisor Table", "UTF-8");
if (Agent.contains ("Firefox")) {
Response.setcharacterencoding ("Utf-8");
Response.setheader ("Content-disposition",
"Attachment;filename=" + New String ("Supervisor". GetBytes (), "iso8859-1") + ". Doc");
} else {
Response.setheader ("Content-disposition", "attachment;filename=" + Codedfilename + ". Doc");
}


try{

Document document = new document (PAGESIZE.A4);
OutputStream out = Response.getoutputstream ();
Rtfwriter2.getinstance (document, out);

Document.open ();//Open File

Basefont Bfchinese = Basefont.createfont (Basefont.helvetica, Basefont.winansi, basefont.not_embedded);
Font titlefont = new Font (Bfchinese, n, Font.Bold);//Title Style
Font contextfont = new Font (Bfchinese, ten, font.normal);//Text style
Table table = new Table (7, (Username.size () +4));
int [] Withs = {1,2,1,1,3,4,2};//Sets the width of each header, the following is the design table
Table.setwidths (withs);
Table.setwidth (100);
Table.setalignment (Element.align_center);
Table.setautofillemptycells (TRUE);
String titlestring = "November 2017" + (c+6) + "List of daily supervisors";
Paragraph title = new Paragraph (titlestring);
Title.setalignment (Element.align_center);
Title.setfont (Titlefont);
Document.add (title);//Add title
String contextstring = "Test Institute name: Northeast Normal University Humanities College, East 1th Building, Area B, number of examinations: 29 Inspectors: 12 Contact: Li Huiling Tel: 13844057575 location: No. 1488th, Bo Shuo Road, net month area, Changchun City On duty Tel: 0431-84537193 ";
Paragraph context = new Paragraph (contextstring);
Context.setalignment (Element.align_center);
Context.setfont (Contextfont);
Context.setspacingbefore (10);//Set the spacing to the previous paragraph
Context.setfirstlineindent (7);
Document.add (context);
Table section
cell[] Cellheaders = new cell[7];//Creating a table header
Cellheaders[0] = new Cell (New Phrase ("ordinal", Contextfont));
CELLHEADERS[1] = new Cell (new Phrase ("name", Contextfont));
CELLHEADERS[2] = new Cell (new Phrase ("level", Contextfont));
CELLHEADERS[3] = new Cell (New Phrase ("Gender", Contextfont));
CELLHEADERS[4] = new Cell (New Phrase ("Phone", Contextfont));
CELLHEADERS[5] = new Cell (New Phrase ("Work unit", Contextfont));
CELLHEADERS[6] = new Cell (New Phrase ("In charge of the examination Room", contextfont));
for (int i1 = 0; i1 < 7; i1++) {
Cellheaders[i1].sethorizontalalignment (Element.align_center);
Cellheaders[i1].setverticalalignment (Element.align_middle);
Table.addcell (Cellheaders[i1]);
}
Populating the table with data
String contextstring1= "Leader Approval: Audit: watchmakers: Wei Jianqiang Remarks: Withholding tax is deducted monthly with salary. ";
Paragraph context1 = new Paragraph (CONTEXTSTRING1);
Context.setalignment (Element.align_center);
Context.setfont (Contextfont);
Context.setspacingbefore (10);

for (int i=0;i<username.size (); i++) {
String name = Username.get (i). toString ();
Name = Name.substring (12);
Name = Name.substring (0,name.length ()-1);

for (int lie=1;lie<8;lie++) {
if (lie==2)
{
Cell Celladd = new cell (new Phrase (Name,contextfont));
Table.addcell (Celladd);
}
Else
{
Cell cell_null = new cell (New Phrase ("", Contextfont));
Table.addcell (cell_null);}
}
}
String S1 = "Select from C_kdxsyb";
Cell cell1 = new cell (New Phrase ("Ride: Bus No. 1th", Contextfont);//Add a cross-column portion of a table
Cell1.sethorizontalalignment (Element.align_center);
Cell1.setverticalalignment (Element.align_middle);
Cell1.setcolspan (4);
Table.addcell (CELL1);

Cell cell2 = new cell (New Phrase ("", Contextfont));//Add a cross-column portion of a table
Cell2.sethorizontalalignment (Element.align_center);
Cell2.setverticalalignment (Element.align_middle);
Cell2.setcolspan (3);
Table.addcell (CELL2);

Cell Cell3 = new cell (New Phrase ("No commission", Contextfont));
Cell3.sethorizontalalignment (Element.align_center);
Cell3.setverticalalignment (Element.align_middle);
Cell3.setcolspan (4);
Table.addcell (CELL3);

Cell CELL4 = new cell (new Phrase ("Leader: Zhao 18686420022", Contextfont));
Cell4.sethorizontalalignment (Element.align_center);
Cell4.setverticalalignment (Element.align_middle);
Cell4.setcolspan (3);
Table.addcell (CELL4);

Cell CELL5 = new cell (New Phrase ("Security officer", Contextfont));
Cell5.sethorizontalalignment (Element.align_center);
Cell5.setverticalalignment (Element.align_middle);
Cell5.setcolspan (4);
Cell5.setrowspan (2);
Table.addcell (CELL5);

Cell cell6 = new cell (New Phrase ("Li Weidong 15904405903", Contextfont));
Cell6.sethorizontalalignment (Element.align_center);
Cell6.setverticalalignment (Element.align_middle);
Cell6.setcolspan (3);
Table.addcell (CELL6);

Cell CELL7 = new cell (New Phrase ("Sun Chengfu 15904407150", Contextfont));
Cell7.sethorizontalalignment (Element.align_center);
Cell7.setverticalalignment (Element.align_middle);
Cell7.setcolspan (3);
Table.addcell (CELL7);

Document.add (table);
Document.add (CONTEXT1);
Document.close ();
Out.close ();

} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (Documentexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

}

A little idea of exporting word with itext in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.