我一直在关注Stack Overflow很长一段时间,无论何时何地都需要帮助。我所拥有的大多数问题通常都已经得到解答。 :)这个问题也得到了回答,但是还没有帮助,所以直接在这里询问。
我正在处理的要求是: 进入excel文件(无论大小)并计算此excel文件中的工作表数和总行数(所有工作表中的行数之和)。 我的代码适用于较小的文件,但是当从较大的文件中读取时,Eclipse放弃了。
我的代码是:
import java.io.File;
import java.util.*;
import java.io.*;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportCheck
{
public static void main(String[] args)
{
List<String> listFile = new ArrayList<String>();
int count = 0; int pageCount;
int i; int totalRows, currentSheetRows;
int sheetCount = 0;
String filePath="";
String path;
System.out.println("enter path:");
Scanner in = new Scanner(System.in);
path= in.nextLine();
File f = new File(path);
File[] files = f.listFiles();
if (files != null)
{
//getting the name of the file
for (i = 0; i < files.length; i++)
{
listFile.add(files[i].getName());
count++;
}
//getting the file size and extension of the file
for(int j=0; j<count;j++)
{
filePath= path+"/"+listFile.get(j);
String fileExtension = getExtension(filePath);
System.out.println("File "+(j+1)+":\nName:"+listFile.get(j)+"\tSize: "+getFileSize(filePath)+" KB");
//excel operations - to get no. of worksheets and total no. of rows in excel
if(fileExtension.equals("xlsx"))
{
try
{
totalRows=0;
FileInputStream inputStream = new FileInputStream(new File(filePath));
Workbook wb = new XSSFWorkbook(inputStream);
sheetCount = wb.getNumberOfSheets();
for(i=0;i<sheetCount;i++)
{
Sheet currentSheet = wb.getSheetAt(i);
currentSheetRows=currentSheet.getPhysicalNumberOfRows();
totalRows=totalRows+currentSheetRows;
}
System.out.println("No. of sheets in excel: "+sheetCount+"\tTotal No. of rows: "+totalRows);
wb.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static long getFileSize(String filename)
{
File file = new File(filename);
if (!file.exists() || !file.isFile())
{
System.out.println("File doesn\'t exist");
return -1;
}
long length = file.length()/1024;
return length;
}
public static String getExtension(String filename)
{
String extension = "";
int i = filename.lastIndexOf('.');
if (i > 0)
extension = filename.substring(i+1);
return(extension);
}
}
我已将最大堆增加到1 gig,但这仍然无用。
我得到的错误是:
任何帮助将不胜感激。