放假了真是颓废…
咨询了某宝销售超市小票打印机的客服,只要在指定目录写入xxx.doc文件,配套的打印机软件便能自动打印,实现小票的打印功能。
需要下载Freemaker.jar并在build path中导入
首先新建一个word文件
将其中需要在java中写入内容的字段写成拼音
然后另存为xxx.xml
用xml编辑器打开生成的文件,搜索“neirong”(不带双引号),将其修改成“${neirong}”![这里写图片描述] ,同理,对其余两处进行相同操作。(https://img-blog.csdn.net/20150504164223539)
在指定目录生成xxx.doc
package com.lvkang.javaword;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class WordTest {
private Configuration configuration = null;
public WordTest() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public static void main(String[] args) {
WordTest test = new WordTest();
test.createWord();
}
public void createWord() {
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
// ftl文件所存在的位置 ftl位于包内 不是磁盘
configuration.setClassForTemplateLoading(this.getClass(),
"/com/lvkang/javaword");
Template t = null;
try {
// ftl文件名
t = configuration.getTemplate("MyFood.ftl");
} catch (IOException e) {
e.printStackTrace();
}
//输出xxx.doc的文件位置
File outFile = new File("/Users/kangkang/wordJava/"
+ new java.util.Date().getTime() + ".doc");
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter;
try {
oWriter = new OutputStreamWriter(fos, "UTF-8");
out = new BufferedWriter(oWriter);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getData(Map<String, Object> dataMap) {
// 模拟取得的数据
ArrayList<HashMap<String, Object>> orderFoodArrayList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 20; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "鸡腿");
map.put("num", 5 + i);
map.put("unitPrice", 2.0 * i);
orderFoodArrayList.add(map);
}
// 取出ArrayList中的数据
String string = "";
int addPrice = 0;
for (int i = 0; i < orderFoodArrayList.size(); i++) {
String nameString = (String) orderFoodArrayList.get(i).get("name");
String num = orderFoodArrayList.get(i).get("num").toString();
String unitPrice = orderFoodArrayList.get(i).get("unitPrice")
.toString();
String price = Integer.parseInt(num) * Float.parseFloat(unitPrice)
+ "";
addPrice += Float.parseFloat(price);
// 实现换行格式
for (int j = nameString.length(); j < 8; j++) {
nameString += " ";
}
for (int j = num.length(); j < 10; j++) {
num += " ";
}
for (int j = unitPrice.length(); j < 10; j++) {
unitPrice += " ";
}
for (int j = price.length(); j < 8; j++) {
price += " ";
}
string += nameString + num + unitPrice + price;
}
string += "共" + orderFoodArrayList.size() + "项";
dataMap.put("zongjinge", addPrice);
dataMap.put("riqi", "2015");
dataMap.put("neirong", string);
}
}
效果图
打印机目前还在路上…到了之后再进行测试。
原文地址http://blog.csdn.net/zhanwentao2/article/details/7255432