java程序读取特定数据

标签 java excel

我需要java代码从Excel工作表中读取特定列的数据。 –(lo 号、行、凭证号、STLoc、数量、 Activity 。) 特定列的这些值集将用于 sql 查询(jdbc-odbc 连接完成)。 查询的输出将与此表中的列相匹配(这部分将在稍后完成) 请帮忙。 sample excel sheet

最佳答案

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excelfilereading;

/**
 *
 * @author vkantiya
 */
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class Main {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
//
// An excel file name. You can create a file name with a full
// path information.
//
        String filename = "FirstExcel.xls";


// Create an ArrayList to store the data read from excel sheet.
//
        List sheetData = new ArrayList();

        FileInputStream fis = null;
        try {
//
// Create a FileInputStream that will be use to read the
// excel file.
//
            fis = new FileInputStream(filename);

//
// Create an excel workbook from the file system.
//
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
            HSSFSheet sheet = workbook.getSheetAt(0);

//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();
                    data.add(cell);
                }

                sheetData.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        showExelData(sheetData);
    }

    private static void showExelData(List sheetData) {
//
// Iterates the data and print it out to the console.
//
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                HSSFCell cell = (HSSFCell) list.get(j);
                System.out.print(
                        cell.getRichStringCellValue().getString());
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }
    }
}

关于java程序读取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9415011/

相关文章:

vba - Excel VBA : Range match with conditions

c# - 用于反透视数据表的 Lambda 表达式

Java 错误 - 找不到符号

java - 返回具有对象数组返回类型的 int 数组

java - 应用程序在执行 AsyncTask 后不显示任何内容

java - 如何从 testContext.xml 中的组件扫描中排除 @Configuration 类

SharePoint 中的 VBA MKDIR 失败

excel - 如何在excel中将字符串分解为子字符串?

excel - "Expected End of Statement"VBA 中 Target.Address 错误

java - 在 NetBeans 中打开项目时缺少插件