I have read others questions similar to mine, but I miss something as none of my rows are deleted... After a lot of tries I have ended to the following code..
I want to delete all the rows except the 1st line, that's why the counter starts from 1...
//...
FileInputStream file = new FileInputStream(new File("E:\\products.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
for(int i=1; i<= sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
deleteRow(sheet, row);
}
file.close();
FileOutputStream outFile = new FileOutputStream(new File("E:\\products.xls"));
workbook.write(outFile);
outFile.close();
//...
//The delete method...
private void deleteRow(HSSFSheet sheet, Row row) {
int lastRowNum = sheet.getLastRowNum();
int rowIndex = row.getRowNum();
if(rowIndex >= 0 && rowIndex < lastRowNum){
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if(rowIndex == lastRowNum){
Row removingRow = sheet.getRow(rowIndex);
if(removingRow != null){
sheet.removeRow(removingRow);
System.out.println("Deleting.... ");
}
}
}
I don't get any error, but none row is deleted.
UPDATE
As you mentioned below, I wasn't saving the file... I updated the code above where now I save it! However, some of my rows are deleted and not all of them...
Any ideas why is this happening?
Yuor code works perfectly , only one thing missing : write the result to file ...
wb.write(new FileOutputStream(new File("E:\\products.xls")));
Complete working example:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
public class Main {
public Main() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException {
FileInputStream file = null;
HSSFWorkbook wb = null;
FileOutputStream out = null;
try{
file = new FileInputStream(new File("E:\\products.xls"));
wb = new HSSFWorkbook(file);
HSSFSheet sheet = wb.getSheetAt(0);
for(int i=1; i<= sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
deleteRow(sheet, row);
}
out = new FileOutputStream(new File("E:\\products.xls"));
wb.write(out);
}
catch(Exception e){}
finally{
if(file!=null)
file.close();
if(out!=null)
out.close();
if(wb!=null)
wb.close();
}
}
public static void deleteRow(HSSFSheet sheet, Row row) {
int lastRowNum = sheet.getLastRowNum();
int rowIndex = row.getRowNum();
if(rowIndex >= 0 && rowIndex < lastRowNum){
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if(rowIndex == lastRowNum){
Row removingRow = sheet.getRow(rowIndex);
if(removingRow != null){
sheet.removeRow(removingRow);
System.out.println("Deleting.... ");
}
}
}
}
The only problem in your code its that the beavhiour its not what you expect from the method deleteRow(..) i modified your method in this way :
public static void deleteRow(HSSFSheet sheet, Row row) {
int lastRowNum = sheet.getLastRowNum();
if(lastRowNum !=0 && lastRowNum >0){
int rowIndex = row.getRowNum();
Row removingRow = sheet.getRow(rowIndex);
if(removingRow != null){
sheet.removeRow(removingRow);
System.out.println("Deleting.... ");
}
}
}
If there is only one row in the file , you will not delete , if there are more , you delete all the rows except the first one .
You forgot to save your workbook apparently...
workbook.Save(path)
I think you forgot save changes.
Related
I'm working on POI application in order to manipulate excel file.
In fact the user is giving a formula and files and I am applying the formula on a output file.The formula have to modify the value of the column on the cell.
For example on the columns B, I want to apply on all my column a formula.
The user is giving to me LEFT(x,2), and I have to apply this to all the columns.
(x is defining all the columns)
But when I am applying the formula I got the formula as a String. I try to pass the cell value at the formula but of course it is not working...
I think I should copy all my data into a another excel file, work on it and copy paste them in the output file or their is another way ?
Regards,
Code:
for (int i = 0; i < cell[0].length; i++){ //Checking the header
for (int j = 0; j < ruleArray.length; j++){ //Checking the Header of the array with the rule to apply
if (cell[0][i].toString().equals(ruleArray[j][0])){ //Comparing
String testF = ruleArray[j][1];
if (testF.contains("X") || testF.contains("x")){ //Replacing X with value for the formula
for (int k = 0; k < cell.length; k++){
indexT = cell[0][i].getColumnIndex();
indexC = cell[k][i].getRowIndex()+1;
String colLetter = CellReference.convertNumToColString(indexT);
formula = testF.replace("x", colLetter+indexC);
cell[k][i].setCellType(CellType.FORMULA);
cell[k][i].setCellFormula(formula);
}
}
}
}
}
I am not rewrite your code but you can take a help from this. Create a excel file with column City and Formula and then run this code. I have attached some snapshot of excel file. I thin kit will help you. LEFT(X,2) only parse first two char from a string
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class TestProblem
{
public static void main(String[] args) throws IOException {
InputStream inp = null;
inp = new FileInputStream("E:\\Projects\\PoiAdvanceExample\\stackProblem.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
int rowsCount = sheet.getLastRowNum();
int columnCount = sheet.getRow(0).getLastCellNum();
String[][] inputData = new String[rowsCount+1][columnCount];
for (int i = 0; i <= rowsCount; i++) {
Row row = sheet.getRow(i);
int colCounts = row.getLastCellNum();
for (int j = 0; j < colCounts; j++) {
Cell cell = row.getCell(j);
if(cell.getCellType() == CellType.NUMERIC) {
inputData[i][j] = Double.toString(cell.getNumericCellValue());
}
if(cell.getCellType() == CellType.FORMULA) {
inputData[i][j] = cell.getCellFormula();
}
if(cell.getCellType() == CellType.STRING) {
inputData[i][j] = cell.getStringCellValue();
}
}
}
writeData(inputData);
}
private static void writeData(String[][] inputData) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
int r = 0;
for (String[] dataRow : inputData) {
Row row = sheet.createRow(r++);
int column = 0;
for (String dataCell : dataRow) {
Cell cell = row.createCell(column++);
if (r == 1 || column == 1) cell.setCellValue(dataCell);
else if (column == 2) {
CellReference cellReference = new CellReference(cell);
String thisR = cellReference.getCellRefParts()[1];
cell.setCellFormula("LEFT(A" + thisR + ",2)");
}
}
}
FileOutputStream fileOut = new FileOutputStream("stackProblem.xlsx");
workbook.write(fileOut);
workbook.close();
}
}
Excel file before run will be like this.
Excel file after run this code will be like this.
I'm using the Apache POi HSSF library to import and export an Excel table to my application (tableview). I want to delete from Java a Row in Excel with a specific ID. It works. My Problem is, after i delete a row there are a empty row and it delete more than the selected row. Could everybody help?
FileInputStream inp = new FileInputStream(
"...............";
HSSFWorkbook wb = (HSSFWorkbook) WorkbookFactory.create(inp);
HSSFSheet sheet = wb.getSheetAt(0);
String selectedid = auftragTabelle.getSelectionModel().getSelectedItem().getId();
int rowIndex = 0;
int lastRowNum = sheet.getLastRowNum();
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(selectedid)) {
rowIndex = cell.getRowIndex();
}
}
}
}
if (rowIndex >= 0 && rowIndex < lastRowNum) {
sheet.shiftRows(rowIndex, lastRowNum, -1);
}
if (rowIndex <= lastRowNum) {
HSSFRow removingRow = sheet.getRow(rowIndex);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
OutputStream out = new FileOutputStream(
"............";
wb.write(out);
out.close();
}
Here's an example of how I would do it:
public static void main(String[] args) throws InvalidFormatException, IOException {
FileInputStream inp = new FileInputStream(FILENAME);
HSSFWorkbook wb = (HSSFWorkbook) WorkbookFactory.create(inp);
HSSFSheet sheet = wb.getSheetAt(0);
String selectedid = "3";
int rowIndex = getRowIndexOfId(sheet, selectedid);
removeRow(sheet, rowIndex);
OutputStream out = new FileOutputStream(FILENAME);
wb.write(out);
out.close();
}
private static int getRowIndexOfId(HSSFSheet sheet, String selectedid) {
DataFormatter formatter = new DataFormatter();
for (Row row : sheet) {
for (Cell cell : row) {
if (formatter.formatCellValue(cell).trim().equals(selectedid)) {
return row.getRowNum();
}
}
}
return -1;
}
private static void removeRow(HSSFSheet sheet, int rowIndex) {
if (rowIndex >= 0) {
sheet.removeRow(sheet.getRow(rowIndex));
if(rowIndex < sheet.getLastRowNum()) {
sheet.shiftRows(rowIndex + 1, sheet.getLastRowNum(), -1);
}
}
}
A few comments:
You can use the class DataFormatter to format the cell values (so you can compare any cell value)
I - like you - compare any cell in a cell; the typical use case for me would be to search the first col of each row to find the id. SO if you want that, adjust the code accordingly.
I did the shift row in the end, which should work better.
I have tried various ways of removing the index out of bound error by changing the upper limits of the array.But the error persists. Where am I going wrong?
Screenshot of my excel sheet
My program reads values(all rows) in the first column of excel sheet and finds the maximum value. Then based on the maximum value,criteria are formulated and the values are classified as Low,Medium,High and written back into a new excel sheet.
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.*;
import java.util.*;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.Label;
import jxl.write.WriteException;
public class Bus3{
List<String> numbusarray = new ArrayList<String>();
List<String> numcommutersarray = new ArrayList<String>();
List<String> numcommercialarray = new ArrayList<String>();
static WritableWorkbook workbook;
static WritableSheet wSheet;
public void readExcel() throws BiffException, IOException, WriteException//method to read contents form excel
{
String FilePath = "Bus1.xls";
Scanner sc = new Scanner(System.in);
int max=0;
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh = wb.getSheet("Bus1");// TO get the access to the sheet
int totalNoOfRows = sh.getRows();// To get the number of rows present in sheet
int totalNoOfCols = sh.getColumns();// To get the number of columns present in sheet
System.out.println(totalNoOfRows);
//adding excel contents from every column to arraylist
for (int row = 1; row <totalNoOfRows; row++)
{
numbusarray.add(sh.getCell(2, row).getContents());
}
for (int row = 1; row <totalNoOfRows; row++)
{
numcommutersarray.add(sh.getCell(3, row).getContents());
}
for (int row = 1; row <totalNoOfRows; row++)
{
numcommercialarray.add(sh.getCell(4, row).getContents());
}
//to find maximum of numbusarray
max=Integer.parseInt(numbusarray.get(0));
for (int row = 1; row < totalNoOfRows-1; row++)
{
if(!(numbusarray.get(row)).isEmpty())
{
int intNumber=Integer.parseInt(numbusarray.get(row));
if(intNumber>max)
{
max=intNumber;
//System.out.println(max);
}
}
}
System.out.println(max);
WritableWorkbook workbook = Workbook.createWorkbook(new File("sampletestfile.xls"));
WritableSheet wSheet = workbook.getSheet(0);
int increment=max/3;
int a=increment;
int b=a+increment;
int c=b+increment;
for (int row = 0; row < totalNoOfRows-1; row++)
{
if(!(numbusarray.get(row)).isEmpty())
{
int compare=Integer.parseInt(numbusarray.get(row));
if(compare<=a)
{Label label= new Label(0, row, "Low");//column,row,strngdata
wSheet.addCell(label);}
else if((compare>a)&&(compare<=b))
{Label label= new Label(0, row, "Medium");//column,row,strngdata
wSheet.addCell(label);}
else
{Label label= new Label(0, row, "High");//column,row,strngdata
wSheet.addCell(label);}
}
}
/*Iterator itr=numbusarray.iterator(); //to print arraylist demo
while(itr.hasNext()){
System.out.println(itr.next());
}*/
}//end of method to read contents from excel
//to close file
public static void closeFile()
{
try {
// Closing the writable work book
workbook.write();
workbook.close();
// Closing the original work book
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[]) throws BiffException, IOException, WriteException //main class
{
Bus3 DT = new Bus3();
DT.readExcel();
Bus3.closeFile();
}//end of main class
}
It is because your sh Sheet.class object doesn't have cells with column = 4.
This should fix it:
for (int row = 1; row < totalNoOfRows; row++) {
numbusarray.add(sh.getCell(1, row).getContents());
}
for (int row = 1; row < totalNoOfRows; row++) {
numcommutersarray.add(sh.getCell(2, row).getContents());
}
for (int row = 1; row < totalNoOfRows; row++) {
numcommercialarray.add(sh.getCell(3, row).getContents());
}
LAST EDIT:
for (int row = 1; row < totalNoOfRows; row++) {
numbusarray.add(sh.getCell(1, row).getContents());
}
for (int row = 1; row < totalNoOfRows; row++) {
numcommutersarray.add(sh.getCell(2, row).getContents());
}
for (int row = 1; row < totalNoOfRows; row++) {
numcommercialarray.add(sh.getCell(3, row).getContents());
}
// to find maximum of numbusarray
max = 0;
for (int row = 1; row < totalNoOfRows; row++) {
if (!(numbusarray.get(row - 1)).isEmpty()) {
int intNumber = Integer.parseInt(numbusarray.get(row - 1));
if (intNumber > max) {
max = intNumber;
System.out.println("max: " + max);
}
}
}
System.out.println(max);
workbook = Workbook.createWorkbook(new File("sampletestfile.xls"));
WritableSheet wSheet = workbook.createSheet("name", 0);
It does not look like a very complex problem.
Index out of bounds means that you are trying to access a position in the array that does not exists.
Watch your numbusarray variable, probably row is being set to an invalid index.
Good afternoon, for me what worked was to create a new xls file, and copy the data from the old to the new one. and the errors stopped.
I have data in the above format in an excel file , I want to edit it as follows:
I have used the following code :
public void editExcelTemplate() throws FileNotFoundException, IOException
{
InputStream ExcelFileToRead = new FileInputStream("file.xls");
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
String cmp = "none";
for(int i=0;i<rows;i++)
{
Row row = sheet.getRow(i);
int col =row.getPhysicalNumberOfCells();
int colIndex = 1;
int v=0;
for(int j=0;j<col;j++)
{
String content = row.getCell(j).getStringCellValue();
if(!(content == cmp) && !(content.equals("none")))
{
if(!(cmp.equals("none")))
{
System.out.println("content: "+content);
System.out.println("cmp: "+cmp);
v= j;
System.out.println("row : "+i+"colst : "+(colIndex)+"colend : "+v);
if(!( v-colIndex == 0) && v>0)
{
System.out.println("row : "+i+"colst : "+(colIndex)+"colend : "+v);
sheet.addMergedRegion(new CellRangeAddress(i,i,colIndex-1,v-1));
System.out.println("merged");
}
}
}
if(!(content == cmp))
{
colIndex = v+1;
}
cmp = content;
}
}
FileOutputStream excelOutputStream = new FileOutputStream(
"file.xls");
wb.write(excelOutputStream);
excelOutputStream.close();
}
I endedup getting the following output :
Can anybody help me get an appropriate output ? The main purpose is to merge the cells with common data in the entire proces.
I'm writing a program that does the following tasks:
Cleans up Excel files (i.e. remove rows based on a condition).
Add data from notepad to a new Excel (say Result Excel).
Add the data from Cleaned Excel files to the Result Excel.
Below is my code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TextToExcelMultiple {
static int counter = 1;
public static void main(String[] args) throws Exception {
File mainFolder = new File("C:\\D\\Mypath\\New");
File[] files;
int rowCount = 0;
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Result");
CellStyle style = workbook.createCellStyle();// Create style
Font font = workbook.createFont();// Create font
font.setBoldweight(Font.BOLDWEIGHT_BOLD);// Make font bold
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(font);// set it to bold
Row row;
Cell cell;
row = sheet.createRow(rowCount);
cell = row.createCell(0);
cell.setCellValue("NAME");
cell = row.createCell(1);
cell.setCellValue("TITLE");
cell = row.createCell(2);
cell.setCellValue("FIRM");
cell = row.createCell(3);
cell.setCellValue("ADDRESS 1");
cell = row.createCell(4);
cell.setCellValue("ADDRESS 2");
cell = row.createCell(5);
cell.setCellValue("ADDRESS 3");
cell = row.createCell(6);
cell.setCellValue("CITY");
cell = row.createCell(7);
cell.setCellValue("STATE");
cell = row.createCell(8);
cell.setCellValue("POSTAL CODE");
cell = row.createCell(9);
cell.setCellValue("COUNTRY");
cell = row.createCell(10);
cell.setCellValue("PHONE");
cell = row.createCell(11);
cell.setCellValue("FAX");
cell = row.createCell(12);
cell.setCellValue("URL");
cell = row.createCell(13);
cell.setCellValue("ISN");
for (int i = 0; i < row.getLastCellNum(); i++) {
row.getCell(i).setCellStyle(style);
}
rowCount = 1;
if (mainFolder.isDirectory()) {
files = mainFolder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
files = file.listFiles();
for (File finalFile : files) {
String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
if ((ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
cleanUpFile(finalFile);
}
}
for (File finalFile : files) {
String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
if ((ext.equals("txt") && finalFile.getName().contains("brief"))
|| (ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
int rowNumber = writeData(finalFile, sheet, rowCount, workbook);
rowCount = rowNumber;
}
}
}
}
}
FileOutputStream outputStream = new FileOutputStream("C:\\D\\Mihir\\new.xls");
workbook.write(outputStream);
outputStream.close();
}
private static int writeData(File file, XSSFSheet sheet, int rowCount, XSSFWorkbook workbook) throws Exception {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
String x;
Row row;
Cell cell;
String ext = FilenameUtils.getExtension(file.getAbsolutePath() + " \t " + file.getName());
if (!ext.contains("xls")) {
while ((x = br.readLine()) != null) {
if (!(x.contains("NAME"))) {
row = sheet.createRow(rowCount);
String[] namesList = x.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1);
int columnCount = 0;
for (String name : namesList) {
cell = row.createCell(columnCount);
cell.setCellValue(name.replace("\"", ""));
columnCount += 1;
}
rowCount += 1;
}
}
}
if (ext.contains("xls")) {
int result = appendDataToExcel(file, workbook, rowCount, counter);
counter = result;
}
br.close();
fileInputStream.close();
return rowCount;
}
private static void cleanUpFile(File file) throws Exception {
System.out.println(file.getAbsolutePath());
FileInputStream fin = new FileInputStream(new File(file.getAbsolutePath()));
Workbook wb = new XSSFWorkbook(fin);
Sheet sheet = wb.getSheetAt(0);
Cell cell;
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
cell = sheet.getRow(i).getCell(1);
if (cell == null) {
sheet.removeRow(sheet.getRow(i));
int rowIndex = i;
int lastRowNum = sheet.getLastRowNum();
if (rowIndex >= 0 && rowIndex <= lastRowNum) {
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
}
}
fin.close();
FileOutputStream outFile = new FileOutputStream(new File(file.getAbsolutePath()));
wb.write(outFile);
wb.close();
outFile.close();
}
private static int appendDataToExcel(File file, XSSFWorkbook workbook, int rowCount, int counter) throws Exception {
String path = file.getAbsolutePath();
FileInputStream fin = new FileInputStream(new File(path));
XSSFWorkbook wb = new XSSFWorkbook(fin);
XSSFSheet sheet1 = wb.getSheetAt(0);
int noOfRows = sheet1.getPhysicalNumberOfRows();
XSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
XSSFRow row;
for (int i = 1; i < noOfRows; i++) {
row = sheet.getRow(counter);
cell = row.getCell(13);
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
cell = row.createCell(13);
}
Cell cell1 = sheet1.getRow(i).getCell(1);
if (cell1 != null && !cell1.equals("")) {
cell.setCellValue(sheet1.getRow(i).getCell(1).toString() + "\t" + counter);
}
counter++;
}
wb.close();
fin.close();
counter = rowCount;
return (counter);
}
}
Previously I used to call cleanUpFile(file) method, And this use to give me the below exception.
Later on I tried to first clean files and then run the program on the cleaned files But still I get the same exception:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.io.ByteArrayOutputStream.grow(Unknown Source)
at java.io.ByteArrayOutputStream.ensureCapacity(Unknown Source)
at java.io.ByteArrayOutputStream.write(Unknown Source)
at org.apache.poi.openxml4j.opc.internal.MemoryPackagePartOutputStream.write(MemoryPackagePartOutputStream.java:88)
at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:590)
at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2544)
at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:223)
at org.apache.poi.xssf.usermodel.XSSFSheet.write(XSSFSheet.java:2972)
at org.apache.poi.xssf.usermodel.XSSFSheet.commit(XSSFSheet.java:2927)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:323)
at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:327)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:195)
at TextToExcelMultiple.main(TextToExcelMultiple.java:100)
And when I click on at TextToExcelMultiple.main(TextToExcelMultiple.java:100) it points out to workbook.write(outputStream);
I've got really big set of files.
One more thing: when I comment out the below block and run it, it works fine:
for (File finalFile : files) {
String ext = FilenameUtils.getExtension(finalFile.getAbsolutePath());
if ((ext.equals("txt") && finalFile.getName().contains("brief"))
|| (ext.contains("xls") && finalFile.getName().toLowerCase().contains("detail"))) {
int rowNumber = writeData(finalFile, sheet, rowCount, workbook);
rowCount = rowNumber;
}
}
Going through some other SO posts, I've added the below in my arguments:
-Xms1100m -Xmx1100m
When I try to increase heap memory -Xms1150m -Xmx2048m it gives me below error:
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap
Since my free memory available is around 1150MB:
Also if I deploy this as a swing app in my client's system, what all should be taken care before doing so?
How can I run this full program without facing the exception?