Requisito previo: POI 4.1.2 corresponde a JDK1.8, si la versión no corresponde, por ejemplo, JDK1.7 definitivamente no funciona, y luego daré muchos desvíos.
-
Descarga del paquete POI jar
dirección de descarga del sitio web oficial de POI -
Descomprimir después de descargar
- Copie el paquete jar (todos los paquetes jar, incluido el paquete jar en la carpeta lib) en la carpeta descomprimida a la carpeta lib en el proyecto java recién creado "Excel" (la carpeta creada por usted mismo)
- Seleccione todos los paquetes jar, haga clic con el botón derecho y seleccione "Crear ruta" -> "agregar a la ruta de construcción" para agregar todos los paquetes jar a la ruta.
5 El error "Excepción en el hilo" principal "java.lang.UnsupportedClassVersionError: org / apache / poi / ss / usermodel / Sheet: Unsupported major.minor version 52.0" se reporta durante la operación. Baidu encontró que la versión actual de jdk es demasiado baja ., Uso la
solución JDK 1.7 :
(1) Descargue JDK1.8 e instale:
Dirección del disco en la nube de Baidu:
https://pan.baidu.com/s/1s5VbBpjaKn9klEr2iYTCiw
Código de extracción: 40i8
(2) Configurar el entorno JDK1.8
Referencia: https://blog.csdn.net/sunrise52java/article/details/88536711
( 3) Configurar eclipse
Haga clic en Ventana-> Preferencias-> JAVA-> JRES instalado-> Agregar para
seleccionar VM estándar, haga clic en Siguiente,
haga clic en Directorio, busque y seleccione la ruta de instalación de JDK (jdk no jre)
Haga clic en Finalizar-> Aplicar para crear un proyecto JAVA El proyecto creado en este momento se basa en JDK1.8.
- Leer archivo XLS
package tests;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class ReadExcel {
public static void main(String[] args){
try {
File f = new File("C:\\Users\\Admin\\Desktop\\测试.xls");
FileInputStream fis = new FileInputStream(f);
HSSFWorkbook wb = new HSSFWorkbook(fis);
//Sheet st = wb.getSheetAt(0);//获取工作表,此处为第一张工作表
Sheet st = wb.getSheet("新表");//通过表名来获取表
int no = st.getLastRowNum();//获取最后一行的下标,如果是空的则为-1
//int c_no = st.getLeftCol();//获取最后一列的下标,如果是空的则为-1
System.out.println(no);
Object content;
if(no!=-1){
int x = 0;
while(x<=no){
Row row = st.getRow(x);//获取第x行
Cell c = row.getCell(1);//获取第x行第二列的单元格
CellType ct = c.getCellType();
String ctt = ct.toString();
System.out.println("ctt:"+ctt);
switch(ctt)
{
case "STRING":
content = c.getStringCellValue();
break;
case "NUMERIC":
Date d = c.getDateCellValue();//单元格为日期类型可用Date接收
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
content = sdf.format(d);//格式化日期
break;
default:
content = "这个数据类型未知";
}
System.out.println(content.toString());
x++;
}
}
wb.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Producción:
日期
ctt:NUMERIC
2020年11月15日
ctt:NUMERIC
2020年11月16日
ctt:NUMERIC
2020年11月17日
ctt:NUMERIC
2020年11月18日
8. Leer XLS con celdas combinadas
public static void main(String[] args) {
try {
File f = new File("C:\\Users\\Admin\\Desktop\\测试.xls");
FileInputStream fis = new FileInputStream(f);
HSSFWorkbook wb = new HSSFWorkbook(fis);
//Sheet st = wb.getSheetAt(0);//获取工作表,此处为第一张工作表
Sheet st = wb.getSheet("新表");//通过表名来获取表
int no = st.getLastRowNum();//获取最后一行的下标,如果是空的则为-1
//int c_no = st.getLeftCol();//获取最后一列的下标,如果是空的则为-1
System.out.println(no);
Object content;
if(no!=-1){
int x = 0;
while(x<=no){
Row row = st.getRow(x);//获取第x行
Cell c = row.getCell(0);//获取第x行第二列的单元格
CellType ct = c.getCellType();
String ctt = ct.toString();
System.out.println("=============================");
System.out.println("ctt:"+ctt);
System.out.println(c.getColumnIndex());
System.out.println(c.getRowIndex());
switch(ctt)
{
case "STRING":
content = c.getStringCellValue();
break;
case "NUMERIC":
content = (int) c.getNumericCellValue();
break;
case "BLANK"://合并单元格只会读到第一个格的值,其余单元格getCellType()返回为“BLANK"
content = "null";
break;
default:
content = "这个数据类型未知";
}
System.out.println(content.toString());
x++;
}
}
wb.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}