<!-- poi-->
<!--xls(03)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--日期格式化工具-->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
package com.gg.poi;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class CreateExcelTest {
public static void main(String[] args) throws IOException {
parseExcel();
}
public static void createExcel() throws IOException {
Workbook workbook=new XSSFWorkbook();
Sheet sheet = workbook.createSheet("班级信息");
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("性别");
Cell cell12 = row1.createCell(1);
cell12.setCellValue("数量");
Row row2=sheet.createRow(1);
Cell cell21 = row2.createCell(0);
cell21.setCellValue("男");
Cell cell22 = row2.createCell(1);
cell22.setCellValue(31);
Row row3=sheet.createRow(2);
Cell cell31 = row3.createCell(0);
cell31.setCellValue("女");
Cell cell32 = row3.createCell(1);
cell32.setCellValue(1);
FileOutputStream fileOutputStream=new FileOutputStream("C:\\Users\\35125\\Desktop\\1.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("excel表生成完毕");
}
public static void parseExcel() throws IOException {
FileInputStream fileInputStream=new FileInputStream("C:\\Users\\35125\\Desktop\\1.xlsx");
Workbook workbook=new XSSFWorkbook(fileInputStream);
Sheet sheetAt = workbook.getSheetAt(0);
int rows=sheetAt.getPhysicalNumberOfRows();
for(int i=0;i<rows;i++){
Row row = sheetAt.getRow(i);
int cells=sheetAt.getRow(0).getPhysicalNumberOfCells();
for (int j = 0; j < cells; j++) {
Cell cell = row.getCell(j);
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC :
System.out.print(cell.getNumericCellValue()+" ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue()+" ");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue()+" ");
break;
case Cell.CELL_TYPE_FORMULA:
System.out.print(cell.getDateCellValue()+" ");
break;
case Cell.CELL_TYPE_BLANK:
System.out.println("null");
break;
}
}
System.out.println();
}
fileInputStream.close();
}
}