Java Apache POI换行符随机插入到生成的页面?

时间:2019-07-09 00:36:51

标签: java file-io ms-word apache-poi

我正在从事一个有趣的项目,该项目可以处理创建我的自定义信头纸的过程。我正在使用Apache POI处理Word文档。我计划在完成使用AWT添加GUI并允许通过其自定义的基本框架后,对其进行扩展,这说明了如何在代码中进行设置。尝试格式化标题时,我得到了一些非常奇怪的结果,看来Apache POI会在想要的位置插入换行符?我想我不太了解。

CreateDocument.java

awk -F\' '/atop/ {print $2}' b.txt 

很简单吧?我希望得到以下结果:

letterhead07 / 08 / 2019.docx

package letterHeader;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;

public class CreateDocument 
{
    // create blank document
    static XWPFDocument document = new XWPFDocument();

    // create paragraphs for header
    static XWPFParagraph name = document.createParagraph();
    static XWPFParagraph address = document.createParagraph();
    static ArrayList<XWPFParagraph> phoneNumbers = new ArrayList<XWPFParagraph>();
    static ArrayList<XWPFParagraph> emails = new ArrayList<XWPFParagraph>();
    static XWPFParagraph date = document.createParagraph();

    // create runner objects
    static XWPFRun nameRunner = name.createRun();
    static XWPFRun addressRunner = address.createRun();
    // remember to make a runner for each email
    static ArrayList<XWPFRun> phoneRunners = new ArrayList<XWPFRun>();
    static ArrayList<XWPFRun> emailRunners = new ArrayList<XWPFRun>();

    public static void main(String[] args) throws Exception 
    {   
        // make datetime for timestamp
        DateFormat dateFormat = new SimpleDateFormat("MM:dd:yyyy");
        Date date = new Date();

        // create IO stream with document name
        FileOutputStream out = new FileOutputStream( new File("letterhead" + dateFormat.format(date) + ".docx"));

        CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
        CTPageMar pageMar = sectPr.addNewPgMar();
        pageMar.setLeft(BigInteger.valueOf(720L));
        pageMar.setTop(BigInteger.valueOf(720L));
        pageMar.setRight(BigInteger.valueOf(720L));
        pageMar.setBottom(BigInteger.valueOf(720L));

        // storing the ID automatically makes the objects
        int phoneID = addListParagraph("phone");
        int emailID1 = addListParagraph("email");
        int emailID2 = addListParagraph("email");

        // make name
        nameRunner.setText("Michael Simanski");
        nameRunner.setBold(true);
        nameRunner.setFontSize(18);
        nameRunner.setFontFamily("Times");

        // make address
        addressRunner.setText("address");
        addressRunner.setFontSize(12);
        addressRunner.setFontFamily("Times");

        // make phone
        phoneRunners.get(phoneID).setText("phone");
        phoneRunners.get(phoneID).setFontSize(12);
        phoneRunners.get(phoneID).setFontFamily("Times");

        // make emails
        emailRunners.get(emailID1).setText("mfsimanski@gmail.com");
        emailRunners.get(emailID1).setFontSize(12);
        emailRunners.get(emailID1).setFontFamily("Times");  
        emailRunners.get(emailID2).setText("secondemail");
        emailRunners.get(emailID2).setFontSize(12);
        emailRunners.get(emailID2).setFontFamily("Times");
        emailRunners.get(emailID2).addCarriageReturn();

        document.write(out);
        out.close();


    }

    public static int addListParagraph(String type) 
    {
        switch (type) 
        {
            case "phone":
                phoneNumbers.add(document.createParagraph());
                phoneRunners.add(phoneNumbers.get(phoneNumbers.size() - 1).createRun());
                return phoneNumbers.size() - 1;

            case "email":
                emails.add(document.createParagraph());
                emailRunners.add(emails.get(emails.size() - 1).createRun());
                return emails.size() - 1;

            default:
                System.out.println("ERROR: Paragraph type not found!");
                return 0;
        }
    }

}

但困惑的是:

letterhead07 / 08 / 2019.docx

Michael Simanski
address
phone
mfsimanski@gmail.com
secondemail 

我想念一些东西,愚蠢的东西还是两者兼而有之?

0 个答案:

没有答案