Java - Poi 操作 Excel
關注 “弋凡”(YiFan)微信公衆號吧 記錄簡單筆記 做你的最愛
-
注意
- XSSFWorkbook 對象是操作 .xlsx 格式的表格
- HSSFWorkbook 對象是操作 .xls 格式的表格
- xls格式 <= 65536 行
- xlsx格式 > 65536 行
-
導入pom文件
<!-- .xlsx格式 7版本-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- 日期格式化工具!-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
public static void main(String[] args) throws Exception {
String PATH = "E:/IDEA_Project/hzmv/";
// 創建一個 Excel
Workbook workbook = new XSSFWorkbook();
// 創建一個工作表
Sheet sheet = workbook.createSheet("HZMV~數據源");
// 創建一行
Row row1 = sheet.createRow(0);
// 創建一個單元格 (1,1)
Cell cell1 = row1.createCell(0);
cell1.setCellValue("鏈接");
//(1,2)
Cell cell2 = row1.createCell(1);
cell2.setCellValue("格式");
// 創建一行
Row row2 = sheet.createRow(1);
Cell cell3 = row2.createCell(0);
String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
cell3.setCellValue(time);
// 生成一張表(io流)
FileOutputStream stream = new FileOutputStream(PATH + "hzmv數據源.xlsx");
workbook.write(stream);
stream.close();
System.err.println("hzmv數據源生產完畢~");
}
public static void main(String[] args) throws Exception {
String PATH = "E:/IDEA_Project/hzmv/";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "hzmv數據源.xlsx");
// 創建一個工作簿 execl能使用的操作 這裏都能使用
Workbook workbook = new XSSFWorkbook(inputStream);
// 拿到第一個表
Sheet sheetAt = workbook.getSheetAt(0);
System.err.println(sheetAt);
// 拿到第行
Row row1 = sheetAt.getRow(0);
//拿到第一個單元格
Cell cell1 = row1.getCell(1);
// 讀取的時候一定要注意數據類型
/*
* getStringCellValue 字符串類型
* getNumericCellValue 數字類型
* */
System.err.println(cell1.getStringCellValue());
}
public static void main(String[] args) throws Exception {
String PATH = "E:/IDEA_Project/hzmv/";
// 獲取文件流
FileInputStream inputStream = new FileInputStream(PATH + "hzmv數據源.xlsx");
// 創建一個工作簿 execl能使用的操作 這裏都能使用
Workbook workbook = new XSSFWorkbook(inputStream);
// 拿到第一個表
Sheet sheetAt = workbook.getSheetAt(0);
// 獲取標題內容
Row rowTit = sheetAt.getRow(0);
if(rowTit != null ){
// 得到所有的列~
int cellCount = rowTit.getPhysicalNumberOfCells();
// 遍歷
for (int cellnum = 0; cellnum < cellCount ; cellnum++) {
// 得到單元格
Cell cell = rowTit.getCell(cellnum);
if(cell != null ){
CellType cellType = cell.getCellType();
String cellValue = "";
switch (cellType){
case _NONE:
break;
case NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case STRING :
cellValue = cell.getStringCellValue();
break;
case FORMULA:
break;
case BLANK:
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
break;
}
System.err.print(cellValue+"->"+cellType+" | ");
}
}
}
System.err.println();
// 獲取表中的內容
int rowCount = sheetAt.getPhysicalNumberOfRows();
for (int rownum = 1; rownum < rowCount ; rownum++) {
Row rowData = sheetAt.getRow(rownum);
if(rowData != null){
// 得到所有的列
int cellCount = rowData.getPhysicalNumberOfCells();
for (int cellnum = 0; cellnum < cellCount ; cellnum++) {
// 得到每個單元格
Cell cell = rowData.getCell(cellnum);
if(cell != null ){
// 得到每個單元格的數據的類型~
CellType cellType = cell.getCellType();
String cellValue = "";
switch (cellType){
case _NONE:
cellValue= "null";
break;
case NUMERIC:
// 日期
if(DateUtil.isCellDateFormatted(cell)){
Date value = cell.getDateCellValue();
cellValue = new DateTime(value).toString("yyyy-MM-dd HH:mm:ss");
break;
}else {
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case STRING :
cellValue = cell.getStringCellValue();
break;
case FORMULA:
break;
case BLANK:
cellValue= "null";
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
cellValue= "error";
break;
}
System.err.print(cellValue+"->"+cellType+" | ");
}
}
System.err.println();
}
}
inputStream.close();
}