-1

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

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

4

2 回答 2

0

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

于 2013-04-05T13:31:38.070 回答
0

这就是使用 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();
        }
    }

}
于 2013-04-05T13:33:19.003 回答