在jxl中编写Excel单元时出现内存不足错误(JAVA - Out Of Memory Error while writing Excel Cells in jxl)
我正在使用JXL编写50000行和30列的excel文件。 我的代码如下所示:
for(int j = 0; j <countOfRows; j ++){
myWritableSheet.addCell(new Label(0, j, myResultSet.getString(1), myWritableCellFormat)); myWritableSheet.addCell(new Label(1, j, myResultSet.getString(2), myWritableCellFormat)); ..... .....
}
在编写单元格时,程序变得越来越慢
最后在25000行左右出现以下错误:
线程“Thread-3”中的异常java.lang.OutOfMemoryError:jxl.write.biff.WritableSheetImpl.getRowRecord上的Java堆空间(WritableSheetImpl.java:984)位于jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:951 )在KLL.ConverterMainFrame $ exportToXLSBillRightsThread.run(ConverterMainFrame.java:6895)
在Java中处理内存总是很困难。
在这种情况下,它似乎是jxl的问题。
有没有办法写出文件,清除内存,并coninue每1000个单元格写入单元格?
这会是一个好主意吗?或者你会提出什么样的解决方案?
I am using JXL to write an excel file of 50000 rows and 30 columns. My code looks like this:
for (int j = 0; j < countOfRows; j++) {
myWritableSheet.addCell(new Label(0, j, myResultSet.getString(1), myWritableCellFormat)); myWritableSheet.addCell(new Label(1, j, myResultSet.getString(2), myWritableCellFormat)); ..... .....
}
While writing the cells, the program goes slower and slower
and finally around the row 25000 I am getting the following error:
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space at jxl.write.biff.WritableSheetImpl.getRowRecord(WritableSheetImpl.java:984) at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:951) at KLL.ConverterMainFrame$exportToXLSBillRightsThread.run(ConverterMainFrame.java:6895)
It's always difficult in Java to handle the memory.
In this case it seems to be the jxl's problem.
Is there a way to write the file, clear the memory and coninue writing cells every 1000 cells?
Would that be a good idea or what else would you propose as a solution?
原文:https://stackoverflow.com/questions/3167029
最满意答案
问题是您正在从
main
方法调用非静态方法(sumLists
)。main
方法是static,因此只能访问同一个类中的其他静态方法。除非......你创建了一个例如
Solution
的实例:public static void main(String... args) { Solution s = new Solution(); s.sumLists(...); // now you invoke it using an instance method (i.e. a non-static method) }
所以,你的选择是:
- 让你的方法保持静态。
- 创建类的实例并调用非静态方法。
有关这方面的一些好的阅读,请查看Oracle教程
The problem is that you are invoking a non-static method (
sumLists
) from youmain
method. Themain
method is static and can therefore only access other static methods within the same class.Unless... You create an instance of e.g.
Solution
:public static void main(String... args) { Solution s = new Solution(); s.sumLists(...); // now you invoke it using an instance method (i.e. a non-static method) }
So, your options are:
- Make your methods static.
- Create an instance of the class and invoke the non-static methods.
For some good reading about this take a look at the Oracle tutorial
相关问答
更多-
public static void main(String[] args) { int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 }; counting(a); printCount(a); } 在printCount()方法中传递数组。 public static void main(String[] args) { int[] a = { 2, 3, 4, 3, 3, ...
-
问题是您正在从main方法调用非静态方法( sumLists )。 main方法是static,因此只能访问同一个类中的其他静态方法。 除非......你创建了一个例如Solution的实例: public static void main(String... args) { Solution s = new Solution(); s.sumLists(...); // now you invoke it using an instance method (i.e. a non-stati ...
-
你没有得到任何输出,因为你实际上没有打印任何东西。 如果要打印某些内容,则必须与程序的stdout或stderr进行交互。 例如: public static void print(String str) { System.out.print(str); } You aren't getting any output because you aren't actually printing anything. If you want to print something, you'll have ...
-
您应该能够将返回到您的特定课程的活动投入到公共方法中。 如果您的主类称为MainActivity,并且您有一些公共方法Method,则可以使用fragment方法执行以下操作: ((MainActivity) getActivity()).Method(); 或者,您可以使用片段文档中描述的事件回调模式 。 You should be able to cast the activity returned to your specific class to access the public methods ...
-
通常,您无法从静态类型调用非静态方法,因此您可以这样做: MainActivity m = new MainActivity(); // No constructor needed in class def. m.setDateText(); 但是,当程序启动时,你不会在开始时给你的JVM任何东西调用,所以你需要添加: @Override //the function called when activity is created public void onCreate(Bundle savedI ...
-
加载类后 , static块不会执行。 它在类被加载时执行。 在静态块退出之前,该类没有完全构建。 --------编辑---------- @sky,试着运行这个程序,看看会发生什么: class Foo { static String name = "Michael"; static { Thread t = new Thread(new Bar()); t.start(); try { Thread.sleep(3 ...
-
同时startremdup()和RemoveDuplicate()静态。 private static void RemoveDuplicate(string sourceFilePath, string destinationFilePath) { var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default); File.WriteAllLines(destinationFilePath, readLines.Di ...
-
Client是构造函数。 你通过new调用它: Client c = new Client(); 你可以在main内部做到这一点。 如果它是实例方法,则需要一个实例来调用它。 但是因为它是一个构造函数,所以你用它来创建一个实例(然后你可以调用实例方法)。 Client is a constructor. You call it via new: Client c = new Client(); You can do that inside main. If it were an instance met ...
-
方法的位置不会改变方法的使用方式。 如果方法具有static修饰符,则不能在不使用类的构造函数作为指针的情况下将非静态方法调用到该方法中。 此外,因为您在另一个答案中提到,除非该变量也是静态的,否则无法将变量调用为静态方法,但同样可以使用构造函数指向该变量。 class MyClass { String s = "";//can't be used in a static method static String t = "";//can be used in a static method ...
-
在Main中调用静态方法(Calling a Static Method In Main)[2022-01-05]
你可以这样称呼它: public class Main { public static void main(String[] args) { Rational rational1 = new Rational(1,2); Rational rational2 = new Rational(1,2); Rational result = Rational.multiply(rational1, rational2); } } You can c ...