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