您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将Excel数据转换为MySql表

将Excel数据转换为MySql表

这是您阅读Excel文件并将其存储在集合对象中的方式

    import java.io.*;
    import java.util.Iterator;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ReadExcelFile {
        public static void main(String[] args) 
        {
            try {

                FileInputStream file = new FileInputStream(new File("C:/Users/hussain.a/Desktop/newExcelFile.xlsx"));
                XSSFWorkbook workbook = new XSSFWorkbook(file);
                XSSFSheet sheet = workbook.getSheetAt(0);
                Iterator<Row> rowIterator = sheet.iterator();
                rowIterator.next();
                while(rowIterator.hasNext())
                {
                    Row row = rowIterator.next();
                    //For each row, iterate through each columns
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while(cellIterator.hasNext())
                    {
                        Cell cell = cellIterator.next();
                        switch(cell.getCellType()) 
                        {
                            case Cell.CELL_TYPE_BOOLEAN:
                                System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");
// write hibernate lines here to store it in your domain
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                System.out.println("numeric===>>>"+cell.getNumericCellValue() + "\t");
// write hibernate lines here to store it in your domain
                                break;
                            case Cell.CELL_TYPE_STRING:
                                System.out.println("String===>>>"+cell.getStringCellValue() + "\t");
// write hibernate lines here to store it in your domain
                                break;
                        }
                    }
                    System.out.println("");
                }
                file.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

之后,您可以使用hibernate模式并存储在您的域类中

MySQL 2022/1/1 18:21:36 有511人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶