Java sample code 03

I communicated the source posted in SAMPLE GALLERY and added an import statement.

3.java poi Word

need to download poi jar file

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

3-1. Creating a Word file

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();
            }
        }
    }

}

3-2. Write characters in Word file

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();
            }
        }
    }

}

3-3. Make the letters bold

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();
            }
        }
    }

}

3-4. Make characters italic

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();
            }
        }
    }

}

3-5. Underline letters

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();
            }
        }
    }

}

3-6. Set the text color

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();
            }
        }
    }

}

3-7. Add tabs

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();
            }
        }
    }

}

3-8. Add a strikethrough

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();
            }
        }
    }

}

3-9. Add a double strikethrough

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();
            }
        }
    }

}

3-10. Specify the font size

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();
            }
        }
    }

}

3-11. Specify the font

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();
            }
        }
    }

}

3-12. Insert a line break

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

Java sample code 03
Java sample code 04
Java sample code 01
Digital signature sample code (JAVA)
Java parallelization code sample collection
Script Java code
Java code TIPS
[Java] Generics sample
Selenium sample (Java)
Java GUI sample
Java 9 new features and sample code
Java character code
Sample code using Minio from Java
Sample code collection for Azure Java development
Apache beam sample code
[Java] Holiday judgment sample
[Java] logback slf4j sample
[Java] Explanation of Strategy pattern (with sample code)
Java test code method collection
[Windows] Java code is garbled
Java
Java in Visual Studio Code
Java standard log output sample
Write Java8-like code in Java8
Sample code for log output by Java + SLF4J + Logback
Java
Selenium Sample Reservation Form (Java)
Sample code to parse date and time with Java SimpleDateFormat
Guess the character code in Java
Code Java from Emacs with Eclim
Java Spring environment in vs Code
Java 15 implementation and VS Code preferences
[Java] Boilerplate code elimination using Lombok
Java build with mac vs code
Arbitrary string creation code by Java
Execute packaged Java code with commands
A simple sample callback in Java
Java source code reading java.lang.Math class
[Java] Boilerplate code elimination using Lombok 2
BloomFilter description and implementation sample (JAVA)
[Java] Date period duplication check sample
EXCEL file update sample with JAVA
Java development environment (Mac, VS Code)
[Android] Convert Android Java code to Kotlin
Sample vending machine made in Java
Basic structure of Java source code
How to manage Java code automatically generated by jOOQ & Flyway sample
Sample code to call the Yahoo! Local Search API in Java
Sample code that uses the Mustache template engine JMustache in Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Prepare Java development environment with VS Code
[Java] Module
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Sample code for Singleton implementation using enum
Java methods