I'm reading the xls file as per below:
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow rowTPNB;
HSSFCell cellTPNB;
rowTPNB = sheet.getRow(5);
cellTPNB = rowTPNB.getCell(1);
With that code i'm getting the value from excel which like this 76653764 to 7.6653764E7. How do I do to remain value to 76653764. Please advice
You can do it this way
Double.valueOf("7.6653764E7").longValue();
This will return a long value and remove the scientific notation :)
Related
I need to read file excel, and I am working with java spring boot, Apache poi.
they told us
When working with the newer .xlsx file format, you would use the XSSFWorkbook, XSSFSheet, XSSFRow, and XSSFCell classes. To work with the older .xls format, use the HSSFWorkbook, HSSFSheet, HSSFRow, and HSSFCell classes.
this is my code:
Workbook workbook;
if (FileExtension == "xlsx"){
workbook = new XSSFWorkbook(excelFile);
}
else if (FileExtension == "xls"){
workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetAt(0);
Now im getting "workbook" might not have been initialized.
How to solve that?
Current apache poiversions provide WorkbookFactory. Using the create methods of that class there is no need for determining the file format (XSSF or HSSF) by file extension. The WorkbookFactory.create methods are creating XSSFWorkbook or HSSFWorkbook dependent of the file contents found.
So do using:
...
Workbook workbook = WorkbookFactory.create(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
...
I think what you want to express is this...
Workbook workbook;
if (FileExtension == "xlsx"){
workbook = new XSSFWorkbook(excelFile);
}
// for other case
else{
workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetAt(0);
Notice the comment in the code, I am sure can investigate the difference by yourself, whick helps a lot.
You need to use xmlbeans3.1 instead of xmlbeans4.0.
Try this...
Workbook workbook = null;
Not initialized.. you should do this
Workbook wb;
wb = Workbook.getWorkbook(new File("d:\\test\\book1.xls"));
I believe it is apart of JVM issues, but initializing and declaring at the sometime is sometimes the only way.
Try this:
if (FileExtension == "xlsx"){
Workbook workbook = new XSSFWorkbook(excelFile);
}
else if (FileExtension == "xls"){
Workbook workbook = new HSSFWorkbook(excelFile);
}
Sheet datatypeSheet = workbook.getSheetA
Please find the Apache POI java code to read the .xls file.
FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(file);
while reading the .xls file using Java Apache POI, I am getting the below error in the Java Console.
java.io.IOException: Invalid header signature; read 0x6C6D783F3CBFBBEF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
I am able to manually open the excel file without any issues. Do we have the solution to overcome this. I'm completely out of ideas so any help/pointers are greatly appreciated :)
FileInputStream fis = new FileInputStream(new File(yourpath+"/WebContent/ProductUpload.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
int numberOfSheets = workbook.getNumberOfSheets();
for(int i=0; i < numberOfSheets; i++){
XSSFSheet sheet = workbook.getSheetAt(i);
Iterator ite = sheet.rowIterator();
while(ite.hasNext()){
Row row = (Row)ite.next();
Iterator<org.apache.poi.ss.usermodel.Cell> cite = row.cellIterator();
while(cite.hasNext()){
org.apache.poi.ss.usermodel.Cell cell = cite.next();
}
}
}
I simply want to put the result of a Java program into an Excel cell. Let's assume the result is 5, then my code in main() would look like this:
XSSFWorkbook wb = C:\Users\Username\Desktop\java-programs\results.xlsm;
XSSFSheet ExcelSheet = wb.getSheet("numbers");
XSSFRow row = ExcelSheet.getRow(0);
XSSFCell cell = row.getCell(0);
cell.setCellValue(5);
However, this code doesn't compile.
I read some examples on this topic but the code given simply refers to the Excel sheet as (e.g.) "sheet" and then goes on with sheet.getRow(some number). The examples don't explain how I tell Java which workbook to write to.
Your first line is incorrect.. try this:
String fileName = "C:/Users/Username/Desktop/java-programs/results.xlsm";
FileInputStream fileIn = new FileInputStream(fileName);
Workbook wb = WorkbookFactory.create(fileIn);
// update the workbook
wb.getSheet("numbers").getRow(0).getCell(0).setCellValue(5);
fileIn.close();
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.close();
I'm trying to change value of cell in .xls document.
In .xls file i have got only 1 cell - A1 with abc value inside.
My code:
File fo = new File("D:\\TMP\\Zeszyt1.xls");
HSSFWorkbook a = new HSSFWorkbook(new FileInputStream(fo));
HSSFSheet my_sheet = a.getSheetAt(0);
HSSFRow my_row = my_sheet.getRow(0);
HSSFCell myCell;
myCell = my_row.getCell(0);
myCell.setCellValue("NEW VALUE");
How to commit this changes? When i open .xls file i still have got abc value inside A1.
You have to write to the file.
FileOutputStream outputStream = new FileOutputStream(new File("abc.xls"));
workbook.write(outputStream);
outputStream.close();//Close in finally if possible
I'm a little confused, I used to do this:
HSSFWorkbook wb = new HFFSWorkbook();
But with the new POI, I dont have to do that.
I can't do this:
Workbook wb = new Workbook();
I understand WorkbookFactory.create, but that is for opening a file.
How do I set up a new workbook with this ss model?
You can still use the SS model but need to decide on the file format at the time of creation.
For xls -> Workbook wb = new HSSFWorkbook();
For xlsx -> Workbook wb = new XSSFWorkbook();
In "New POI", you can write/read both XLS files and XLSX files. In any case, for XLS file-format you were using:
HSSFWorkbook wb = new HSSFWorkbook();
So for XLSX file-format, you have to use:
XSSFWorkbook wb = new XSSFWorkbook();
// you could also do below
// Workbook wb = new XSSFWorkbook();
Also it would be helpful for you if you refer below links for starting with XLS to XLSX migration.
1. http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html
2. http://poi.apache.org/spreadsheet/converting.html
Make sure you download and add the POI JAR file to your project’s class path before running the code. The Apache POI JAR file can be found here.
public void main(String[] args) throws IOException {
// Directory path where the xls file will be created
String destinationFilePath = "C:/Users/devesh_/Documents/HelloWorld.xls";
// Create object of FileOutputStream
FileOutputStream fout = new FileOutputStream(destinationFilePath);
// Build the Excel File
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
HSSFWorkbook workBook = new HSSFWorkbook();
// Create the spreadsheet
HSSFSheet spreadSheet = workBook.createSheet("Hello_World");
// Create the first row
HSSFRow row = spreadSheet.createRow((short) 0);
// Create the cells and write to the file
HSSFCell cell;
// Write Hello
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("Hello"));
// Write World
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("World"));
workBook.write(outputStream);
outputStream.writeTo(fout);
outputStream.close();
fout.close();
}
When creating a file, you need to decide up front what format it'll be - you can't just wait until write-out time to do that. You code would be something like:
Workbook wb = null;
if (shouldBeXLS) {
wb = new HSSFWorkbook();
} else {
wb = new XSSFWorkbook();
}
// work on the file in a generic way
// save, with a suitable name
String filename = "test.xls";
if (!shouldBeXLS) { filename = filename + "x"; }
FileOutputStream fout = new FileOutputStream(filename);
wb.write(fout);
fout.close();
At the start, decide what format you want for this particular instance, and create that. Treat it as a general workbook, and write to it in the common way. At the end, remember what it is so you can give the file the right extension!
(When reading a file in, WorkbookFactory will let you load the appropriate instance for the file type. When creating a new file, you have to pick yourself as there's nothing there yet!)