废话不多说,直接上代码,网上代码都不能用,迫不得已,最后查询了官网的源码。上菜!
模板文件可以自己去github去下载。
package com.aspose.words.examples.mail_merge;
import com.aspose.words.Document;
import com.aspose.words.FieldMergingArgs;
import com.aspose.words.IFieldMergingCallback;
import com.aspose.words.ImageFieldMergingArgs;
import com.aspose.words.net.System.Data.DataRow;
import com.aspose.words.net.System.Data.DataTable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
//ExStart:
public class InsertImagesFromADatabase {
private static final String dataDir = "D:\\\\works\\\\IdeaProjects\\\\Aspose.Words-for-Java\\\\Examples\\\\src\\\\main\\\\resources\\\\";
private static final String path = dataDir + "MailMerge\\\\";
public static void main(String\[\] args) throws Exception {
Document doc = new Document(path + "MailMerge.MergeImage.doc");
// Set up the event handler for image fields.
doc.getMailMerge().setFieldMergingCallback(new HandleMergeImageFieldFromBlob());
Map<String, Object> map = new HashMap<>(16);
map.put("FirstName","测试第一名称");
map.put("LastName","测试最后名称");
map.put("Title","测试标题");
map.put("Address","测试地址");
map.put("City","测试城市");
map.put("Country","测试国家");
map.put("PhotoBLOB", Base64.getEncoder ().encodeToString(toByteArray3("D:\\\\test.jpeg")));
DataTable table = new DataTable("Employees");
DataRow dataRow = table.newRow();
map.forEach((k,v) -> {
table.getColumns().add(k);
dataRow.set(k,v);
});
table.getRows().add(dataRow);
//DataTable table = new DataTable(resultSet, "Employees");
// Perform mail merge.
doc.getMailMerge().executeWithRegions(table);
// Close the database.
// conn.close();
doc.save(path + "MailMerge.MergeImage Out.doc");
}
/\*\*
\* 把ResultSet转化成map对象
\*
\* @param rs
\* @return
\* @throws SQLException
\*/
public static Map<String, Object> Result2Map(ResultSet rs)
throws SQLException {
Map<String, Object> hm = new HashMap<>(16);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
if (rs.next()) {
for (int i = 1; i <= count; i++) {
String key = rsmd.getColumnName(i);
Object value = rs.getString(i);
hm.put(key, value);
}
return hm;
}
return null;
}
public static byte\[\] toByteArray3(String filename) throws IOException {
FileChannel fc = null;
try {
fc = new RandomAccessFile(filename, "r").getChannel();
MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ\_ONLY, 0,
fc.size()).load();
System.out.println(byteBuffer.isLoaded());
byte\[\] result = new byte\[(int) fc.size()\];
if (byteBuffer.remaining() > 0) {
// System.out.println("remain");
byteBuffer.get(result, 0, byteBuffer.remaining());
}
return result;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class HandleMergeImageFieldFromBlob implements IFieldMergingCallback {
@Override
public void fieldMerging(FieldMergingArgs args) throws Exception {
// Do nothing.
}
/\*\*
\* This is called when mail merge engine encounters Image:XXX merge
\* field in the document. You have a chance to return an Image object,
\* file name or a stream that contains the image.
\*/
@Override
public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {
// The field value is a byte array, just cast it and create a stream on it.
System.out.println(e);
if (e.getFieldValue() != null) {
ByteArrayInputStream imageStream = new ByteArrayInputStream(Base64.getDecoder ().decode (e.getFieldValue ().toString ()));
// Now the mail merge engine will retrieve the image from the stream.
e.setImageStream(imageStream);
}
}
}
//ExEnd:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章