Java xlsx

Ads

 
 

Share on Google+Share on Google+

Vikram
Java xlsx
2 Answer(s)      8 years ago
Posted in : JSP-Servlet
Ads
View Answers

September 21, 2009 at 1:07 PM


Hi Friend,

1)Write .xlsx file:

import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CreateFile{
public static void main(String args[]) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
int rowCount=0;
int count=0;
HSSFRow headerRow = sheet.createRow((short) 0);
HSSFRow dataRow =sheet.createRow((short) 1);
headerRow.createCell((short)0).setCellValue("ID");
headerRow.createCell((short)1).setCellValue("NAME");
headerRow.createCell((short)2).setCellValue("Address");
for(int i=0;i<5;i++){
rowCount++;
sheet.createRow((short)rowCount);
dataRow.createCell((short)0).setCellValue(1);
dataRow.createCell((short)1).setCellValue("Rose");
dataRow.createCell((short)2).setCellValue("Delhi");
count++;
}
File f = new File("c:\\test.xlsx");
FileOutputStream fos = new FileOutputStream(f);
wb.write(fos);
}
}

2)Read .xlsx File:

import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadFile {
public static void main( String [] args ) {

String fileName="C:\\test.xlsx";
Vector dataHolder=ReadFile(fileName);
read(dataHolder);
}
public static Vector ReadFile(String fileName){
Vector cellVectorHolder = new Vector();
try{
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector=new Vector();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
}catch (Exception e){ }
return cellVectorHolder;
}
private static void read(Vector dataHolder) {
for (int i=0;i<dataHolder.size(); i++){
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++){
HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
System.out.print(stringCellValue+"\t");
}
System.out.println();
}
}
}
Thanks


September 1, 2010 at 10:45 PM


Hi... If you want to read/write XLSX file (of office
2007). You just can change all "HSSFwhatever" by "XSSF whatever"... As follows...


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testes;

import java.io.*;
import java.util.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadFile {

public static void main(String[] args) {

String fileName = "relat2002_12.xlsx";
Vector dataHolder = ReadFile(fileName);
read(dataHolder);
}

public static Vector ReadFile(String fileName) {
Vector cellVectorHolder = new Vector();
try {
FileInputStream arquivo = new FileInputStream(fileName);
XSSFWorkbook planilha = new XSSFWorkbook(arquivo);

XSSFSheet aba = planilha.getSheetAt(0);
Iterator rowIter = aba.rowIterator();

while (rowIter.hasNext()) {
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();

Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {

XSSFCell myCell = (XSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
} catch (Exception e) {
}
return cellVectorHolder;
}

private static void read(Vector dataHolder) {
for (int i = 0; i < dataHolder.size(); i++) {
Vector cellStoreVector = (Vector) dataHolder.elementAt(i);

for (int j = 0; j < cellStoreVector.size(); j++) {
XSSFCell myCell = (XSSFCell) cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
System.out.print(stringCellValue + "\t");
}
System.out.println();
}

}
}

Ads









Related Tutorials/Questions & Answers:
Java xlsx - JSP-Servlet
Java xlsx  Dear Sir's / Madam's i want to read/write XLSX file... but not xlsx file.   Hi Friend, 1)Write .xlsx file: import java.io....); } } 2)Read .xlsx File: import java.io.*; import java.util.*; import
How to delete .xlsx file in java?
How to delete .xlsx file in java?  Hello Sir, I am unable to delete .xlsx file using below code, What is wrong here? if(myFile != null && myFile.exists()    && myFile.canRead() &&
Advertisements
Java code for conversion between .xls and .xlsx or vice versa - Development process
Java code for conversion between .xls and .xlsx or vice versa  I've...) and clients may be using MS Excel 2007 / 2010 (.xlsx) format. I need a solution or tool in Java for converting between .xls to .xlsx or Vice versa
Java code for converting between .xls to .xlsx and vice versa - Java Beginners
Java code for converting between .xls to .xlsx and vice versa  I've a web application which allows clients to upload or download MS Excel sheets. I need a solution in Java from which i can upload in MS Excel 2003 and download
How can i extract email ids from xls/xlsx file in java?
How can i extract email ids from xls/xlsx file in java?  that's the code..... i tried to extract email id;s from xls file........... but it doesn;t gives the proper output? table is like this: bhavik
Version of com.monitorjbl>xlsx-streamer dependency
List of Version of com.monitorjbl>xlsx-streamer dependency
Version of com.monitorjbl>xlsx-streamer dependency
List of Version of com.monitorjbl>xlsx-streamer dependency
how to convert a .xlsx to .xls fil - JSP-Servlet
how to convert a .xlsx to .xls fil  Dear sir, How to convert a .xlsx file to .xls using poi .Please give me some clue.... Thanks in advance
Maven Dependency xlsx-streamer >> 0.2.11
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.11 in your project
Maven Dependency xlsx-streamer >> 0.2.12
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.12 in your project
Maven Dependency xlsx-streamer >> 0.2.3
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.3 in your project
Maven Dependency xlsx-streamer >> 0.2.4
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.4 in your project
Maven Dependency xlsx-streamer >> 0.2.6
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.6 in your project
Maven Dependency xlsx-streamer >> 0.2.7
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.7 in your project
Maven Dependency xlsx-streamer >> 0.2.9
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.9 in your project
Maven Dependency xlsx-streamer >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version1.0.0 in your project
Maven Dependency xlsx-streamer >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version1.0.2 in your project
Maven Dependency xlsx-streamer >> 1.1.0
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version1.1.0 in your project
Maven Dependency xlsx-streamer >> 0.2.10
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.10 in your project
Maven Dependency xlsx-streamer >> 0.2.13
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.13 in your project
Maven Dependency xlsx-streamer >> 0.2.5
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.5 in your project
Maven Dependency xlsx-streamer >> 0.2.8
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version0.2.8 in your project
Maven Dependency xlsx-streamer >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of com.monitorjbl >> xlsx-streamer version1.0.1 in your project
how to connect xlsx(2007 excel) - JDBC
how to connect xlsx(2007 excel)  i am not able connect to office 2007 excel file from jdbc
JSP Excel report download for .xlsx format
JSP Excel report download for .xlsx format  <%@ page contentType... but my requirement is to download excel with format .xlsx (m.S 2007),so i changed.../opening file with format .xlsx. CAN you guide me in this. I am using 2007 M.S
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database table  Here is my requirement, I want to read the records from a .xlsx...){ if(saveFile.endsWith(".xlsx")){ byte[] b=strLine.getBytes
Create Excel(.xlsx) document using Apache POI
Create Excel(.xlsx) document using Apache POI In this section, you will learn how to create a Excel sheet having .xlsx extension using Apache POI library. In the given below example, we will going to create excel document having one
Read Excel(.xlsx) document using Apache POI
Read Excel(.xlsx) document using Apache POI In this section, you will learn how to read Excel file having .xlsx extension using Apache POI library...;xlsx/workbook.xlsx"; // // Create an ArrayList to store the data read
Upload csv or .xlsx file from JSP form to Database Table using servlet
Upload csv or .xlsx file from JSP form to Database Table using servlet  dear sir, i need the Servlet code that reads the .xlsx or CSV excel file and stores it into the oracle database table. Sir Its very Urgent I have
java
java  diff bt core java and java
java
java  what is java

Ads

 
Advertisement null

Ads