日期:2014-05-20 浏览次数:20876 次
import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class DOCWriter { public static String fileToWrite = "e:\\workspace\\TestPoi2\\test.doc"; public static void main(String[] args) throws Exception{ String content = "测试"; byte b[] = content.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(b); POIFSFileSystem fs = new POIFSFileSystem(); FileOutputStream ostream = new FileOutputStream(fileToWrite); fs.writeFilesystem(ostream); bais.close(); ostream.close(); } }
import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class DOCWriter { public static void main(String[] args) throws Exception{ byte[] a=new String("看见了!").getBytes(); FileOutputStream fos; ByteArrayInputStream bs = new ByteArrayInputStream(a); POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry directory = fs.getRoot(); DocumentEntry de = directory.createDocument("WordDocument", bs); //以上两句代码不能省略,否则输出的是乱码 fos = new FileOutputStream("e:\\text.doc"); fs.writeFilesystem(fos); bs.close(); fos.flush(); fos.close(); } }
public static void WriteWord(String fileUrl, String fileName, Object bfo) { // ActiveXComponent app = new ActiveXComponent("Word.Application"); // // 要转换的word文件 // String inFile = fileUrl + "test.doc"; // // 要报存的目标文件 // String tpFile = fileUrl + Date2.getDate() + Date2.getTime() // + "test.doc"; // boolean flag = false; // Dispatch xlo = (Dispatch) app.getObject(); // // boolean visible = false; // // String bookMarkKey = "LB_KJGG"; // try { // // 设置word不可见 // app.setProperty("Visible", new Variant(visible)); // Dispatch docs = app.getProperty("Documents").toDispatch(); // Dispatch doc = Dispatch.invoke( // docs, // "Open", // Dispatch.Method, // new Object[] { inFile, new Variant(false), // new Variant(true) }, new int[1]).toDispatch(); // 打开word文件 // // // java反射 // Class clazz = bfo.getClass(); // // Field[] fields = clazz.getDeclaredFields();// 所有属性 // Field[] fields = clazz.getDeclaredFields(); // // // for (int i = 0; i < fields.length; i++) { // Dispatch selection = app.getProperty("Selection").toDispatch(); // // 得到一个组件 // Dispatch find = app.call(selection, "Find").toDispatch(); // // 查找什么文本 // Dispatch.put(find, "Text", "${" + fields[i].getName() + "}"); // // 替换文本 // Dispatch.call(find, "ClearFormatting"); // Dispatch.put(find, "Text", "${" + fields[i].getName() + "}"); // Dispatch.call(find, "Execute"); // // if (null == fields[i] // // || "null".equals(fields[i].get(bfo).toString())) { // // Dispatch.put(selection, "Text", ""); // // } else { // Dispatch.put(selection, "Text", fields[i].get(bfo).toString()); // // } // // } // // // 保存文件 // Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { // tpFile, new Variant(0) }, new int[1]); // // 作为word格式保存到目标文件 // Variant f = new Variant(false); // Dispatch.call(doc, "Close", f); // flag = true; // } catch (Exception e) { // e.printStackTrace(); // } finally { // app.invoke("Quit", new Variant[] {}); // // } // 要转换的word文件 String inFile = fileUrl + "wordModel.doc"; // 要报存的目标文件 String tpFile = fileUrl + Date2.getDate() + Date2.getTime() + "wordModel.doc"; Document doc; try { doc = new Document(); doc.open(inFile); // java反射 Class clazz = bfo.getClass(); // Field[] fields = clazz.getDeclaredFields();// 所有属性 Field[] fields = clazz.getDeclaredFields(); // for (int i = 0; i < fields.length; i++) { if (fields[i].get(bfo) == null) { doc.replaceAll("${" + fields[i].getName() + "}", ""); } else { doc.replaceAll("${" + fields[i].getName() + "}", fields[i] .get(bfo).toString()); } } doc.saveAs(tpFile); doc.close(false); } catch (Exception e1) { e1.printStackTrace(); } try { Object obj = CommonUtil.downloadFileEx(tpFile); CommonUtil.downLoadFile(obj, fileName, "application/msword"); File file = new File(tpFile); file.delete(); } catch (Exception e) { e.printStackTrace(); } }