使用模板导出excel、word

Demo

    public void demo(HttpServletRequest request) {
        try {
            //数据  实体
            List<XXX> list = new ArrayList<>(16);

            //打开模板
            ClassPathResource template = new ClassPathResource("static/templateFile/xxx.xls");
            Workbook wb = new Workbook(template.getInputStream());


            //打开模板方式2
            //String template_path_new = new ClassPathResource("/").getURL().getPath() + "static/templateFile/exportTemp/teachingYearTemp.xls";
            //Workbook wb = new Workbook(template_path_new);
            WorkbookDesigner designer = new WorkbookDesigner(wb);

            //参数使用方法   &=[Params].xxx   序号使用方法&=&={r}-标题行数
            designer.setDataSource("Params", list);

            designer.process();
            //designer.process(true);

            //服务器端:保存生成后的表格
            String serverPath = new ClassPathResource("aspose/temp").getURL().getPath() + "/" + DateUtil.currentSeconds() + Guid.getGuid() + ".xlsx";
            designer.getWorkbook().save(serverPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

工具类

package com.wkrj.core.utils;

import cn.hutool.core.date.DateUtil;
import com.aspose.cells.Workbook;
import com.aspose.cells.WorkbookDesigner;
import com.aspose.words.License;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AsposeExportUtil {

    /**
     * 使用模板导出Word
     *
     * @param tempPath 模板地址
     * @param filename 导出文件名
     * @throws Exception
     */
    public void ExportToWord(String tempPath, String filename, HttpServletResponse response, HttpServletRequest request) throws Exception {
        RegistAspose();//注册
        String path = AsposeExportUtil.class.getClass().getClassLoader().getResource("../../").getPath();
        System.out.println(path);
        System.out.println(path + tempPath);
		/*Document doc = new Document(path+ tempPath);
		DocumentBuilder builder = new DocumentBuilder(doc);

		//直接插入书签
		builder.moveToBookmark("name");
		builder.write("张三");
		
		//多条插入书签
		builder.moveToBookmark("sex");
		builder.writeln("2014.6 培训1");
		builder.write("2014.7 培训2");
		
		//插入图片
		String imagePath = path + "upload/personFirst/20151106979110.jpg";
		//读取一个二进制图片  
        FileInputStream fis = new FileInputStream(imagePath);  
        byte[] image = new byte[fis.available()];  
        fis.read(image);  
        fis.close();
		builder.moveToBookmark("photoimg");
        builder.insertImage(image,115D,145D);
		
        
        
		//插入family循环
	    builder.moveToBookmark("family");
	    List<WkrjPublicFamily> list = new ArrayList<WkrjPublicFamily>();
	    WkrjPublicFamily family = new WkrjPublicFamily();
	    family.setRelation("fdf1");
	    family.setName("dfdfd1");
	    list.add(family);
	    family.setRelation("fdf2");
	    family.setName("dfdfd2");
	    list.add(family);
	    family.setRelation("fdf3");
	    family.setName("dfdfd3");
	    list.add(family);
	    family.setRelation("fdf4");
	    family.setName("dfdfd4");
	    list.add(family);
	    family.setRelation("fdf5");
	    family.setName("dfdfd5");
	    list.add(family);
	    */

        //插入表头
        //builder.endRow();//行结束
	    
	    /*builder.insertCell();
	    builder.getRowFormat().setHeight(30.0D);
	    builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);//合并竖着的单元格
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.writeln("家外");
	    builder.writeln("庭亲");
	    builder.writeln("主属");
	    builder.writeln("要中");
	    builder.writeln("成的");
	    builder.writeln("员知");
	    builder.writeln("或名");
	    builder.writeln("国人");
	    builder.write("内士");*/
	    
	    /*builder.startTable();
	    builder.getRowFormat().setHeight(40);
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.getCellFormat().setWidth(75);
	    builder.getRowFormat().getBorders().setLineWidth(0);
	    builder.write("关系");
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.getCellFormat().setWidth(75);
	    builder.write("姓  名");
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.getCellFormat().setWidth(50);
	    builder.write("出生年月");
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.getCellFormat().setWidth(50);
	    builder.write("性别");
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.getCellFormat().setWidth(80);
	    builder.write("政治面貌");
	    
	    builder.insertCell();
	    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
	    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
	    builder.write("工作单位及职务");
	    
	    builder.endRow();//行结束
	    
	    for (int i = 0; i < list.size(); i++) {
	    	builder.getRowFormat().setHeight(40);
	    	builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.getCellFormat().setWidth(75);
		    builder.write(list.get(i).getRelation());
		    
		    builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.getCellFormat().setWidth(75);
		    builder.write(list.get(i).getName());
		    
		    builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.getCellFormat().setWidth(50);
		    builder.write("出生年月"+i);
		    
		    builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.getCellFormat().setWidth(50);
		    builder.write("性别"+i);
		    
		    builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.getCellFormat().setWidth(80);
		    builder.write("政治面貌"+i);
		    
		    builder.insertCell();
		    builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
		    builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
		    builder.write("工作单位及职务"+i);

		    builder.endRow();
		}
	    
	    System.out.println(filename);
	    String serverPath = path+"plug-in/temp/temp1.doc";
	    doc.save(serverPath);
	    System.out.println(serverPath);
	    downloadFile(request,response,filename,serverPath);*/

    }

    public static void main(String[] args) {
        try {
            ClassPathResource license_path = new ClassPathResource("aspose");
            System.out.println(license_path.getPath());
            System.out.println(license_path.getDescription());
            System.out.println(AsposeExportUtil.class.getClassLoader());
            System.out.println(license_path.getFilename());
            System.out.println(license_path.getFile().getPath());
            System.out.println(license_path.getURL().getPath());
            System.out.println(license_path.getURI().getPath());
            System.out.println(AsposeExportUtil.class.getClassLoader().getResource("aspose/license.xml").getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册控件证书,注册Aspose.Words
     */
    public void RegistAspose() {
        try {
            ClassPathResource license = new ClassPathResource("aspose/license.xml");
            //String license_path = AsposeExportUtil.class.getClass().getClassLoader().getResource("/").getPath() + "license.xml";
            License aposeLic = new License();
            aposeLic.setLicense(license.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 注册控件证书,注册Aspose.Cells
     */
    public void RegistAsposeCells() {
        try {
            ClassPathResource license = new ClassPathResource("aspose/license.xml");
            //String license_path = getClass().getClassLoader().getResource("/").getPath() + "licenseCells.xml";
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(license.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 下载文件
     *
     * @param request
     * @param response
     * @param fileName
     * @param serverPath
     */
    public void downloadFile(HttpServletRequest request, HttpServletResponse response, String fileName, String serverPath) {
        response.setContentType("text/html;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        long fileLength = new File(serverPath).length();

        try {
            response.setHeader("Content-disposition", "attachment; filename="
                    + new String(fileName.getBytes("gb2312"), "ISO8859-1"));

            response.setHeader("Content-Length", String.valueOf(fileLength));
            bis = new BufferedInputStream(new FileInputStream(serverPath));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bis.close();
            bos.close();
        } catch (Exception e) {
            response.setHeader("Location", response.encodeRedirectURL("index"));
            //e.printStackTrace();
        }
    }

}

license.xml 信息如下

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
      <Product>Aspose.Words for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>20991231</SubscriptionExpiry>
    <LicenseExpiry>20991231</LicenseExpiry>
    <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

参数使用方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值