java 如何使用java和poi或任何其他库读取excel文件并将这些数据插入数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15835091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:59:57  来源:igfitidea点击:

how to read excel file and insert those data into database using java and poi or any other libraray?

javadatabaseexcelservlets

提问by user1847801

i am using servlet and trying to read the user uploaded excel file and insert into database. my excel is in this format: ID IP1 IP2 USER TKTNO(these are headings in excel & database table as well)

我正在使用 servlet 并尝试读取用户上传的 excel 文件并插入到数据库中。我的 excel 格式如下:ID IP1 IP2 USER TKTNO(这些也是 excel 和数据库表中的标题)

under those heading i have data in excel file which i have to read and insert into database. please desperately need help....thank you

在这些标题下,我在 excel 文件中有数据,我必须读取并插入到数据库中。请迫切需要帮助....谢谢

回答by Waqas Memon

I am using Docx4J for this purpose... good with Docx and xlsx http://www.docx4java.org/trac/docx4j

我为此目的使用 Docx4J... Docx 和 xlsx 很好 http://www.docx4java.org/trac/docx4j

回答by Hussain Akhtar Wahid 'Ghouri'

this is how you read an excel file using apache POI library , i guess this is good enough for starters , now you can take the cell values stored in some collection objects and store the object to Database according to requirement

这是您使用apache POI库读取excel文件的方式,我想这对初学者来说已经足够了,现在您可以根据需要获取存储在某些集合对象中的单元格值并将对象存储到数据库中

package com.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/mar_25/Tradestation_Q4 Dashboard_Week 5_1029-1104.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");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.println("numeric===>>>"+cell.getNumericCellValue() + "\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.println("String===>>>"+cell.getStringCellValue() + "\t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}