4.java poi Excel --Créer un fichier Excel
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);//Je crée un fichier Excel ici.
} 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("Feuille 1");//Le nom de la feuille est spécifié ici.
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("Feuille 1");
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("Feuille 1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Cell Value");//La valeur est définie dans la cellule ici.
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("Feuille 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);//La taille de la police est spécifiée ici.
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("Feuille 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");//La police est spécifiée ici.
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("Feuille 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);//Il est mis en gras ici.
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("Feuille 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);//Il est spécifié ici en italique.
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("Feuille 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);//Il est souligné ici.
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("Feuille 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);//Un barré est dessiné ici.
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("Feuille 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);//La couleur de fond est spécifiée ici.
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("Feuille 1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Cell Value");
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);//Les valeurs sont alignées ici (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("Feuille 1");
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Cell Value");
XSSFCellStyle style = workbook.createCellStyle();
style.setVerticalAlignment(VerticalAlignment.CENTER);//Ici, nous alignons les valeurs (verticalement)
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("Feuille 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);//La couleur du texte est définie ici.
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("Feuille 1");
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue("Cell Value");
XSSFCellStyle style = workbook.createCellStyle();
BorderStyle border = BorderStyle.THIN;//Le type de bordure est spécifié ici.
style.setBorderBottom(border);//La bordure inférieure est spécifiée.
style.setBorderLeft(border);//La bordure gauche est spécifiée.
style.setBorderRight(border);//La bordure droite est spécifiée.
style.setBorderTop(border);//La bordure supérieure est spécifiée.
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("Feuille 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);//La couleur de la bordure est spécifiée ici.
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("Feuille 1");
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(1);
cell.setCellValue("Hello World.");
XSSFCellStyle style = workbook.createCellStyle();
style.setWrapText(true);//L'emballage est spécifié ici.
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("Feuille 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);//L'hyperlien est spécifié ici.
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("Feuille 1");
sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));//Les cellules à fusionner sont définies ici. (Première rangée, lastRow, firstCol, lastCol)
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
}
}
}
Recommended Posts