3.java poi Word
3.java poi Word
3-0.download&install
Copy the "* .jar" file from the bin file downloaded from the following site to Project
https://poi.apache.org/
https://xmlbeans.apache.org/download/
--Subcomponent
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            wordFile.write(os);//I am creating a word file here.
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile2.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");//I'm writing letters here.
            run.setText("Kakikukeko");
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setBold(true);//It is in bold here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setItalic(true);//Italicized here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setUnderline(UnderlinePatterns.DOUBLE);//It is underlined here. The underline type can be selected in the "Underline Patterns" class.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setColor("FF0000");//The color of the characters is specified here. Hexadecimal. "RRGGBB".
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.addTab();//I'm attaching a tab here.
            run.setText("AIUEO");
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setStrikeThrough(true);//A strikethrough is added here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setDoubleStrikethrough(true);//A double strikethrough is added here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("AIUEO");
            run.setFontSize(20);//I am changing the font size here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("Hello World.");
            run.setFontFamily("Times New Roman");//The font is specified here.
            run.addCarriageReturn();
            run = paragraph.createRun();
            run.setText("Hello World.");
            run.setFontFamily("MS gothic");//The font is specified here.
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SamplePoi {
    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestWordFile.docx");
            XWPFDocument wordFile = new XWPFDocument();
            XWPFParagraph paragraph = wordFile.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("Hello World.");
            run.addCarriageReturn();//A line break is inserted here.
            run.setText("Hello World.");
            wordFile.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }
}
        Recommended Posts