zoukankan      html  css  js  c++  java
  • 《aspose》 word表格循环导出图片

    废话不多说,直接上代码,网上代码都不能用,迫不得已,最后查询了官网的源码。上菜!

    模板文件可以自己去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:
  • 相关阅读:
    uinty实现玩家尾随鼠标位置平滑旋转角度
    matlab hornerDemo
    【App 开发框架
    mobiscroll手机端插件 好用(时间、日历、颜色)
    朴素的UNIX之-调度器细节
    String与StringBuffer的差别
    用jquery ajax做的select菜单,选中的效果
    Codeforces Round #253 (Div. 2)A. Anton and Letters
    unix环境高级编程——文件i/o
    CSDN的技术问题
  • 原文地址:https://www.cnblogs.com/coes/p/14156086.html
Copyright © 2011-2022 走看看