Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取大型excel文件的前n行_Java_Excel_Error Handling - Fatal编程技术网

Java 如何读取大型excel文件的前n行

Java 如何读取大型excel文件的前n行,java,excel,error-handling,Java,Excel,Error Handling,因此,我试图编写一个程序,扫描excel文件行中的特定模式。也就是说,N后跟任何字母,然后是S或T(每个字母占用一个单元格) 问题是,我使用的excel文件绝对庞大,大约有3000行和近1000列。为了减少java堆空间,我尝试只在前60行中搜索此模式。我如何使我的算法适合这样做?我的内存仍然不足 我的代码如下: import java.awt.List; import java.io.File; import java.io.FileInputStream; import java.io.IO

因此,我试图编写一个程序,扫描excel文件行中的特定模式。也就是说,N后跟任何字母,然后是S或T(每个字母占用一个单元格)

问题是,我使用的excel文件绝对庞大,大约有3000行和近1000列。为了减少java堆空间,我尝试只在前60行中搜索此模式。我如何使我的算法适合这样做?我的内存仍然不足

我的代码如下:

import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

    public int Reader(File file) throws IOException, EncryptedDocumentException, InvalidFormatException {
        FileInputStream fis = new FileInputStream(file);
        String filepath = file.getPath();
        Workbook wb = WorkbookFactory.create(new File(filepath));
        XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);
        XSSFRow row;
        XSSFCell cell;
        ArrayList<Integer> list = new ArrayList<Integer>();

        int rows;
        int cols = 0;
        int temp = 0;
        rows = sheet.getPhysicalNumberOfRows();

        for (int i = 0; i < 10 || i < 60; i++) {
            row = sheet.getRow(i);
            if (row != null) {
                temp = sheet.getRow(i).getPhysicalNumberOfCells();
                if (temp > cols)
                    cols = temp;
            }
        }
        for (int r = 0; r <= 60; r++) {
            row = sheet.getRow(r);
            if (row != null) {
                for (int c = 0; c <= cols; c++) {
                    int numblanks = 0;
                    cell = row.getCell((short) c);
                    if (cell != null) {
                        //System.out.print(cell + "\t\t");
                    } else {
                        //System.out.print("\t\t");
                    }
                    if (cell != null && cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        if ("N".equals(cell.getStringCellValue())) {
                            for (int k = c; k <= cols; k++) {
                                if ("-".equals(row.getCell(k).getStringCellValue())) {
                                    numblanks++;
                                    continue;
                                }
                                if ("S".equals(row.getCell(c + 2 + numblanks).getStringCellValue())
                                        || "T".equals(row.getCell(c + 2 + numblanks).getStringCellValue())) {
                                    list.add((int) sheet.getRow(1).getCell(c).getNumericCellValue());
                                    break;
                                }
                            }
                        }
                    }
                }
                System.out.println();
            }
        }
        System.out.println();
        System.out.println("Rows: " + rows);
        System.out.println("Columns: " + cols);
        System.out.println(list);
        return temp;
    }
}
导入java.awt.List;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入org.apache.poi.EncryptedDocumentException;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.usermodel.WorkbookFactory;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共课优秀读者{
公共int读取器(文件)引发IOException、EncryptedDocumentException、InvalidFormatException{
FileInputStream fis=新的FileInputStream(文件);
字符串filepath=file.getPath();
工作簿wb=WorkbookFactory.create(新文件(filepath));
XSSFSheet sheet=(XSSFSheet)wb.getSheetAt(0);
XSSFRow行;
XSSFCell细胞;
ArrayList=新建ArrayList();
int行;
int cols=0;
内部温度=0;
rows=sheet.getPhysicalNumberOfRows();
对于(int i=0;i<10 | | i<60;i++){
行=表。获取行(i);
如果(行!=null){
temp=sheet.getRow(i).getPhysicalNumberOfCells();
如果(温度>冷却液)
cols=温度;
}
}

对于(int r=0;r转换为CSV文件,这很容易做到。 如果可能的话,我会将数据插入到数据库表中,并使用一个过程来搜索和查找您要查找的内容。
这可以使用Spring批处理和Java来完成

您不能将其转换为CSV,然后从文件中任意读取n个字节吗?在VBA中,您可以使用
ExecuteExcel4Macro(“'”&path&“[”&file&“]”&sheet&“!”&range)
在不打开文件的情况下获取值…但我怀疑java中是否有类似的功能…但也许您可以使用一些转换或创建一个“自动运行”工作簿,该工作簿使用此功能仅为原始文件的一部分创建副本…只是一个想法。哪一行给您带来了问题???您打算这样做10次还是60次::(inti=0;i<10 | | i<60;i++)和“-”是否真的要计算空格或连字符???也许find方法可以帮助您我不知道您是否意识到这一点,但是*.xlsx文件实际上是一个压缩的XML结构,但它很难理解。如果您能够理解它是如何工作的,您可能会对该结构启动一些
XPath
查询,从而更轻松地获取信息。