MySQL 데이터베이스에 Excel 데이터 가져오기

8518 단어
package com.nongyuanbao.farm.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.neptune.core.project.AutoIdHelper;
import com.neptune.file.service.FileHelper;
import com.nongyuanbao.farm.model.Crops;

public class ReadExcelServer {

    public void add() throws FileNotFoundException, IOException {
    // Excel File excelFile
= new File("E:\\A3.xlsx"); XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(excelFile)); XSSFSheet sheet = wb.getSheetAt(0); // List pictures = wb.getAllPictures(); // for (Row row : sheet) { for (int i = 0; i < sheet.getLastRowNum(); i++) { // // for (Cell cell : row) { // set save
       XSSFRow row = sheet.getRow(i);
       Crops crops = new Crops()
       for (int s = 0; s < row.getLastCellNum(); s++) {
          // 
                Cell cell = row.getCell(s);
                // }
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:

                    System.out.print(cell.getRichStringCellValue().getString());
                    // s 
                    //N 
                    if (s == 0) {
                        crops.setId(AutoIdHelper.getId());
                        crops.setFarmName(cell.getRichStringCellValue().getString());
                    } else if (s == 1) {//N 2 
                        crops.setFarmCrops(cell.getRichStringCellValue().getString());
                    } else if (s == 2) {//N 3 
                        crops.setGatherTime(cell.getRichStringCellValue().getString());
                    } else if (s == 3) {//N 4 
                        crops.setNongYuanBaoId(cell.getRichStringCellValue().getString());
                    }

                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.print(String.valueOf(cell.getDateCellValue()));
                    } else {
                        System.out.print(cell.getNumericCellValue());
                    }
                    System.out.print("|");
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    System.out.print("|");
                    break;
                default:
                }

            }
            // ( )
            XSSFPictureData pictureData = pictures.get(i);
            byte[] data = pictureData.getData();
            if (data==null) {
                crops.setImage(null);
            }else {
                crops.setImage(new String(Base64.encodeBase64(data)));
            }
            System.out.println(new String(Base64.encodeBase64(data)));
            // 
            crops.save();

            System.out.println();
        }

        System.out.println(wb.getSheetName(0));
        renderSuccess(" !!");
    }


}

 
다음으로 전송:https://www.cnblogs.com/wuaili/p/8652608.html

좋은 웹페이지 즐겨찾기