【Excel-java利用POI 读取Excel数据的真实有效内容
首先导入依赖包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>RELEASE</version>
</dependency>
然后写代码
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class XLSReadCreate { public static void main(String[] args){ //1.创建输入流 try { FileInputStream fip = new FileInputStream("F:\\ceshi_read.xlsx"); //2.在输入流中获取工作簿 XSSFWorkbook wb = new XSSFWorkbook(fip); //3.在工作簿获取目标工作表 // Sheet sheet = wb.getSheet("Sheet1"); Sheet sheet = wb.getSheetAt(0); //4.在工作表中获取目标行 Row row = sheet.getRow(0); //5.在目标行中获取目标单元格的值 String s1 = row.getCell(0).getStringCellValue(); String s2 = row.getCell(1).getStringCellValue(); String s3 = row.getCell(2).getStringCellValue(); String s4 = row.getCell(3).getStringCellValue(); String s5 = row.getCell(4).getStringCellValue(); System.out.println(s1+"\t"+s2+"\t"+s3+"\t"+s4+"\t"+s5); //获取有效行 int n = sheet.getPhysicalNumberOfRows(); System.out.println("单元格的行数:"+n); for(int i = 1;i<n;i++){ row = sheet.getRow(i); int a1 = (int) row.getCell(0).getNumericCellValue(); String a2 = row.getCell(1).getStringCellValue(); Date a3 = row.getCell(2).getDateCellValue(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd"); String a33 = simpleDateFormat.format(a3); double a4 = row.getCell(3).getNumericCellValue(); String a5 = row.getCell(4).getStringCellValue(); System.out.printf("%d\t%s\t%s\t%.1f\t%s\n",a1,a2,a33,a4,a5); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
=

浙公网安备 33010602011771号