프레임 워 크 템 플 릿 다운로드 파일

151589 단어 JAVA
maven:
 
            org.freemarker
            freemarker
            2.3.28
        


import freemarker.template.*;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Map;

public class DocUtil {
	public Configuration configure = null;
	
	public DocUtil() {
		configure = new Configuration(Configuration.VERSION_2_3_28);
		configure.setDefaultEncoding("utf-8");
	}
	
	/**
	 *   Doc    word  
	 *
	 * @param dataMap               
	 * @param downloadType     
	 * @param savePath         
	 */
	public void createDoc(Map dataMap, String downloadType, String savePath) {
		try {
			//         
			Template template = null;
			//           ,FreeMarker          。   servlet,classpath,     。
			//      ,  testDoc 
			configure.setClassForTemplateLoading(this.getClass(), "/templates");
			//       
//            configure.setObjectWrapper(new DefaultObjectWrapper());
			//       
			configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
			//  Template  ,         downloadType   
			template = configure.getTemplate(downloadType + ".ftl");
			File outFile = new File(savePath);
			Writer out;
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
			template.process(dataMap, out);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	
	public String getImageStr(String imgFile) {
		InputStream in;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}
}

테스트 코드
package com.swad.smas.information.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestDoc {
    public static void main(String[] args) {
        DocUtil docUtil=new DocUtil();
        Map dataMap=new HashMap();
        dataMap.put("name", "Joanna");
        dataMap.put("examNum", "111111111111");
        dataMap.put("IDCard", "222222222222222222");
        dataMap.put("carModel", "C1");
        dataMap.put("driverSchool", "    ");
        dataMap.put("busyType", "    ");
        dataMap.put("examDate", "2016-03-10");
        dataMap.put("orderCount", " 1 ");
        List pic = new ArrayList<>();
        pic.add(docUtil.getImageStr("D:\\test\\1.jpeg"));
        pic.add(docUtil.getImageStr("D:\\test\\3.png"));
        pic.add(docUtil.getImageStr("D:\\test\\3.png"));
        pic.add(docUtil.getImageStr("D:\\test\\3.png"));
        dataMap.put("images", pic);
        dataMap.put("firstExamTime", "12:41:17-12:44:38");
        dataMap.put("firstExamScores", "0 ,   ");
        dataMap.put("firstDeductItem", "12:44:15 20102 1     ,      100 ");
        //dataMap.put("firstPic1", docUtil.getImageStr("D:\\test\\2.jpeg"));
        //dataMap.put("firstPic2", docUtil.getImageStr("D:\\test\\1.jpeg"));
        //dataMap.put("secondExamTime", "12:46:50-13:05:37");
        //dataMap.put("secondExamScores", "90 ,  ");
        //dataMap.put("secondDeductItem", "");
        //dataMap.put("secondPic1", docUtil.getImageStr("D:\\test\\1.jpeg"));
        //dataMap.put("secondPic2", docUtil.getImageStr("D:\\test\\1.jpeg"));
        docUtil.createDoc(dataMap, "testDoc", "D:\\test\\22211.doc");
    }
}

POI
package com.swad.smas.information.utils;
 
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
 
 
/**
 * Created by zhouhs on 2017/1/9.
 */
public class WordExportController {
 
    public static void main(String[] args)throws Exception {
        //Blank Document
        XWPFDocument document= new XWPFDocument();
 
        //Write the Document in file system
        FileOutputStream out = new FileOutputStream(new File("D://test/create_table.docx"));
 
 
        //    
        XWPFParagraph titleParagraph = document.createParagraph();
        //      
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
 
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        titleParagraphRun.setText("Java PoI");
        titleParagraphRun.setColor("000000");
        titleParagraphRun.setFontSize(20);
 
 
        //  
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("Java POI   word  。");
        run.setColor("696969");
        run.setFontSize(16);
        
        FileInputStream im =  new FileInputStream(new File("D:\\test/1.jpeg"));
        document.addPictureData(im,Document.PICTURE_TYPE_JPEG);
 
        //        
        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
        cTShd.setVal(STShd.CLEAR);
        cTShd.setFill("97FFFF");
 
 
        //  
        XWPFParagraph paragraph1 = document.createParagraph();
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setText("\r");
 
 
        //      
        XWPFTable infoTable = document.createTable();
        //     
        infoTable.getCTTbl().getTblPr().unsetTblBorders();
 
 
        //      
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));
 
 
        //     
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);
        infoTableRowOne.getCell(0).setText("  ");
        infoTableRowOne.addNewTableCell().setText(": Java      ");
 
        //     
        XWPFTableRow infoTableRowTwo = infoTable.createRow();
        infoTableRowTwo.getCell(0).setText("  ");
        infoTableRowTwo.getCell(1).setText(": seawater");
 
        //     
        XWPFTableRow infoTableRowThree = infoTable.createRow();
        infoTableRowThree.getCell(0).setText("  ");
        infoTableRowThree.getCell(1).setText(": xxx-xx-xx");
 
        //     
        XWPFTableRow infoTableRowFour = infoTable.createRow();
        infoTableRowFour.getCell(0).setText("  ");
        infoTableRowFour.getCell(1).setText(":  ");
 
        //     
        XWPFTableRow infoTableRowFive = infoTable.createRow();
        infoTableRowFive.getCell(0).setText("   ");
        infoTableRowFive.getCell(1).setText(": xx");
 
 
        //          
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun paragraphRun = paragraph.createRun();
        paragraphRun.setText("\r");
    
        XWPFParagraph pic = document.createParagraph();
        XWPFRun oicRun = pic.createRun();
        FileInputStream im1 =  new FileInputStream(new File("D:\\test/2.JPEG"));
        oicRun.addPicture(im1,Document.PICTURE_TYPE_JPEG,"2.JPEG",200,400);
        //      
        XWPFTable ComTable = document.createTable();
 
 
        //      
        CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
        comTableWidth.setType(STTblWidth.DXA);
        comTableWidth.setW(BigInteger.valueOf(9072));
 
        //     
        XWPFTableRow comTableRowOne = ComTable.getRow(0);
        comTableRowOne.getCell(0).setText("    ");
        comTableRowOne.addNewTableCell().setText("    ");
        comTableRowOne.addNewTableCell().setText("    ");
        comTableRowOne.addNewTableCell().setText("title");
 
        //     
        XWPFTableRow comTableRowTwo = ComTable.createRow();
        comTableRowTwo.getCell(0).setText("2016-09-06");
        comTableRowTwo.getCell(1).setText("  ");
        comTableRowTwo.getCell(2).setText("seawater");
        comTableRowTwo.getCell(3).setText("Java     ");
 
        //     
        XWPFTableRow comTableRowThree = ComTable.createRow();
        comTableRowThree.getCell(0).setText("2016-09-06");
        comTableRowThree.getCell(1).setText("  ");
        comTableRowThree.getCell(2).setText("seawater");
        comTableRowThree.getCell(3).setText("Java     ");
 
 
        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
 
        //    
        CTP ctpHeader = CTP.Factory.newInstance();
        CTR ctrHeader = ctpHeader.addNewR();
        CTText ctHeader = ctrHeader.addNewT();
        String headerText = "Java POI create MS word file.";
        ctHeader.setStringValue(headerText);
        XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
        //      
        headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0] = headerParagraph;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
 
 
        //    
        CTP ctpFooter = CTP.Factory.newInstance();
        CTR ctrFooter = ctpFooter.addNewR();
        CTText ctFooter = ctrFooter.addNewT();
        String footerText = "http://blog.csdn.net/zhouseawater";
        ctFooter.setStringValue(footerText);
        XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
        parsFooter[0] = footerParagraph;
        policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
 
 
        document.write(out);
        out.close();
        System.out.println("create_table document written success.");
    }
 
 
}

ftl:



    
        
            
                
                
                
                
            
        
    
    
        
            
                
                
                
                
                
                
                
            
        
    
    
        
            
                
                    
                        
                            
                            
                            
                            
                                
                                
                                
                                
                                
                                
                            
                            
                            
                                
                                
                                
                                
                            
                        
                        
                            
                            
                            
                            
                            
                        
                        
                            
                                
                                    
                                    
                                    
                                    
                                    
                                    
                                
                                
                                
                                    
                                    
                                    
                                    
                                
                            
                            
                                
                                    
                                    
                                
                                

${name}

${examNum}

${IDCard}

${carModel}

${driverSchool}

${busyType}

${examDate}

${orderCount}

${firstExamTime}

${firstExamScores}

${firstDeductItem}

1 0 0 0 0 0 false false 0 WPS Office_11.1.0.8573_F1E327BC-269C-435d-A152-05C5408002CA 0 2019-05-21T08:17:00Z 2019-05-21T09:15:38Z 1 2052-11.1.0.8573 ${image} #list>

좋은 웹페이지 즐겨찾기