1.POI簡單介紹
Apache POI 是用Java 編寫的免費(fèi)開源的跨平臺(tái)的 Java API面睛,Apache POI提供API給Java程式對 Microsoft Office 格式檔案讀和寫的功能
- HSSF 提供讀寫Microsoft Excel XLS格式檔案的功能。
- XSSF 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能忆某。
- HWPF 提供讀寫Microsoft Word DOC格式檔案的功能统捶。
- HSLF 提供讀寫Microsoft PowerPoint格式檔案的功能顶捷。
- HDGF 提供讀Microsoft Visio格式檔案的功能蝶念。
- HPBF 提供讀Microsoft Publisher格式檔案的功能。
- HSMF 提供讀Microsoft Outlook格式檔案的功能堤尾。
2.步驟
1.環(huán)境配置:導(dǎo)入jar包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
2.創(chuàng)建一個(gè)Excel工作簿
@Test
public void test() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel工作蒲.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}
3.創(chuàng)建一個(gè)sheet頁
@Test
public void sheet() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//創(chuàng)建sheet頁面
wb.createSheet("第一個(gè)sheet頁");
wb.createSheet("第二個(gè)sheet頁");
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel工作蒲帶有sheet頁.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}
sheet頁
4.創(chuàng)建行和列
@Test
public void row() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//創(chuàng)建sheet頁面
Sheet sheet = wb.createSheet("學(xué)生信息sheet頁");
//創(chuàng)建一行
Row row = sheet.createRow(0);
//創(chuàng)建一個(gè)單元格
Cell cell =null;
for(int i = 0 ;i<5;i++){
row.createCell(i).setCellValue("寫入信息:單元格內(nèi)容"+i);
}
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel學(xué)生信息.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}
5.創(chuàng)建一個(gè)時(shí)間樣式到Excel
@Test
public void date() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//創(chuàng)建sheet頁面
Sheet sheet = wb.createSheet("時(shí)間sheet頁");
//創(chuàng)建一行
Row row = sheet.createRow(0);
//創(chuàng)建一個(gè)單元格
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
CreationHelper creationHelper = wb.getCreationHelper();
//設(shè)置單元格樣式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("YYYY-MM-DD hh:mm:ss"));
cell = row.createCell(1);
cell.setCellValue(new Date());
//設(shè)置日期樣式
cell.setCellStyle(cellStyle);
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel日期格式.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}
6.單元格對其方式及行高
@Test
public void style() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//創(chuàng)建sheet頁面
Sheet sheet = wb.createSheet("第一個(gè)sheet");
//創(chuàng)建一行
Row row = sheet.createRow(0);
//設(shè)置行高
row.setHeightInPoints(30);
//創(chuàng)建一個(gè)單元格
createCell(wb,row,(short)0,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_JUSTIFY,HSSFCellStyle.VERTICAL_CENTER);
createCell(wb,row,(short)2,HSSFCellStyle.ALIGN_CENTER_SELECTION,HSSFCellStyle.VERTICAL_JUSTIFY);
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel樣式.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}
/**
* 創(chuàng)建一個(gè)單元格設(shè)置對應(yīng)的對其方式
* @param workbook 工作蒲
* @param row 行
* @param column 列
*/
private static void createCell(Workbook workbook, Row row, short column,short halign,short valign){
Cell cell = row.createCell(column);//創(chuàng)建單元格
cell.setCellValue(new HSSFRichTextString("我是富文本"));//設(shè)置值
CellStyle cellStyle = workbook.createCellStyle();//創(chuàng)建樣式
cellStyle.setAlignment(halign);//設(shè)置單元格水平方向?qū)ζ浞绞? cellStyle.setVerticalAlignment(valign);//設(shè)置單元格垂直方向?qū)ζ浞绞? cell.setCellStyle(cellStyle);
}
合并單元格
@Test
public void test1() throws IOException {
//定義一個(gè)工作蒲
Workbook wb = new HSSFWorkbook();
//創(chuàng)建sheet頁面
Sheet sheet = wb.createSheet("第一個(gè)sheet");
//創(chuàng)建一行
Row row = sheet.createRow(1);
//設(shè)置行高
row.setHeightInPoints(30);
//創(chuàng)建一個(gè)單元格
Cell cell = row.createCell(1);
cell.setCellValue("合并單元格");
//合并單元格(起始行,結(jié)束行,起始列,結(jié)束列)
sheet.addMergedRegion(new CellRangeAddress(1,2,1,2));
//定義一個(gè)輸出流
FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel樣式.xls");
//寫入在輸出流
wb.write(fileOutputStream);
//關(guān)閉輸出流
fileOutputStream.close();
}