Word makes database fetch data

Source: Internet
Author: User

/**
* Word Export
* @throws documentexception
*/
public void Exeportword () throws documentexception{
Put it under Tomcat.
String formname= "word";
String pathname=servletactioncontext.getrequest (). GetSession (). Getservletcontext (). Getrealpath ("") + "\\export\\" +formname+ ". Doc";

4.1 Defining a table header
String tablename= "T_user";
String tablecname = selectdataservice.getstring ("Select Comments from user_tab_comments WHERE table_name = '" +tablename+ "‘");



4.2 Get the Chinese comment for the table
List<map> list=selectdataservice.queryforlist ("Select t.column_name,t.comments from User_col_comments t where table_name = ' "+tablename+" ' ");
4.2 Creating a list
List<string> columns=new arraylist<string> ();
Map<string, string> colmap=new treemap<string, string> ();
for (Map map:list) {

Take the comments out of the database and put them in the columns.
Columns.Add (Map.get ("COMMENTS"). toString ());
Put the Chinese note of the repository into the map collection
Colmap.put (Map.get ("COMMENTS"). ToString (), Map.get ("column_name"). ToString ());

}

FileOutputStream Outputstream=null;
Document Document=null;
try {


Create an output stream
Outputstream=new FileOutputStream (pathName);

Create Document Object
Document=new Document (PAGESIZE.A4);

The output stream is associated with the document
Rtfwriter2.getinstance (document, OutputStream);

Open Document
Document.open ();

Defining fonts
Basefont CreateFont = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded);
Font font=new font (createfont,10,font.bold);

Write font
Paragraph paragraph=new Paragraph ("User Management", font);
Put him in the middle.
Paragraph.setalignment ("center");
Save to document
Document.add (paragraph);

Line break
Document.add (new Paragraph ("\ n", font));


Remove all data from the database
List<map> datalist=selectdataservice.queryforlist ("select * from" +tablename);

Write a form

Pdfptable pdf= New Pdfptable (Columns.size ());

Pdfpcell Cell=null;
Create a table
for (int i=0;i<11;i++) {

String Zhang = columns.get (i);
Cell=new Pdfpcell (New Phrase (Zhang, font));
Pdf.addcell (cell);
}



for (int i = 0; i < datalist.size (); i++) {

Get a row of data
Map Cols=datalist.get (i);

A J loop is a loop that loops through each value in a single piece of data
for (int j = 0; J < Cols.size (); j + +) {

Put it in every cell.
String Fangyuange = Columns.get (j);

Go to English writing section
String ing = colmap.get (fangyuange);

Put in a list of
Object value = cols.get (ing);

Create cells (through rows) into cells
Cell = new Pdfpcell (New Phrase ("+value,font)");
Pdf.addcell (cell);

}
}



Document.add (PDF);

// Servletactioncontext.getresponse (). Getwriter (). Write ("OK");


} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Document.close ();
try {
Outputstream.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Fileutil.downloadfile (Servletactioncontext.getrequest (), Servletactioncontext.getresponse (), PathName, formName+ "  . doc "); Look at the picture
// return null;

}



Word makes database fetch data

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.