package tests;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadXLSX {
public static void main(String[] args) {
File f = new File("C:\\Users\\Admin\\Desktop\\测试01.xlsx");
FileInputStream fip;
try {
fip = new FileInputStream(f);
XSSFWorkbook xwb = new XSSFWorkbook(fip);
XSSFSheet st = xwb.getSheet("新表");//获取表
int row_sum = st.getLastRowNum();
for (int i = 0;i<=row_sum;i++){
XSSFRow sxr = st.getRow(i);
int col_sum = sxr.getPhysicalNumberOfCells();
//String info = "";
for(int j = 0; j < col_sum;j++){
XSSFCell sxc = sxr.getCell(j);
CellType ct = sxc.getCellType();
String ctt = ct.toString();
Object content;
switch(ctt)
{
case "STRING":
content = sxc.getStringCellValue();
break;
case "NUMERIC":
int v = (int)sxc.getNumericCellValue();
if(v>3000){
Date d = sxc.getDateCellValue();//单元格为日期类型可用Date接收
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
content = sdf.format(d);//格式化日期
}else{
content = v;
}
break;
default:
content = "这个数据类型未知";
}
//System.out.print(info);
System.out.print(content+"\t");
}
System.out.println("");
}
fip.close();
xwb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output:
package tests;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadXLSXRe {
public static void main(String[] args) {
File f = new File("C:\\Users\\Admin\\Desktop\\测试01.xlsx");
FileInputStream fip;
try {
fip = new FileInputStream(f);
XSSFWorkbook xwb = new XSSFWorkbook(fip);
XSSFSheet st = xwb.getSheet("新表");//获取表
int row_sum = st.getLastRowNum();
for (int i = 0;i<=row_sum;i++){
String val = "";
XSSFRow sxr = st.getRow(i);
int col_sum = sxr.getPhysicalNumberOfCells();
//String info = "";
for(int j = 0; j < col_sum;j++){
XSSFCell sxc = sxr.getCell(j);
val = ReadXLSXRe.getValue(sxc);
System.out.print(val+"\t\t");
}
System.out.println("");
}
fip.close();
xwb.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getValue(XSSFCell xsc){
String val = "";
CellType ct = xsc.getCellType();
String ctt = ct.toString();
switch(ctt)
{
case "STRING":
val = xsc.getStringCellValue();
break;
case "NUMERIC":
int v = (int)xsc.getNumericCellValue();
if(v>3000){
Date d = xsc.getDateCellValue();//单元格为日期类型可用Date接收
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
val = sdf.format(d)+"";//格式化日期
}else{
val = v+"";
}
break;
case "BLANK"://合并单元格只会读到第一个格的值,其余单元格getCellType()返回为“BLANK"
val = "null";
break;
default:
val = "这个数据类型未知";
}
return val;
}
}