Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Vikram
Java xlsx
2 Answer(s)      5 years and a month ago
Posted in : JSP-Servlet



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

}
}


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
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
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
Excel conversion tool. - Java Beginners
Excel conversion tool.  Hi, I need a conversion tool which can convert .xls(Excel 2003) to .xlsx (Excel 2007) and vice-versa. Please suggest any links ro tools. Thank You
Hash value is not same - Java Beginners
Hash value is not same  Hi, I've two Excel files with same data. One is in Excel 2003 (.xls) format and other one is in Excel 2007 (.xlsx) format. I've generated one way hash using SHA-1 algorithm. But the hash value of Excel
java
java  diff bt core java and java
java
java  what is java
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  is java purely object oriented language
java
java  how use java method
java
java  send me java interview questions
 

 

 

DMCA.com