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:
  • 相关阅读:
    Windows Server 2003 SP2(32位) 中文版 下载地址 光盘整合方法
    用Recycle()方法对Java对象的重要性
    Lotus中千奇百怪的 $$
    Developing a simple application using steps "User Decision" and "Mail"(1) 沧海
    沟通中的情绪管理(演讲稿) 沧海
    人只有在压力之下,才可能成功,没做一件事,都必须成功,不许言败 沧海
    什么是IDOC,以及IDOC的步骤 沧海
    VS2008 Professional Edition CHS中的deffactory.dat读取错误 沧海
    Including custom text in the step "User Decision" 沧海
    SAP Upgrade Strategy 沧海
  • 原文地址:https://www.cnblogs.com/coes/p/14156086.html
Copyright © 2011-2022 走看看