Exporting Jtable into the Excelsheet using HSSFWorkbook - java

Hello I am trying to Export Jtable Data into the Excel Sheet using HSSFWorkbook. and i am getting all the content what Table have but i am not getting Table Headers please can anyone help for the same.
Here the Command used for Taking content of the Jtable.
try {
HSSFWorkbook fWorkbook = new HSSFWorkbook();
HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
HSSFFont sheetTitleFont = fWorkbook.createFont();
File file = new File("/home/kishan/NetBeansProjects/JavaChecking/src/com/verve/SwingChecking/book.xls");
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//sheetTitleFont.setColor();
TableModel model = jTable1.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i);
for (int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
cell.setCellStyle(cellStyle);
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
fWorkbook.write(bos);
bos.close();
fileOutputStream.close();
}catch(Exception e){
}
for (int i = 0; i < model.getColumnCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i);
for(int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
System.out.println(model.getColumnName(j));
}
}
last for loop is not addind data of table header.
and i am getting this excel file
how to get Table header along with that??

Here's my implementation of the HSSF Workbook from the answers in this thread.
I created a class ExcelWriter then a Method Writer which takes two parameters; the JTable and the FileLocation to be used.
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author oluwajayi
*/
public class ExcelWriter {
public static void Writer (JTable jTable1, String Location) throws FileNotFoundException, IOException {
HSSFWorkbook fWorkbook = new HSSFWorkbook();
HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
HSSFFont sheetTitleFont = fWorkbook.createFont();
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//sheetTitleFont.setColor();
TableModel model = jTable1.getModel();
//Get Header
TableColumnModel tcm = jTable1.getColumnModel();
HSSFRow hRow = fSheet.createRow((short) 0);
for(int j = 0; j < tcm.getColumnCount(); j++) {
HSSFCell cell = hRow.createCell((short) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
cell.setCellStyle(cellStyle);
}
//Get Other details
for (int i = 0; i < model.getRowCount(); i++) {
HSSFRow fRow = fSheet.createRow((short) i+1);
for (int j = 0; j < model.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(model.getValueAt(i, j).toString());
cell.setCellStyle(cellStyle);
}
}
FileOutputStream fileOutputStream;
fileOutputStream = new FileOutputStream(Location);
try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) {
fWorkbook.write(bos);
}
fileOutputStream.close();
}
}

Something like this to add Column Names in the first row of your sheet:
TableColumnModel tcm = jTable1.getColumnModel();
HSSFRow fRow = fSheet.createRow((short) 0);
for(int j = 0; j < tcm.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
}
You could run this first and then add the table data starting from the second row.

you are only writing the data in the TableModel to the workbook. This model does not contain the table header. Take a look at JTable.getTableHeader()
for example:
public class JTableExport {
public static void main(String[] args) {
Object[] columnNames = new Object[] {"column1", "column2"};
JTable table = new JTable(new Object[0][0], columnNames);
TableColumnModel model = table.getTableHeader().getColumnModel();
for (int i = 0; i < model.getColumnCount(); i++) {
System.out.println(model.getColumn(i).getHeaderValue());
}
}
}
this code prints
column1
column2

for(int j = 0; j < tcm.getColumnCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
}
for(int j = 0; j < tcm.getRowCount(); j++) {
HSSFCell cell = fRow.createCell((short) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
}

I created this code:
public void Export() {
JFileChooser save = new JFileChooser();
save.setDialogTitle("Save as...");
save.setFileFilter(new FileNameExtensionFilter("xls", "xlsx", "xlsm"));
int choose = save.showSaveDialog(null);
if(choose == JFileChooser.APPROVE_OPTION) {
XSSFWorkbook export = new XSSFWorkbook();
XSSFSheet sheet1 = export.createSheet("new file");
try{
TableModel tableModel = showQuery.getModel();
for(int i=0; i<tableModel.getRowCount(); i++) {
XSSFRow newRow = sheet1.createRow(i);
for(int j=0; j<tableModel.getColumnCount(); j++) {
XSSFCell newCell = newRow.createCell((short) j);
if(i==0){
XSSFCellStyle style = export.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
newCell.setCellStyle(style);
newCell.setCellValue(tableModel.getColumnName(j));
} else {
XSSFCellStyle style = export.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
newCell.setCellStyle(style);
newCell.setCellValue(tableModel.getValueAt(i, j).toString());
}
}
}
FileOutputStream otp = new FileOutputStream(save.getSelectedFile()+".xlsx");
BufferedOutputStream bos = new BufferedOutputStream(otp);
export.write(bos);
bos.close();
otp.close();
JOptionPane.showMessageDialog(null, "Arquivo exprtado com sucesso!");
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}

Related

Excel formula on the current value/cell POI

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.

How to remove the index out of bound error in my program?

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.

Apache POI Update Excel File After Adding Columns/Rows to JTable

New here so please be kind. I've spent a few hours trawling through posts trying to find an answer but no such luck.
I have an excel file that I import into a JTable and can then edit. Add columns, rows etc.
When I click the button to add a column or row they appear on the JTable as expected. However when I click the save button to save the file it hasn't added the new columns or rows to the Excel file.
I thought my save wasn't working correctly, but if I edit one of the two columns I already have saved in the Excel file the changes are saved to the Excel file so I am confused as to why I can edit the existing data in the file but any new rows or columns are not saved.
My export to excel is:
public void toExcel()
{
try
{
FileInputStream fsIP = new FileInputStream(new File("Recipes.xls"));
HSSFWorkbook fWorkbook = new HSSFWorkbook(fsIP);
HSSFSheet fSheet = fWorkbook.getSheet("RECIPES");
HSSFFont sheetTitleFont = fWorkbook.createFont();
HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
TableColumnModel tcm = tables.getColumnModel();
HSSFRow fRow = fSheet.createRow((short) 0);
for(int j = 0; j < tcm.getColumnCount(); j++)
{
HSSFCell cell = fRow.createCell((int) j);
cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
}
for (int i = 0, row = 1; i < models.getRowCount(); i++, row++)
{
HSSFRow fRow1 = fSheet.createRow((short) row);
for (int j = 0; j < models.getColumnCount(); j++)
{
HSSFCell cell = fRow1.createCell((int) j);
cell.setCellValue(models.getValueAt(i, j).toString());
cell.setCellStyle(cellStyle);
}
}
FileOutputStream output_file = new FileOutputStream(new File("Recipes.xls"));
BufferedOutputStream bos = new BufferedOutputStream(output_file);
fWorkbook.write(output_file);
fWorkbook.close();
bos.close();
output_file.close();
}
catch(Exception e)
{
}
JOptionPane.showMessageDialog(
null, "Changes Saved",
"Confirmation",
JOptionPane.INFORMATION_MESSAGE);
}
My method for adding a column is as follows:
public void addNewRecipe()
{
models.addColumn(JOptionPane.showInputDialog("Enter A Description for Recipe"));
}
Thanks.
EDIT
Here is code for calling toExcel
saveButton.addActionListener(new ActionListener()
{
#Override public void actionPerformed(ActionEvent arg0)
{
{
models = new DefaultTableModel(datas, headers);
tableWidth = models.getColumnCount() * 150;
tableHeight = models.getRowCount() * 50;
tables.setPreferredSize(new Dimension( tableWidth, tableHeight));
tables.setModel(models);
tables.setRowHeight(50);
toExcel();
}
}
});

Java Memory heap error using text and excel files

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?

Remove all borders on a specific excel worksheet using Apache POI

I am using Apache POI to generate an Excel file. I need to delete all of the borders in my worksheet. How can I accomplish this using Apache PIO 3.11 and Microsoft Excel 2007?
Here is the code I have so far:
package models;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.FileOutputStream;
import java.util.List;
public class Excel {
public static void writeDocument() {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet");
//first font
Font font1 = workbook.createFont();
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
//first style
CellStyle style1 = workbook.createCellStyle();
style1.setBorderLeft(CellStyle.BORDER_NONE);
style1.setBorderRight(CellStyle.BORDER_NONE);
style1.setBorderBottom(CellStyle.BORDER_NONE);
style1.setBorderTop(CellStyle.BORDER_NONE);
//second style
CellStyle style2 = workbook.createCellStyle();
style2.setFont(font1);
style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style2.setAlignment(CellStyle.ALIGN_CENTER);
style2.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(CellStyle.BORDER_THIN);
style2.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
style2.setBorderLeft(CellStyle.BORDER_THIN);
style2.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
style2.setBorderRight(CellStyle.BORDER_THIN);
style2.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
style2.setBorderTop(IndexedColors.GREY_25_PERCENT.getIndex());
for(int i=0; i< 100 ; i++){
for(int j=0; j< 100; j++){
Cell cell = sheet.createRow(i).createCell(j);
cell.setCellStyle(style1);
}
}
CellRangeAddress region = new CellRangeAddress(0, 10, 0, 10);
cleanBeforeMergeOnValidCells(sheet, region, style2);
sheet.addMergedRegion(region);
try {
FileOutputStream output = new FileOutputStream("tmp/rapport.xls");
workbook.write(output);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void cleanBeforeMergeOnValidCells(Sheet sheet, CellRangeAddress region, CellStyle cellStyle) {
for (int rowNum = region.getFirstRow(); rowNum <= region.getLastRow(); rowNum++) {
Row row = sheet.getRow(rowNum);
if (row == null) {
sheet.createRow(rowNum);
}
for (int colNum = region.getFirstColumn(); colNum <= region.getLastColumn(); colNum++) {
Cell currentCell = row.getCell(colNum);
if (currentCell == null) {
currentCell = row.createCell(colNum);
}
currentCell.setCellStyle(cellStyle);
}
}
}
}
I think you mean that you don't need the gridlines, in that case use the setDisplayGridlines method like:
sheet.setDisplayGridlines(false);

Categories