PageRenderTime 33ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/PrintDemo/src/com/loveplusplus/demo/print/util/WordUtil.java

https://github.com/0451liyang/StudyDemo
Java | 43 lines | 33 code | 8 blank | 2 comment | 2 complexity | 419c1341b2f0c7eb98833e52c475698e MD5 | raw file
  1. package com.loveplusplus.demo.print.util;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.util.Map;
  6. import org.apache.poi.hwpf.HWPFDocument;
  7. import org.apache.poi.hwpf.usermodel.CharacterRun;
  8. import org.apache.poi.hwpf.usermodel.Range;
  9. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  10. import android.content.Context;
  11. import android.util.Log;
  12. public class WordUtil {
  13. private static final String TAG = "WordUtil";
  14. public static void word(Context context, File distFile,String templateName,String pref,Map<String,String> map) {
  15. try {
  16. // 模板文件
  17. InputStream tempInputStream = context.getAssets().open(templateName);
  18. POIFSFileSystem fs = new POIFSFileSystem(tempInputStream);
  19. HWPFDocument doc = new HWPFDocument(fs);
  20. Range range = doc.getRange();
  21. for (int i = 0; i < range.numCharacterRuns(); i++) {
  22. CharacterRun run = range.getCharacterRun(i);
  23. String text = run.text();
  24. Log.d(TAG, text);
  25. // 替换自定义标记
  26. if (text.startsWith(pref)&&map.containsKey(text)) {
  27. run.replaceText(map.get(text), true);
  28. }
  29. }
  30. doc.write(new FileOutputStream(distFile));
  31. } catch (Exception e) {
  32. Log.d(TAG, "", e);
  33. }
  34. }
  35. }