生活随笔
收集整理的這篇文章主要介紹了
Java - Poi 操作 Excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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/";Workbook workbook
= new XSSFWorkbook();Sheet sheet
= workbook
.createSheet("HZMV~數據源");Row row1
= sheet
.createRow(0);Cell cell1
= row1
.createCell(0);cell1
.setCellValue("鏈接");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
);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");Workbook workbook
= new XSSFWorkbook(inputStream
);Sheet sheetAt
= workbook
.getSheetAt(0);System
.err
.println(sheetAt
);Row row1
= sheetAt
.getRow(0);Cell cell1
= row1
.getCell(1);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");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();
}
快來關注“弋凡”微信公眾號吧
總結
以上是生活随笔為你收集整理的Java - Poi 操作 Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。