upload large .xlsx file in gui java - java

i have a large excel file containing 600.000 rows , i used XSSFWorkbook to upload the excel file at a Jtable in my GUI but it takes about 15 minutes to be done in eclipse and once i export my project to a jar file i can't do it even in the 15 minutes . Any help please ?
Here is the method , that i found in internet to upload my excel file .
void fillData(File file) {
int index = -1;
XSSFWorkbook workbook = null;
try {
try {
String f = file.getPath();
File file1 = new File(f);
OPCPackage opcPackage = OPCPackage.open(file1);
workbook = new XSSFWorkbook(opcPackage);
} catch (IOException ex) {
Logger.getLogger(ProjectApp3.class.getName()).log(Level.SEVERE, null, ex);
}
String[] strs = new String[workbook.getNumberOfSheets()];
//get all sheet names from selected workbook
for (int i = 0; i < strs.length; i++) {
strs[i] = workbook.getSheetName(i);
}
JFrame frame = new JFrame("Input Dialog");
//select sheet
String selectedsheet = (String) JOptionPane.showInputDialog(
frame, "Which worksheet you want to import ?", "Select Worksheet",
JOptionPane.QUESTION_MESSAGE, null, strs, strs[0]);
if (selectedsheet != null) {
for (int i = 0; i < strs.length; i++) {
if (workbook.getSheetName(i).equalsIgnoreCase(selectedsheet))
index = i;
}
XSSFSheet sheet = workbook.getSheetAt(index);
XSSFRow row = sheet.getRow(0);
//import headers data
headers.clear();
for (int i = 0; i < row.getLastCellNum(); i++) {
XSSFCell cell1 = row.getCell(i);
headers.add(cell1.toString());
}
//import data
data1.clear();
for (int j = 1; j < sheet.getLastRowNum() + 1; j++) {
Vector d = new Vector();
row = sheet.getRow(j);
int noofrows = row.getLastCellNum();
for (int i = 0; i < noofrows; i++) { //To handle empty excel cells
XSSFCell cell = row.getCell(i,
org.apache.poi.ss.usermodel.Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
d.add(cell.toString());
}
d.add("\n");
data1.add(d);
}
} else {
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}

I think the basic problem is that you're trying to give. your Jtable all the data at startup. This is going to be deeply problematic. You may want to write a custom subclass from AbstractTableModel. See the docs for Jtable that includes this:
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(table);
You can implement those three methods based on the info that POI gives you. But do lazy loading of the data, most especially for getValueAt(). Keep the spreadsheet file open and grab the data only when the user scrolls to view it.

Related

Exporting JTable Data including Table Headers to Excel File

I need help regarding this, I can't get it right. I want to export JTable data to excel file. There is no error but I am getting only the data it shows and does not include the table headers.
I am running this in a Netbeans 8.2 IDE and had also imported the essential jar files.
Data on my jtable is provided by my mysql database and I need to export it from jtable to an excel file.
Anyway tblData is my JTable variable name.
private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {
FileOutputStream excelFOS = null;
BufferedOutputStream excelBOS = null;
XSSFWorkbook wb = null;
JFileChooser excelFileChooser = new JFileChooser();
excelFileChooser.setDialogTitle("Save As");
FileNameExtensionFilter fnef = new FileNameExtensionFilter("Excel Files","xls","xlsx","ods");
excelFileChooser.setFileFilter(fnef);
int excelChooser = excelFileChooser.showSaveDialog(null);
if(excelChooser == JFileChooser.APPROVE_OPTION){
try {
wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Data Sheet");
for(int i = 0; i < tblData.getRowCount(); i++){
XSSFRow excelRow = sheet.createRow(i);
for(int j = 0; j < tblData.getColumnCount(); j++){
XSSFCell excelCell = excelRow.createCell(j);
excelCell.setCellValue(tblData.getValueAt(i, j).toString());
}
}
excelFOS = new FileOutputStream(excelFileChooser.getSelectedFile() + ".xlsx");
excelBOS = new BufferedOutputStream(excelFOS);
wb.write(excelBOS);
JOptionPane.showMessageDialog(null, "Successfully saved.");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if(excelBOS != null){
excelBOS.close();
}
if(excelFOS != null){
excelFOS.close();
}
if(wb != null){
wb.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
} //---- end finally
} //---- end if condition
}
I am expecting for the header to be exported. Can anyone help me with this.
Not really clear what tblData is in your provided code. But If I would have the requirement to export a JTable to Excel, then I would go by TableModel of the JTable. First write the column names to first row of the Excel sheet using TableModel.getColumnName. Then write the table data to next rows of the Excelsheet using TableModel.getValueAt.
Complete example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import javax.swing.JTable;
import javax.swing.table.TableModel;
class WriteJTableToExcel {
static void exportToExcel(JTable table, String filePath) throws Exception {
TableModel model = table.getModel();
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
Row row;
Cell cell;
// write the column headers
row = sheet.createRow(0);
for (int c = 0; c < model.getColumnCount(); c++) {
cell = row.createCell(c);
cell.setCellValue(model.getColumnName(c));
}
// write the data rows
for (int r = 0; r < model.getRowCount(); r++) {
row = sheet.createRow(r+1);
for (int c = 0; c < model.getColumnCount(); c++) {
cell = row.createCell(c);
Object value = model.getValueAt(r, c);
if (value instanceof String) {
cell.setCellValue((String)value);
} else if (value instanceof Double) {
cell.setCellValue((Double)value);
}
}
}
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
workbook.close();
}
public static void main(String[] args) throws Exception {
Object columnNames[] = {"Name", "Amount", "Factor"};
Object rowData[][] = {
{"Bob", 12.0, 3.0},
{"Alice", 34.0, 2.5},
{"Jack", 56.0, 2.0},
{"John", 78.0, 1.5}
};
JTable table = new JTable(rowData, columnNames);
exportToExcel(table, "./Excel.xlsx");
}
}
you need to export the getTableHeader() from your jTable separately before you export your data. It is not stored together with the data, as it is in an Excel workbook.
Hope that helps :-)

Apache POI - Excel Import, insert data from a new sheet into a column

I'm here to ask for help in my java project in Netbeans.
I'm using Apache POI to import/export excel data. To make you understand what is the problem in my application, I'm showing you a print of the debug.
In the print, you can see 2 sheets. The first header "aiai" and the data from that sheet.
My problem is: How do i insert the data from "aiai2" which is the second sheet from my excel file, in its proper place, below the header "aiai2"
On other words, I want to separate the sheets vertically.
Below, I will show my code:
Workbook wb;
public String Importar(File archivo, JTable tablaD) {
String answer = "Unable to import";
DefaultTableModel modeloT = new DefaultTableModel();
tablaD.setModel(modeloT);
tablaD.getModel();
tablaD.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
try {
wb = WorkbookFactory.create(new FileInputStream(archivo));
int nsheets = wb.getNumberOfSheets();
for (int i = 0; i < nsheets; i++) {
Sheet sheet = wb.getSheetAt(i);
Iterator filaIterator = sheet.rowIterator();
int rownum = -1;
while (filaIterator.hasNext()) {
rownum++;
Row fila = (Row) filaIterator.next();
/*if (i > 0) {//se o nr da ficha atual for maior que 0, começa a escrever as linhas apartir da row 0 da tabela
modeloT.moveRow(modeloT.getRowCount() -1, modeloT.getRowCount() - 1, 0);
}*/
Iterator columnaIterator = fila.cellIterator();
Object[] listaColumna = new Object[1000];
int columnnum = -1;
while (columnaIterator.hasNext()) {
columnnum++;
Cell celda = (Cell) columnaIterator.next();
if (rownum == 0) {
modeloT.addColumn(celda.getStringCellValue());
} else {
if (celda != null) {
switch (celda.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
listaColumna[columnnum] = (int) Math.round(celda.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
listaColumna[columnnum] = celda.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
listaColumna[columnnum] = celda.getBooleanCellValue();
break;
default:
listaColumna[columnnum] = celda.getDateCellValue();
break;
}//end switch case
System.out.println("Column:" + columnnum + " Row:" + rownum + " value:" + celda + ".");
}
}
}//end while column Iterator
if (rownum != 0) {
modeloT.addRow(listaColumna);
}
}//end while row iterator
}//end for
answer = "Imported with success";
} catch (IOException | InvalidFormatException | EncryptedDocumentException e) {
System.err.println(e.getMessage());
}
return answer;
}
public String Exportar(File archivo, JTable tablaD) {
String answer = "Unable to export";
int numFila = tablaD.getRowCount(), numColumna = tablaD.getColumnCount();
if (archivo.getName().endsWith("xls")) {
wb = new HSSFWorkbook();
} else {
wb = new XSSFWorkbook();
}
Sheet hoja = wb.createSheet("Default");
try {
for (int i = -1; i < numFila; i++) {
Row fila = hoja.createRow(i + 1);
for (int j = 0; j < numColumna; j++) {
Cell celda = fila.createCell(j);
if (i == -1) {
celda.setCellValue(String.valueOf(tablaD.getColumnName(j)));
} else {
celda.setCellValue(String.valueOf(tablaD.getValueAt(i, j)));
}
wb.write(new FileOutputStream(archivo));
}
}
answer = "Exported with success";
} catch (Exception e) {
System.err.println(e.getMessage());
}
return answer;
}
As I understand your question, I assume you want to create a separate table for each sheet, one below other. In that case you need to create a new table everytime you read a new sheet. If you use only one table, you will get only one header.
Try this :
Create a new method Importar that takes a new table and a Sheet parameter
public String Importar(JTable tablaD, Sheet sheet) {
String answer = "Unable to import";
DefaultTableModel modeloT = new DefaultTableModel();
tablaD.setModel(modeloT);
tablaD.getModel();
tablaD.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
try {
Iterator filaIterator = sheet.rowIterator();
int rownum = -1;
....
....
So the calling method would be :
try {
Workbook wb = WorkbookFactory.create(new FileInputStream(archivo));
int nsheets = wb.getNumberOfSheets();
for (int i = 0; i < nsheets; i++) {
//You have to make sure your JTable gets rendered.
JTable tablaD = new JTable();
Importar( tablaD, wb.getSheetAt(i) );
}
} catch ( Exception e ) {
e.printStackTrace();
}
Important point is that your new table needs to get rendered or added to frame each time before you call Importar

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();
}
}
});

cannot get a numerical value from the excel cell

public Object[][] dataProviderMethod() throws IOException {
try {
file = new FileInputStream(new File("/Users/nanthakumar/Documents/workspace/Myna_Admin/src/com/myna/testdata/login.xls"));
workbook = new HSSFWorkbook(file);
sheet = workbook.getSheetAt(0);
row = sheet.getLastRowNum() + 1;
col = sheet.getRow(0).getLastCellNum();
data = new String[row][col];
for (i = 0; i < row; i++) {
rowvalue = sheet.getRow(i);
for (j = 0; j < col; j++) {
cellValue = rowvalue.getCell(j);
data[i][j] = cellValue.getStringCellValue();
System.out.println("The value is ----->" + data[i][j]);
}
workbook.close();
file.close();
}
} catch (FileNotFoundException nana) {
nana.printStackTrace();
}
return data;
}
This is my code and I tried to add the getnumericalCellvalue() instead of getStringCellValue() but that is not working for me.
first you have to check in the template excel whether the column is number or text.you can get the numerical value if and only if the column is of type number.so change the template excel and try

FileOutputStream (Apachhe POI) taking too long time to save

When I am edit a .xlsx file using Apache poi, its taking too long to save. The .xlsx file contains, formulas formatting and freeze pane. I am using the following code,
try {
FileInputStream file = new FileInputStream(new File(path));
XSSFWorkbook fWorkbook = new XSSFWorkbook(file);
XSSFSheet fSheet = fWorkbook.getSheetAt(0);
for(int i = 0; i < jTable1.getRowCount(); i++){
if(jTable1.getModel().getValueAt(i, index1).equals("1")){
XSSFCell cell = fSheet.getRow(i+1).getCell(index1);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(new Double(1));
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
cell =fSheet.getRow(i+1).getCell(index2);
cell.setCellValue(new Date());
cell.setCellStyle(cs);
}
}
file.close();
FileOutputStream fileOutputStream = new FileOutputStream(path);
fWorkbook.write(fileOutputStream);
fileOutputStream.close();
JOptionPane.showMessageDialog(this, "Data saved successfully.");
parent.removeContent();
}catch(Exception e){
e.printStackTrace();
}
Edit 1:
The excel file: http://ge.tt/5orGWSJ2/v/0?c
Loading Data into JTable from Excel:
try {
FileInputStream file1 = new FileInputStream(new File("c:\\sample.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file1);
XSSFSheet sheet = workbook.getSheetAt(0);
int rowc=sheet.getLastRowNum()+1;
int colc=sheet.getRow(0).getLastCellNum();
Object heading[] = new Object[colc+1];
XSSFRow row1 = sheet.getRow(0);
for(int i=0, column =0; i < colc; i++){
if(i == 1){
heading[column++] = "";
}
heading[column++] = cellToString(row1.getCell(i));
}
Object [][]j=new Object[rowc-1][colc+1];
for (int i = 1; i < rowc; i++) {
row1 = sheet.getRow(i);
for (int jj = 0, column = 0; jj < colc; jj++) {
if(column == 1){
j[i-1][column++] = new Boolean(false);
j[i-1][column] = cellToString(row1.getCell(jj));
}
else{
j[i-1][column]=cellToString(row1.getCell(jj));
}
column++;
}
}
jTable1.setModel(new DefaultTableModel(j, heading){
public Class getColumnClass(int columnIndex) {
if(columnIndex == 0){
return java.lang.Integer.class;
}
else if(columnIndex == 1){
return java.lang.Boolean.class;
}
else{
return java.lang.Object.class;
}
}
});
jTable1.getColumnModel().getColumn(1).setMaxWidth(60);
jTable1.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
jTable1.getTableHeader().setReorderingAllowed(false);
} catch (IOException ex) {
JOptionPane.showMessageDialog(BarcodePrint.this, ex);
}
Writing data into excel after editing column BarcodePrint:
try {
FileInputStream file = new FileInputStream(new File("c:\\sample.xlsx"));
XSSFWorkbook fWorkbook = new XSSFWorkbook(file);
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
Date currentdate = new Date();
XSSFSheet fsheet = fWorkbook.getSheetAt(0);
Double barcodeprintstatus = new Double(1);
int newindex = 24;
int printdate = 26;
int uniqueid = 27;
for(int i = 0; i < jTable1.getModel().getRowCount(); i++){
if(jTable1.getModel().getValueAt(i, newindex).equals("1")){
for(int k=1; k < fsheet.getPhysicalNumberOfRows(); k++){
XSSFCell cell = fsheet.getRow(k).getCell(uniqueid-1);
String uid = cell.getRawValue();
if(jTable1.getModel().getValueAt(i, uniqueid).toString().equals(uid)){
cell = (fsheet.getRow(i+1)).getCell(newindex-1);
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(barcodeprintstatus);
cell = fsheet.getRow(i+1).getCell(printdate-1);
cell.setCellValue(currentdate);
cell.setCellStyle(cs);
}
}
}
}
file.close();
FileOutputStream fileOutputStream = new FileOutputStream("c:\\sample.xlsx");
fWorkbook.write(fileOutputStream); // this is taking so much of time. Approximately 1 min.
fileOutputStream.close();
}catch(Exception e){
e.printStackTrace();
}
I am unable to solve this problem. fWorkbook.write(fileOutputStream); is taking so much of time as I mentioned above. Please help. Is there any other way to save the excel file? Or can I write data partially for a single column rather than the hole workbook?
Try to improve the code.
You call fSheet.getRow(i+1) twice. Try to introduce a variable and reuse the row rather than obtain it.
cell.setCellValue(new Double(1));
Create the 1 double once before the for loop and reuse it.
XSSFCellStyle cs = fWorkbook.createCellStyle();
cs.setDataFormat(fWorkbook.getCreationHelper().createDataFormat().getFormat("dd/MMMM/yyyy"));
Move the cell style creation and initializing out of the for loop. Create it before the loop and reuse.
cell.setCellValue(new Date());
Introduce a Date variable and create the Date just once. Again before the for loop.
Add a BufferedOutputStream around the FileOutputStream.

Categories