1.怎么做一个word模板,用java调用打印功能
1、首先新建一个空白文档,并另存为“Normal.dotm(Word2007及以后版本)Normal.dot(Word2003-2007版本)”。
2、进行需要的模板设置,对字体、段落、页眉页脚,页面边距等进行设置。3、对字体、段落的设置:在空白的新文档中,右击进行对字体的设置,在设置完成后,点击“默认”按钮。
4、对页眉页脚,页面边距:同样在菜单栏,选择插入"页眉页脚"进行设置,设置后点击“默认”按钮,并同时设置“页面边距”并点击“默认”按钮。5、在全面进行模板设置后,需要保存,在操作中会出现对一些“默认的设置”如果所示,要选择“是”,“你准备将默认的字体更改为。
..,是否将此更改应用于所有基于NORMAL模板的新文档?”6、把刚才保存的模板,复制起来,在打开“C:\Users\Administrator\AppData\Roaming\Microsoft\Templates”替换原有的Normal.dotm或Normal.dot,关闭。7、测试效果:打开一个Word文档,并按下“CTRL+N”新建一个空白文档,任意输入内容,可以看到是刚才设置的模板。
java实现调用默认打印机打印文档(以D盘zhidao.txt文档为例),代码如下:import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import javax.print.Doc; import javax.print.DocFlavor; import javax.print.DocPrintJob; import javax.print.PrintException; import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.ServiceUI; import javax.print.SimpleDoc; import javax.print.attribute.DocAttributeSet; import javax.print.attribute.HashDocAttributeSet; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet;/** * 调用打印机功能 * @author Administrator**/ public class PrintDemoII { public static void main(String[] args) { DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); PrintService []pservices = PrintServiceLookup.lookupPrintServices(flavor, aset); PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); PrintService service = ServiceUI.printDialog(null, 200, 200, pservices, defaultService, flavor, aset); if(service != null){ try { DocPrintJob pj =service.createPrintJob(); FileInputStream fis = new FileInputStream("D:"+ File.separator +"zhidao.txt");//打印D盘zhidao.txt文档。DocAttributeSet das = new HashDocAttributeSet(); Doc doc = new SimpleDoc(fis, flavor, das); pj.print(doc, aset); } catch (FileNotFoundException fe) { fe.printStackTrace(); } catch (PrintException e) { e.printStackTrace(); } }else { System.out.println("打印失败"); } } }。
2.java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。 将jacob.jar加入项目。
/* * Java2word.java * * Created on 2007年8月13日, 上午10:32 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /* * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。 * word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……, 以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。 * 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表 word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代 表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/ package com.word.util; /** * * @author kdl */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Java2word { private boolean saveOnExit; /** * word文档 */ Dispatch doc = null; /** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents; /** * 构造函数 */ public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null) documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; } /** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch(); } /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");="" }="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");="" }="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");="" }="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {="" dispatch.call(selection,"homekey",new="" variant(6));="" }="" *="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询="" dispatch="" find="word.call(selection,"Find").toDispatch();" 设置要查找的内容="" dispatch.put(find,"text",tofindtext);="" 向前查找="" dispatch.put(find,"forward","true");="" 设置格式="" dispatch.put(find,"format","true");="" 大小写匹配="" dispatch.put(find,"matchcase","true");="" 全字匹配="" dispatch.put(find,"matchwholeword","true");="" 查找并选中="" return="" dispatch.call(find,"execute").getboolean();="" }="" *="" *="" 把选定内容替换为设定文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
3.java打印word
步骤:
1,用word编辑你的模板
2,模板保存为html格式
3,在代码中把你要填的值动态拼接到那个html中,
最后
byte[] bytes= bf.toString().getBytes("GBK"); //bf.tostring()得到的拼接后的字符串
response.reset();
response.setContentType("application/msword");
response.setHeader("Content-disposition", "inline; filename=case_print.doc");
response.setCharacterEncoding("GBK");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
4.java设计系统时,系统中的打印模板想依照用户上传的word或excel模
一般来说没有这种方式的,都是采用报表控件实现
但是你这种方式也可以实现就是比较考验功底
首先必须对word或者excel进行统一的规范性处理,也就是形成约束,比如版本,比如你的实现机制(例如书签或者变量)的命名等地方
其次就是sql查询数据与你实现机制的绑定,然后在相对应的变量或书签处设值
建议先看看开源报表的原理,实现有多种实现方式,你可以用poi,也可以用一些其他的组件对word进行解析,也可以编写Dephi控件,但是都必须满足以下几点
1、可维护
2、高效
3、对于多行分页的自动处理
4、对于单页排版的处理
5、如果有可能需加入图片或者图表
5.java 实现标签打印机打印
刚好我额项目中也要这个需求,还没有很好的思路,可以参考如下这个内容:
第三种方案利用word强大的排版、打印功能,把排版和打印的需求扔到word中,OA软件要做的仅仅是让数据导到word中去。JAVA读取WORD模板,通过XML读取相关的数据库数据,自动转载数据到WORD模板中,最终展现给客户打印的是WORD。
通常,客户的报表都有word格式,或政务OA中的红头文件,或院校OA中的奖学金格式,大家习惯用word制定。在客户提供的word格式中,制定标签,并且通过XML配置文件,把标签跟数据库的内容结合,达到自动装载数据又能保持原有word模板格式的效果
6.java word中表格如何排版输出
首先我用的技术是 poi
这是代码,一个工具类得调用
public class WordUtil {
/**
* 基于模板文件导出 word 文档,此方法主要是用来处理文档中需要替换的文本内容,对图片和表格无效
*
* @param templatePath
* 模板文件的路径,要求路径中要包含全名,并且模板文件只能是 07 及以上格式,即 docx 的文件
* @param destFilePath
* 导出文件的存放路径,包含文件名,例如,E:/test/小区公告.docx
* @param data
* 用来替换文档中预定义的字符串,要求预定义的字符串与 data 中的 key 值要相同
*/
public static void exportWordByTemplate(String templatePath,
String destFilePath, Mapdata) {
FileOutputStream out = null;
XWPFDocument doc = null;
try {
doc = new XWPFDocument(POIXMLDocument.openPackage(templatePath));
ListlistRun;
ListlistParagraphs = doc.getParagraphs();
for (int i = 0; i data) {
try {
WordExtractor doc = new WordExtractor(new FileInputStream(
templatePath));
String content = doc.getText();
for (String key : data.keySet()) {
content = content.replaceAll(key, data.get(key));
}
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
directory.createDocument("WordDocument", bais);
FileOutputStream ostream = new FileOutputStream(destFilePath);
fs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Mapmaps = new HashMap();
maps.put("appellation", "万达公寓业主:");
maps.put(
"main_body",
"输出的内容");
maps.put("date", "2013年1月23日");
exportWordByTemplate("E:/sss 2.docx", "E:/test/test.doc", maps);
}
}
"E:/sss 2.docx 模板存放的地址。
E:/test/test.doc 新生成的地址。
7.如何能让Java生成复杂Word文档
客户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈。
POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个20多页,嵌套很多表格和图像的word文档靠POI来写代码输出,对程序员来说比去山西挖煤还惨,况且文档格式还经常变化。
iText操作Excel还行。对于复杂的大量的word也是噩梦。
直接通过JSP输出样式基本不达标,而且要打印出来就更是惨不忍睹。
Word从2003开始支持XML格式,用XML还做就很简单了。
大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
看看实际效果
首先用office【版本要2003以上,以下的不支持xml格式】编辑文档的样式,图中红线的部分就是我要输出的部分:
将编辑好的文档另存为XML
转载请注明出处51数据库 » java打印用word做模板
MrG37088382