Java generates simple word (using the Itext tool), generates simple Excel, and downloads notes

Source: Internet
Author: User

1.java Generate simple word (with picture table)

Adding itext dependent dependencies to the POM

<dependency>       <groupId>com.lowagie</groupId>        <artifactid>itext</artifactid >       <version>2.1.7</version></dependency>         <dependency>        <groupId> com.lowagie</groupid>        <artifactId>itext-rtf</artifactId>        <version>2.1.7</ Version></dependency>

2.java related code (data and pictures can be ignored)

/*** New report (report name). Image. News ID) * *@throwsjsonexception*/@Override Publicstring Addreport (string[] img, string[] newsId, String ReportName, String loginId, String path, string img Path) {Long name=System.currenttimemillis (); String FilePath= path + file.separator + name + ". Doc"; File File=NewFile (FilePath); Report Report=NewReport ();        Report.setloginid (LOGINID);        Report.setreportname (ReportName);        Report.setdownurl (FilePath);        Filedao.addreport (report); System.out.println ("Insert Report Success" +Report.getreportno ()); Document Document=NewDocument (PAGESIZE.A4); Try{rtfwriter2.getinstance (document,Newfileoutputstream (file)); } Catch(filenotfoundexception E2) {//TODO auto-generated Catch blockE2.printstacktrace ();        } document.open (); Basefont Bfchinese=NULL; Try{Bfchinese=Basefont.createfont (Basefont.helvetica, Basefont.winansi, basefont.not_embedded); } Catch(Documentexception |IOException E1)        {E1.printstacktrace (); } Font Titlefont=NewFont (Bfchinese, 22, Font.Bold); Font Contextfont=NewFont (Bfchinese, 10, Font.normal); Paragraph title=NewParagraph ("Statistical report", Titlefont);        Title.setalignment (Element.align_center); //the Red linertfshapeposition position; Position=NewRtfshapeposition (150, 0, 10400, 170);        Position.setxrelativepos (Rtfshapeposition.position_x_relative_margin);        Position.setyrelativepos (rtfshapeposition.position_y_relative_paragraph); Rtfshape Shape=NewRtfshape (rtfshape.shape_rectangle, position); Rtfshapeproperty Property=NewRtfshapeproperty (Rtfshapeproperty.property_line_color, color.red);        Shape.setproperty (property); Paragraph P1=NewParagraph (Shape); Try {            //logoImage Imglogo = image.getinstance (Imgpath + file.separator + "Dfgx.jpg"); Imglogo.setalignment (image.left);//Set Picture Display locationImglogo.scaleabsolute (80, 50);//directly set the display sizeDocument.add (Imglogo);            Document.add (title);        Document.add (p1); } Catch(Exception e) {e.printstacktrace (); } Table Table=NULL; Try{Table=NewTable (4); intWidth[] = {40, 30, 15, 15};//set width ratio per columntable.setwidths (width); } Catch(Exception E1) {e1.printstacktrace (); } table.setborderwidth (1);        Table.setbordercolor (Color.Black); Table.setpadding (0); Table.setspacing (0); Try{Table.addcell (Title); Table.addcell (Link); Table.addcell (Channels); Table.addcell (Time); } Catch(badelementexception E1) {e1.printstacktrace (); } List<Object> list =Estoweb.selectid (newsId); String Time= ""; String Titleinfo= ""; String URL= ""; String Channel= ""; Try {             for(Object string:list) {jsonobject json=NULL; JSON=NewJsonobject (string.valueof (String)); Channel= (String) json.get ("Webcell"); Titleinfo= (String) json.get ("Titlecell"); URL= (String) json.get ("Linkcell"); time= (String) json.get ("Datecell"); Table.addcell (NewParagraph (titleinfo)); Table.addcell (NewParagraph (URL)); Table.addcell (NewParagraph (channel)); Table.addcell (NewParagraph (time)); }        } Catch(Exception e) {e.printstacktrace (); }        Try{document.add (table); } Catch(documentexception E1) {e1.printstacktrace (); }         for(String string:img) {Map<string, object> map =Filedao.findreportpath (string); System.out.println (Map.get ("Pic_path")); Image IMG1=NULL; Try{IMG1= Image.getinstance (Imgpath + file.separator + string.valueof (map.get ("Pic_path")))); } Catch(badelementexception e) {e.printstacktrace (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } img1.setabsoluteposition (0, 0); Img1.setalignment (image.align_center);//Set Picture Display locationImg1.scaleabsolute (360, 180);//directly set the display size            Try{document.add (IMG1); } Catch(documentexception e) {e.printstacktrace ();        }} document.close (); return"OK"; }

3. Generate simple Excel (reference: http://blog.csdn.net/yongqingmiao/article/details/7179452)

Pom Joins related dependencies

<dependency>            <groupId>jexcelapi</groupId>            <artifactId>jxl</artifactId>            <version>2.6</version></dependency>        

4.java Code

file file;        Writableworkbook WWB; Try{file=NewFile (Imgpath + file.separator + "News.xls"); WWB=workbook.createworkbook (file); Writablesheet ws= Wwb.createsheet ("Page 1", 0); Ws.addcell (NewLabel (0, 0, "time")); Ws.addcell (NewLabel (1, 0, "title")); Ws.addcell (NewLabel (2, 0, "link")); Ws.addcell (NewLabel (3, 0, "channel")); List<Object> list =Estoweb.selectid (newsId); String Time= ""; String Titleinfo= ""; String URL= ""; String Channel= ""; Try {                 for(inti = 0; I < list.size (); i++) {Jsonobject json=NULL; JSON=NewJsonobject (string.valueof (List.get (i))); Channel= (String) json.get ("Webcell"); Titleinfo= (String) json.get ("Titlecell"); URL= (String) json.get ("Linkcell"); time= (String) json.get ("Datecell"); Ws.addcell (NewLabel (0, i + 1, time)); Ws.addcell (NewLabel (1, i + 1, Titleinfo)); Ws.addcell (NewLabel (2, i + 1, url)); Ws.addcell (NewLabel (3, i + 1, channel));                } wwb.write ();            Wwb.close (); } Catch(Exception e) {e.printstacktrace (); }        } Catch(Exception e) {e.printstacktrace (); }

5. Download demo (relevant file name parameter reference)

InputStream InputStream =NULL; OutputStream OS=NULL; Res.setcharacterencoding ("Utf-8"); Res.setcontenttype ("Multipart/form-data"); Res.setheader ("Content-disposition", "attachment;filename=\" "+" News.xls "+" \ ""); Try {            //Download FileInputStream =NewFileInputStream (NewFile (Imgpath + file.separator + "News.xls")); OS=Res.getoutputstream (); byte[] B =New byte[4096]; intlength;  while(length = Inputstream.read (b)) > 0) {Os.write (b,0, length); }        } Catch(Exception e) {e.printstacktrace (); return"False"; } finally {            Try{Os.flush ();                Os.close ();                Inputstream.close (); if(NewFile (Imgpath + file.separator + "News.xls"). Exists ()) {NewFile (Imgpath + file.separator + "News.xls"). Delete (); }            } Catch(Exception e) {e.printstacktrace (); return"False"; }        }

Java generates simple word (using the Itext tool), generates simple Excel, and downloads notes

Related Article

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.