Java-Beispielcode 04

Ich habe die in SAMPLE GALLERY veröffentlichte Quelle mitgeteilt und eine Importanweisung hinzugefügt.

4.java poi Excel --Erstellen Sie eine Excel-Datei

4.java poi Excel

4-1. Excel-Datei erstellen

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);//Ich erstelle hier eine Excel-Datei.
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-2. Einstellung des Blattnamens

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("Blatt1");//Hier wird der Blattname angegeben.
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-3 Geben Sie die Farbe des Blattes an

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("Blatt1");
            sheet.setTabColor(new XSSFColor(BLUE));
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-4 Geben Sie einen Wert in die Zelle ein

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("Blatt1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");//Der Wert wird hier in der Zelle eingestellt.
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-5 Geben Sie die Schriftgröße an

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("Blatt1");
            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);//Die Schriftgröße wird hier angegeben.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-6 Geben Sie die Schriftart an

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("Blatt1");
            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");//Die Schriftart wird hier angegeben.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-7. Machen Sie die Zeichen fett

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("Blatt1");
            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);//Hier ist es fett gedruckt.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-8 Zeichen kursiv machen

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("Blatt1");
            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);//Es wird hier als kursiv angegeben.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-9 Zeichen unterstreichen

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("Blatt1");
            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);//Es ist hier unterstrichen.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-10 Zeichnen Sie den Text durch

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("Blatt1");
            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);//Hier wird ein Durchstreichen gezeichnet.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-11 Geben Sie die Hintergrundfarbe der Zelle an

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("Blatt1");
            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);//Hier wird die Hintergrundfarbe angegeben.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-12. Ausrichtung der Werte (horizontale Richtung)

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("Blatt1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.RIGHT);//Die Werte werden hier ausgerichtet (horizontal)
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-13 Ausrichtung der Werte (vertikale Richtung)

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("Blatt1");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setVerticalAlignment(VerticalAlignment.CENTER);//Hier richten wir die Werte aus (vertikal)
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-14 Geben Sie die Farbe des Textes in der Zelle an

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("Blatt1");
            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);//Hier wird die Textfarbe eingestellt.
            style.setFont(font);
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-15 Geben Sie den Zellrand an

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("Blatt1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Cell Value");
            XSSFCellStyle style = workbook.createCellStyle();
            BorderStyle border = BorderStyle.THIN;//Hier wird der Randtyp angegeben.
            style.setBorderBottom(border);//Der untere Rand wird angegeben.
            style.setBorderLeft(border);//Der linke Rand wird angegeben.
            style.setBorderRight(border);//Der rechte Rand ist angegeben.
            style.setBorderTop(border);//Der obere Rand ist angegeben.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-16 Geben Sie die Farbe des Zellrandes an

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("Blatt1");
            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);//Hier wird die Rahmenfarbe angegeben.
            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 Geben Sie den Zellenumbruch an

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("Blatt1");
            XSSFRow row = sheet.createRow(1);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue("Hello World.");
            XSSFCellStyle style = workbook.createCellStyle();
            style.setWrapText(true);//Die Verpackung ist hier angegeben.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-18. Setzen Sie den Hyperlink in die Zelle

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("Blatt1");
            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);//Der Hyperlink wird hier angegeben.
            cell.setCellStyle(style);
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

4-19. Zellen zusammenführen

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("Blatt1");
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));//Hier werden die zusammenzuführenden Zellen festgelegt. (Erste Reihe, lastRow, firstCol, lastCol)
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}

Recommended Posts

Java-Beispielcode 02
Java-Beispielcode 03
Java-Beispielcode 04
Java-Beispielcode 01
Beispielcode für elektronische Signatur (JAVA)
Java Parallel Code Sample Collection
Führen Sie Java-Code skriptweise aus
Java-Code-TIPPS
Selenprobe (Java)
Java GUI Beispiel
Java 9 neue Funktionen und Beispielcode
Java-Zeichencode
Beispielcode mit Minio aus Java
Apache Beam Beispielcode
[Java] Beispiel für ein Urlaubsurteil
[Java] logback slf4j Beispiel
Beispielcode zum Konvertieren von List in List <String> in Java Stream
Sammlung von Java-Testcode-Methoden
[Windows] Java-Code ist verstümmelt
Java
Java mit Visual Studio Code
Beispiel für eine Java-Standardprotokollausgabe
Schreiben Sie Java8-ähnlichen Code in Java8
Beispielcode für die Protokollausgabe von Java + SLF4J + Logback
Java
Selenium Musterbuchungsformular (Java)
Beispielcode zum Parsen von Datum und Uhrzeit mit Java SimpleDateFormat
Errate den Zeichencode in Java
Code Java von Emacs mit Eclim
Java Spring-Umgebung in vs Code
[Java] Eliminierung des Kesselplattencodes mit Lombok
Erstellen Sie Java mit Mac vs Code
Beliebiger Code zum Erstellen von Zeichenfolgen durch Java
Führen Sie gepackten Java-Code mit Befehlen aus
Ein einfaches Beispiel für Rückrufe in Java
Java-Quellcode zum Lesen der Klasse java.lang.Math
[Java] Eliminierung des Kesselplattencodes mit Lombok 2
BloomFilter-Beschreibungs- und Implementierungsbeispiel (JAVA)
[Java] Beispiel für eine Überprüfung des Datumszeitraums
Beispiel für eine EXCEL-Dateiaktualisierung mit JAVA
Java-Entwicklungsumgebung (Mac, VS Code)
[Android] Konvertieren Sie Android Java-Code in Kotlin
Beispielautomat mit Java
Grundstruktur des Java-Quellcodes
So verwalten Sie Java-Code, der automatisch von jOOQ & Flyway generiert wird
Beispielcode zum Aufrufen der Yahoo! Local Search API in Java
Beispielcode mit JMustache, der Moustache-Vorlagen-Engine in Java
Java lernen (0)
Java studieren ―― 3
[Java] -Array
Java geschützt
[Java] Anmerkung
Bereiten Sie die Java-Entwicklungsumgebung mit VS Code vor
[Java] Modul
Java-Array
Java studieren ―― 9
Java Scratch Scratch
Java-Tipps, Tipps
Beispielcode für die Singleton-Implementierung mit enum