Java sample code 04

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

4.java poi Excel --Create an Excel file --Sheet name setting --Specify the color of the sheet --Enter a value in the cell --Specify the font size --Specify font --Make letters bold --Make characters italic --Underline the letters --Draw a strikethrough on the text --Specify the background color of the cell --Alignment of values (horizontal) --Alignment of values (vertical) --Specify the color of the text in the cell --Specify cell border --Specify the color of the cell border --Specify cell wrapping --Set a hyperlink in the cell --Combine cells

4.java poi Excel

4-1. Create an Excel file

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook.createSheet();
            workbook.write(os);//I am creating an Excel file here.
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-2. Sheet name setting

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook.createSheet("Sheet 1");//The sheet name is specified here.
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-3. Specify the color of the sheet

import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import static java.awt.Color.BLUE;

public class SamplePoi {

    public static void main(String[] args) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            sheet.setTabColor(new XSSFColor(BLUE));
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-4. Enter a value in the cell

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");//The value is set in the cell here.
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-5. Specify the font size

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 20);//The font size is specified here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-6. Specify the font

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setFontName("Times New Roman");//The font is specified here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-7. Make the letters bold

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setBold(true);//It is set to bold here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-8. Make characters italic

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setItalic(true);//Italic is specified here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-9. Underline letters

import org.apache.poi.ss.usermodel.FontUnderline;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setUnderline(FontUnderline.SINGLE);//It is underlined here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-10. Draw a strikethrough on the text

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setStrikeout(true);//A strikethrough is drawn here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-11. Specify the background color of the cell

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            //style.setFillPattern(CellStyle.BRICKS);
            XSSFColor color = new XSSFColor(Color.RED);
            style.setFillForegroundColor(color);//The background color is specified here.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-12. Alignment of values (horizontal)

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.RIGHT);//The values are aligned here (horizontal)
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-13. Alignment of values (vertical direction)

import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setVerticalAlignment(VerticalAlignment.CENTER);//The values are aligned here (vertically)
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-14. Specify the text color of the cell

import org.apache.poi.xssf.usermodel.*;

import java.awt.*;
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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            XSSFColor color = new XSSFColor(Color.RED);
            font.setColor(color);//The text color is set here.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-15. Specify cell border

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            BorderStyle border = BorderStyle.THIN;//The border type is specified here.
            style.setBorderBottom(border);//The bottom border is specified.
            style.setBorderLeft(border);//The left border is specified.
            style.setBorderRight(border);//The right border is specified.
            style.setBorderTop(border);//The upper border is specified.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-16. Specify the color of the cell border

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;

import java.awt.*;
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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            BorderStyle border = BorderStyle.THIN;
            XSSFColor color = new XSSFColor(Color.RED);
            style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, color);//The border color is specified here.
            style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, color);
            style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, color);
            style.setBorderColor(XSSFCellBorder.BorderSide.TOP, color);
            style.setBorderBottom(border);
            style.setBorderLeft(border);
            style.setBorderRight(border);
            style.setBorderTop(border);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-17. Specify cell wrapping

import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Hello World.");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setWrapText(true);//Wrapping is specified here.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-18. Set hyperlinks in cells

import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Sample Code Library");
            XSSFCellStyle style = workbook.createCellStyle();
            XSSFHyperlink hyperlink = workbook.getCreationHelper().createHyperlink(Hyperlink.LINK_URL);
            hyperlink.setAddress("http://blueplace.sakura.ne.jp/");
            cell.setHyperlink(hyperlink);//The hyperlink is specified here.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-19. Merge cells

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

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("TestExcelFile.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Sheet 1");
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));//The cells to be merged are set here. (FirstRow, lastRow, firstCol, lastCol)
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

Recommended Posts

Java sample code 02
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
Selenium sample (Java)
Java GUI sample
Java 9 new features and sample code
Java character code
Sample code using Minio from Java
Apache beam sample code
[Java] Holiday judgment sample
[Java] logback slf4j sample
[Java] Explanation of Strategy pattern (with sample code)
Sample code to convert List to List <String> in Java Stream
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
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Sample code for Singleton implementation using enum