4.java poi Excel --Erstellen Sie eine Excel-Datei
4.java poi Excel
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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();
}
}
}
}
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