将excel表格数据导入数据库中---先将数据转化为java实体类

参考:https://www.cnblogs.com/zuoxh/p/9907869.html

@RequestMapping("/importExcel.do")
    public void import2(String xlsPath) throws IOException, BiffException {
    
    

        // 导入已存在的Excel文件,获得只读的工作薄对象
        FileInputStream fis = new FileInputStream(xlsPath);
        Workbook wk = Workbook.getWorkbook(fis);
        // 获取第一张Sheet表
        Sheet sheet = wk.getSheet(0);
        // 获取总行数
        int rowNum = sheet.getRows();
        System.out.println("插入总行数:"+rowNum);
        // 从数据行开始迭代每一行
        for (int i = 0; i < rowNum; i++) {
    
    
            //先判断插入的数据是否和数据库的数据重复
            if(userService.findUser(sheet.getCell(0, i).getContents())>0) {
    
    
                continue;
            }
            User u = new User();
            // getCell(column,row),表示取得指定列指定行的单元格(Cell)
            // getContents()获取单元格的内容,返回字符串数据。适用于字符型数据的单元格
            // 使用实体类封装单元格数据
            u.setName(sheet.getCell(0, i).getContents());
            u.setAge(sheet.getCell(1, i).getContents());
            u.setNickName(sheet.getCell(2, i).getContents());
            userService.saveUser(u);
            System.out.println("成功插入:"+sheet.getCell(0, i).getContents());



            /*// 判断单元格的类型,单元格主要类型LABEL、NUMBER、DATE
            if (sheet.getCell(2, i).getType == CellType.NUMBER) {

                // 转化为数值型单元格
                NumberCell numCell = (NumberCell) sheet.getCell(2, i);
                // NumberCell的getValue()方法取得单元格的数值型数据
                info.setRscore(numCell.getValue());

            }
            if (sheet.getCell(3, i).getType == CellType.NUMBER) {
                NumberCell numCell = (NumberCell) sheet.getCell(3, i);
                info.setRscore(numCell.getValue);
            }

            if (sheet.getCell(4, i).getType == CellType.DATE) {
                DateCell dateCell = (DateCell) sheet.getCell(4, i);
                // DateCell的getDate()方法取得单元格的日期型数据
                info.setDate(dateCell.getDate());
            }*/
        }
        fis.close();
        wk.close();
    }

实例:

package test.propertyimport;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.sound.sampled.LineListener;

import com.zhuozhengsoft.pageoffice.excelreader.Sheet;

import antlr.collections.List;
import jxl.Workbook;
import jxl.read.biff.BiffException;



public class ExcelTest0916 {
    
    

	public static void main(String[] args) throws BiffException, IOException {
    
    
		try {
    
    
			FileInputStream fileInputStream = new FileInputStream("E:\\lixwcs\\testImport1.xls");
			Workbook wk = Workbook.getWorkbook(fileInputStream);
			jxl.Sheet sheet = wk.getSheet(0);
			int rowNum = sheet.getRows();
			System.out.println("excel中数据的行数:"+ rowNum);
			java.util.List<Property> listPropertyList = new ArrayList<Property>();

		for(int i = 0; i < rowNum; i++) {
    
    
			Property property = new Property();
			property.setName(sheet.getCell(1, i).getContents());
			property.setRegNo(sheet.getCell(2, i).getContents());
			property.setFileLoaction(sheet.getCell(3, i).getContents());
			if(property != null) {
    
    
				listPropertyList.add(property);
			}
		}
		for (int i = 0; i < listPropertyList.size(); i++) {
    
    
			System.out.println(listPropertyList.get(i));
		}
		// 记得关闭流
		fileInputStream.close();
		wk.close();
			
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		}

	}

}

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/108621026