弄了一周了,终于好了.最近我们公司在做人脸识别技术,识别完之后需要打印word(printShare实现).下面就看一下怎么实现吧.
首先在我们的项目里需要word文档,放在raw下或者是res下面,我这里是在raw下面写的.
然后还需要下载poi包,,当然下载这个需要积分,下载完之后直接导入到项目中,接着连接数据库,我这里是jtds连接的SqlServer数据库,需要下载jar包,注意是1.2.7版本的.连接SqlServer数据库,这个也是坑啊,连了半天没有连接上,最后在同事的帮助下解决,他需要启用
点击进去设置已启用,然后设置是就好了,
接着代码实现:
//-----------word文档
private String docName = "demo.doc";
private String aafileurl;
private String bbfileurl;
private File demoFile;
private File newFile;
// 消息显示到控件,设置数据
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1001:
String strXM = msg.getData().getString("XM");
String strSFZHM = msg.getData().getString("SFZHM");
String strKH = msg.getData().getString("KH");
String strJZDName = msg.getData().getString("JZDName");
String strDWMC = msg.getData().getString("DWMC");
Map<String, String> map = new HashMap<String, String>();
map.put("XM", strXM);//姓名
map.put("SFZHM", strSFZHM);//身份证号
map.put("KH", strKH);//体检编号
map.put("JZDName", strJZDName);//区县
map.put("DWMC", strDWMC);//报名单位
writeDoc(demoFile, newFile, map);
break;
default:
break;
}
};
};
/**
* 为了保证模板的可用,最好在现有的模板上复制后修改
*/
private void printer(String cardNo) {
try {
saveFile(docName, PersonInfoActivity.this, R.raw.demo);//文件目录res/raw
} catch (IOException e) {
e.printStackTrace();
}
//现场检查记录
aafileurl = Environment.getExternalStorageDirectory() + "/inspection/demo.doc";
bbfileurl = Environment.getExternalStorageDirectory() + "/inspection/demo_printer.doc";
//获取模板文件
demoFile = new File(aafileurl);
//创建生成的文件
newFile = new File(bbfileurl);
if (newFile.exists()) {
newFile.delete();
}
new Thread(new Runnable() {
@Override
public void run() {
/**
* 查询数据库
*/
try {
Connection conn = DBUtil.getSQLConnection();
String sql = "select * from dbo.TT_GR_DJXX where SFZHM =" + cardNo;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String XM = rs.getString("XM");
String SFZHM = rs.getString("SFZHM");
String KH = rs.getString("KH");
String JZDName = rs.getString("JZDName");
String DWMC = rs.getString("DWMC");
Message msg = new Message();
msg.what = 1001;
Bundle data = new Bundle();
data.putString("XM", XM);//姓名
data.putString("SFZHM", SFZHM);//身份证号
data.putString("KH", KH);//体检编号
data.putString("JZDName", JZDName);//区县
data.putString("DWMC", DWMC);//报名单位
msg.setData(data);
mHandler.sendMessage(msg);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 将文件复制到SD卡,并返回该文件对应的对象
*
* @return
* @throws IOException
*/
public void saveFile(String fileName, Context context, int rawid) throws IOException {
// 首先判断该目录下的文件夹是否存在
File dir = new File(Environment.getExternalStorageDirectory() + "/inspection/");
if (!dir.exists()) {
// 文件夹不存在 , 则创建文件夹
dir.mkdirs();
}
// 判断目标文件是否存在
File file1 = new File(dir, fileName);
if (!file1.exists()) {
file1.createNewFile(); // 创建文件
}
InputStream input = context.getResources().openRawResource(rawid); // 获取资源文件raw
try {
FileOutputStream out = new FileOutputStream(file1); // 文件输出流、用于将文件写到SD卡中
byte[] buffer = new byte[1024];
int len = 0;
while ((len = (input.read(buffer))) != -1) { // 读取文件,-- 进到内存
out.write(buffer, 0, len);
}
input.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* demoFile 模板文件
* newFile 生成文件
* map 要填充的数据
*/
public boolean writeDoc(File demoFile, File newFile, Map<String, String> map) {
try {
FileInputStream in = new FileInputStream(demoFile);
HWPFDocument hdt = new HWPFDocument(in);
// // 读取word文本内容
Range range = hdt.getRange();
// 替换文本内容
for (Map.Entry<String, String> entry : map.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
FileOutputStream out = new FileOutputStream(newFile, true);
hdt.write(ostream);
// 输出字节流
out.write(ostream.toByteArray());
out.close();
ostream.close();
// printershare打印doc
File doc = new File(bbfileurl);
ComponentName comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintDocuments");
Intent intent = new Intent();
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
intent.setType("application/doc");
intent.setData(Uri.fromFile(doc));
startActivity(intent);
return true;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
package com.cmos.smrzdevice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static String IP = "200.0.0.225";
private static String DBName = "ZB_TJ2018";
private static String USER = "sa";
private static String PWD = "password";
/**
* 创建数据库对象
*/
public static Connection getSQLConnection() {
Connection con = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
//加上 useunicode=true;characterEncoding=UTF-8 防止中文乱码
// con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + IP + ":3306/" + DBName + ";useunicode=true;characterEncoding=UTF-8", USER, PWD);
con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + IP + ":1433/" + DBName, USER, PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
}
这是连接的方法,最后打印就实现了,非常完美.有问题可以联系我哦.