もともと、Android側にドキュメントを生成したかったのです(これには必要です...)。最終的には、Androidで完璧な良い方法はありません。最終的には、サーバーにのみ移動することができます。無駄にしないでくださいが、各フレームワークがAndroidとその特性をサポートしない理由を記録します。 Javaに関連するこのようなフレームワークはまだたくさんあり、一部は悪くありませんが、残念ながらAndroidをサポートしていないか、料金で請求して低価格を持っています。
個人テストの後、Javaをサポートしていない多くのAWTパッケージをAndroidで直接使用することはできません。 Freemarkerは非常に優れており、複雑で美しいドキュメントを生成できますが、残念ながらAndroidはサポートしていません。 POIを使用してAndroidで実行できますが、バージョン、フォーマットなどのために多くの落とし穴を経て、WFSで開くようにはまだ発生しています。 jwordとaspose.wordは完全にサポートでき、Jword保護観察期間はわずか30日で、両方とも両方が充電されます。 itextにはテストはありませんが、Androidをサポートしていないと言われています。
方法1:フリーマーカー
この方法では、ドキュメントテンプレートを手動で作成し(写真にプレースホルダーを使用することを忘れないでください)、XMLファイルとして保存する必要があります。特定のタグ$ {}のコンテンツを動的に置き換えることにより生成します。例:
まず、レンダリングをしましょう。
public class docutil {public configuration configuration = null; public docutil(){configure = new Configuration(configuration.version_2_3_22); configure.setDefaultEncoding( "utf-8"); } /*** doc templateに基づいて単語ファイルを生成* @param datamapテンプレートに入力する必要があるデータ* @param downloadtypeファイル名* @param save save path* /public void createdoc(map <string、object> datamap、string downloadtype、string savepath){ / //テンプレートデバイスメソッドとパスを設定すると、FreeMarkerは複数のテンプレートロードメソッドをサポートします。サーブレット、クラスパス、およびデータベースマウントをリロードできます。 //テンプレートファイルをロードして、testdoc configure.setclassfortemplateloading(this.getClass()、 "/testdoc");に配置します。 // Object wrapperを設定// configure.setObjectWrapper(new DefaultObjectWrapper()); //例外ハンドラーconfigure.settemplateExceptionHandler(templateExceptionHandler.ignore_handler); //テンプレートオブジェクトを定義します。テンプレートタイプ名は、downloadTypeテンプレート= configure.getTemplate(downloadType+"。xml")と一致することに注意してください。 file outfile = new file(savepath); writer out = null; out = new BufferedWriter(new outputStreamWriter(new FileOutputStream(Outfile)、 "UTF-8")); template.process(datamap、out); out.close(); } catch(ioexception e){e.printstacktrace(); } catch(templateException e){e.printstacktrace(); }} public string getimagestr(string imgfile){inputstream in = null; byte [] data = null; try {in = new fileInputStream(imgfile); data = new byte [in.abailable()]; in.read(data); in.close(); } catch(filenotfoundexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); } base64Encoder encoder = new Base64Encoder(); return encoder.encode(data); }} public class testdoc {public static void main(string [] args){docutil docutil = new docutil(); map <string、object> dataMap = new Hashmap <string、object>(); datamap.put( "name"、 "Joanna"); datamap.put( "Examnum"、 "111111111111111111111111111111111111111111111111年目"); datamap.put( "idcard"、 「2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222チ22222222222222222222222222222222222222222222222222222222222222222222222222222222222; DataMap.put( "c1")、「DrivingSchool」(bushtype "、" dataMap.put); docutil.getimagestr( "d://img//userimg1.png"); datamap.put( "firstexamscores"、 "0ポイント、failed"); datamap.put( "firstpic2"、docutil.getimagestr( "d://img//firstpic2.png")); "12:46:50-13:05:37"); datamap.put( "secondexamscores"、 "90ポイント、パス"); datamap.put( "secondDeductitem"、 ""); datamap.put( "secondpic1"、docutil.getimagestr( "d://img//secondpic1.png")); datamap.put( "secondpic2"、docutil.getimagestr( "d://img//secondpic2.png")); datamap.put( "secondpic3"、docutil.getimagestr( "d://img//secondpic3.png")); docutil.createdoc(datamap、 "Basedoc"、 "d://yanqiong.doc"); }} XMLファイルが長すぎるので、私はそれを投稿しません...
最後に、Androidを使用できない理由:http://stackoverflow.com/questions/25929542/use-freemarker-library-in-android
リストとラインブレークの動的な表示に関する追加の質問
要件は明確です。上記の控除項目では、控除項目がいくつかある場合、各ラインブレークを表示したいと思います。
表示するコンテンツにNewLinesを直接追加することは効果がなく、Newlineとして機能しません。
<#List> </list>などのFTLタグを追加すると、XMLで認識されない問題が発生し、プロジェクトが実行できません。
解決する:
複数の控除項目を表示する必要がある位置にラインブレークを追加して追加します。
<#list firstdeductitem as firstItem>
<w:t> $ {firstItem} </w:t> <w:br/>
</#list>
に変更してください:
List <String> strs = new ArrayList <String>();
strs.add( "111111111111111111111111111111111111111111111111111111111111111111年目)
Strs.add("2222222222222222222222222);
strs.add( "33333333333333");
datamap.put( "firstDeductitem"、strs);
それをdocutil.javaに変更します:
//テンプレートオブジェクトを定義します。テンプレートタイプ名はdownloadTypeと一致することに注意してください
template = configure.getTemplate(downloadType+"。ftl");この時点で、XMLファイルはエラーを報告し、もちろん、プロジェクトをコンパイルして実行することは不可能です。 .xmlファイルを.ftlファイルに変更して保存する必要があります。次に、コンパイルして実行し、レンダリングを実行します。
方法2: POI
この方法を使用して多くのバージョンの問題に遭遇しました。これはPOI3.7+Word2007に基づいており、テストは完全に実行できます。
Word2007を使用してドキュメントテンプレートを手動で生成する必要があります(他の世代を使用するとエラーが発生します:ファイルを開くことはできません)。上記と同様に$ {}で動的に更新する必要があるコンテンツを置き換える必要はありませんが、XMLドキュメント形式として保存する必要はありません。
/** * xwpfdocumentをカスタマイズし、createpicture()method * @author joanna.yan * */public customxwpfdocumentを拡張するxwpffdocument {public customxwpfdocument(inputstream in)throws ioexception {super(in); } public customxwpfdocument(){super(); } public customxwpfdocument(opcpackage pkg)throws ioexception {super(pkg); } public void createpicture(int id、int width、int height、xwpfparagraph段落){final int emu = 9525; width *= emu;高さ *= emu; string blipid =((poixmldocumentpart)getallpictures()。get(id))。getpackagerelationship()。getId(); ctinline inline = paragraph.createrun()。getctr()。addnewdrawing()。addnewinline(); string picxml = "" + "<a:graphic xmlns:a =/" http://schemas.openxmlformats.org/drawingml/2006/main/ ">" + "<a:graphicdata uri =/"http://schemas.openxmlformats.org/drawingml/2006/picture/"> " +" <pic:pic xmlns:pic =/"http:/" http:/ "http:/" http:/ " <pic:cnvpr id =/" + id +"/"name =/" generated/"/>" + "<pic:cnvpicpr/>" + "</pic:nvpicpr>" + "<pic:blipfill>" + "<a:blip r:embed =/" " + blipid +"/"/" xmlns:r =/"http://schemas.openxmlformats.org/officedocument/2006/2006/Relationships/"/> " +" <a:stretch> " +" <a:fillrect/> " +" </a:streten> " +" + " +" </pic x =/"/" y =/"0/"/> " +" <a:ext cx =/"" + width + "/" cy =/"" + height + "/>" + "</a:xfrm>" + "<a:prstgeom prst =/" rect/">"> " +" <a:avlst/> " +" + "</a:prstgeom>" </pic:pic> " +" </a:graphicdata> " +" </a:graphic> "; inline.addnewgraphic()。addnewgraphicdata(); xmltoken xmltoken = null; try {xmltoken = xmltoken.factory.parse(picxml); } catch(xmlexception e){e.printstacktrace(); } inline.set(xmltoken); inline.setDistt(0); inline.setdistb(0); inline.setdistl(0); inline.setDistr(0); ctpositivesize2d ent = inline.addnewextent(); exter.setcx(width); exter.setcy(height); ctnonvisualdrawingprops docpr = inline.addnewdocpr(); docpr.setid(id); docpr.setname( "image"+id); docpr.setdescr( "test"); }} /** * Word 2007に適していますtry {opcpackage pack = poixmldocument.openpackage(template); doc = new customxwpfdocument(pack); if(param!= null&¶m.size()> 0){// paragraph list <xwpfparagraph> paragraphlist = doc.getParagraphs(); ProcessParagraphs(Paragraphlist、Param、doc); //テーブルIteratorの処理<xwpftable> it = doc.getTablesIterator(); while(it.hasnext()){xwpftable table = it.next();リスト<xwpftablerow> rows = table.getRows(); for(xwpftablerow row:rows){list <xwpftablecell> cell = row.getTableCells(); for(xwpftablecellセル:セル){list <xwpfparagraph> paragraphlisttable = cell.getParagraphs(); ProcessParagraphs(paragraphlisttable、param、doc); }}}}}} catch(ioexception e){e.printstacktrace(); } return doc; } / ** * process paragraph * @param paragraphlist * @param param * @param doc * / public static void processparagraphs(list <xwpfparagraph> paragraphlist、map <string、object> param、customxwpfdocument doc){if(paragraphlist!= null&¶graphlist.size()ssize()ssize()ssize()sise()) :paragraphlist){list <xwpfrun> runs = paragraph.getruns(); for(xwpfrun run:runs){string text = run.getText(0); if(text!= null){boolean issettext = false; for(entry <string、object> entry:param.entryset()){string key = entry.getKey(); if(text.indexof(key)!= -1){sittext = true;オブジェクト値= entry.getValue(); if(value instanceof string){//テキスト置換text = text.replace(key、value.toString()); } else if(value instanceof map){// picture facterment text = text.replace(key、 "");マップpic =(map)value; int width = integer.parseint(pic.get( "width")。toString()); int height = integer.parseint(pic.get( "height")。toString()); int pictype = getPictureType(pic.get( "type")。toString()); byte [] bytearray =(byte [])pic.get( "content"); bytearrayinputStream byteInputStream = new bytearrayinputStream(bytearray); try {int ind = doc.addpicture(byteinputStream、pictype); doc.createpicture(ind、width、height、paragraph); } catch(invalidformatexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }}}}}}} if(issettext){run.settext(text、0); }}}}}}}} / ** *画像タイプに従って対応する画像タイプコードを取得 * @param pictype * @return * / public static int getPictureType(string pictype){int res = customxwpfdocument.picture_type_pict; if(pictype!= null){if(pictype.equalsignorecase( "png")){res = customxwpfdocument.picture_type_png; } else if(pictype.equalsignorecase( "dib")){res = customxwpfdocument.picture_type_dib; } else if(pictype.equalsignorecase( "emf")){res = customxwpfdocument.picture_type_emf; } else if(pictype.equalsignorecase( "jpg")|| pictype.equalsignorecase( "jpeg")){res = customxwpfdocument.picture_type_jpeg; } else if(pictype.equalsignorecase( "wmf")){res = customxwpfdocument.picture_type_wmf; }} RESを返します。 }} public class testpoi {public static void main(string [] args)throws ioexception {map <string、object> param = new hashmap <string、object>(); param.put( "$ {name}"、 "joanna.yan"); param.put( "$ {examnum}"、 "00000000000001"); param.put("${IDCard}", "111111111111111111111111111111"); param.put( "$ {carmodel}"、 "c1"); customxwpfdocument doc = wordutil.generewword(param、 "d://joanna.docx"); fileoutputStream fopts = new fileoutputStream( "d://yan.docx"); doc.write(fopts); fopts.close(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。