JAVA small exercise-7 POI processing Excel file-read XLS file

Prerequisite: POI 4.1.2 corresponds to JDK1.8, if the version does not correspond, for example, JDK1.7 is definitely not working, and I will make a lot of detours afterwards.

  1. POI jar package download
    POI official website download address

  2. Unzip after downloading

Insert picture description hereInsert picture description here

Insert picture description here

  1. Copy the jar package (all jar packages, including the jar package in the lib folder) in the unzipped folder to the lib folder in the newly created java project "Excel" (the folder built by yourself)

Insert picture description here

  1. Select all jar packages, right-click and select "Build path"->"add to build path" to add all jar packages to the path.

5 The error "Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/poi/ss/usermodel/Sheet: Unsupported major.minor version 52.0" is reported during operation. Baidu found that the current jdk version is too low. , I use the JDK 1.7
solution:

(1) Download JDK1.8, and install:

Baidu cloud disk address:
https://pan.baidu.com/s/1s5VbBpjaKn9klEr2iYTCiw
Extraction code: 40i8
(2) Configure JDK1.8 environment
Reference: https://blog.csdn.net/sunrise52java/article/details/88536711
( 3) Configure eclipse

Click Window->Preferences->JAVA->Installed JRES->Add to
Insert picture description here
select Standard VM, click Next,
Insert picture description here
click Directory, browse to select the JDK installation path (jdk not jre)

Insert picture description here
Click Finish->Apply to create a JAVA project. The project created at this time is based on JDK1.8.

  1. Read XLS file

Insert picture description here

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();
		}
		}
}

Output:

日期
ctt:NUMERIC
20201115日
ctt:NUMERIC
20201116日
ctt:NUMERIC
20201117日
ctt:NUMERIC
20201118

8. Read XLS with merged cells

Insert picture description here

	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();
		}
	}
}

Guess you like

Origin blog.csdn.net/KathyLJQ/article/details/109695789