My code:
package read_write;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.common.base.Function;
public class Readexcel {
public static void main(String[] args) throws IOException {
File src = new File("D:\\J\\clients_pw.xlsx");
FileInputStream fis = new FileInputStream (src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1= wb.getSheet("MAS_details");
String data1 = sheet1.getRow(1).getCell(0).getStringCellValue();
System.out.println(data1);
}
}
I am facing following error while trying to execute this
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/compress/archivers/zip/ZipFile at
org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298) at
org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
at
org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:307)
at read_write.Readexcel.main(Readexcel.java:19)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.compress.archivers.zip.ZipFile at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown
Source) at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown
Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
I am not sure if i have added all jars. i have added all apache poi jars and google collect
Thank you so much even i had the same issue. It resolved after adding Compress jar
Related
Summary of Problem
I have created a simple program to read from and write to a spreadsheet. But it does not work. Does anyone know what I am doing wrong?
What I Have Tried
I have successfully been able to read from a spreadsheet. I have successfully been able to write to a spreadsheet. However, I cannot do both at the same time.
It seems that the createRow function overwrites the entire row. Thus, erasing previous data. This is a pretty severe constraint. It might be what is preventing me from reading and writing at the same time.
I noticed that the output says "The supplied file was empty (zero bytes long)". But I made sure that the path is correct and that there is indeed data in the spreadsheet. Not sure where that error is coming from.
I've tried following suggestions from other posts on Stack Overflow, but none of their suggestions including closing both the fileInputStream and fileOutputStream seems to affect my program at all.
My Code
package certExamPractice;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SpreadsheetPractice {
public static void main(String[] args) throws IOException {
String path = "C:/Users/james/Desktop/Spreadsheets/Spreadsheet4.xlsx";
FileInputStream fileInputStream = new FileInputStream(path);
FileOutputStream fileOutputStream = new FileOutputStream(path);
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.createSheet("Sheet1");
Set<String> vir = new HashSet<String>();
vir.add(sheet.getRow(0).getCell(0).getStringCellValue());
vir.add(sheet.getRow(1).getCell(0).getStringCellValue());
vir.add(sheet.getRow(2).getCell(0).getStringCellValue());
vir.add(sheet.getRow(3).getCell(0).getStringCellValue());
sheet.getRow(0).createCell(1).setCellValue("zeta");
sheet.getRow(1).createCell(1).setCellValue("eta");
sheet.getRow(2).createCell(1).setCellValue("theta");
sheet.getRow(3).createCell(1).setCellValue("iota");
System.out.println("Vir = " + vir);
workbook.write(fileOutputStream);
workbook.close();
fileOutputStream.close();
}
}
My Spreadsheet
My spreadsheet.
The Output
Exception in thread "main" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:112)
at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:209)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:143)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:175)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:104)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:47)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:299)
at certExamPractice.SpreadsheetPractice.main(SpreadsheetPractice.java:18)
The following code allows you to read from and write to a spreadsheet. You need to write the beginning code and the end code. Then you can read and write as you wish in the middle.
package certExamPractice;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
public class SpreadsheetPractice {
public static void main(String[] args) throws IOException{
//BEGINNING
String path = "C:/Users/james/Desktop/Spreadsheets/Spreadsheet4.xlsx";
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook = WorkbookFactory.create(fileInputStream);
Sheet sheet = workbook.getSheet("Sheet1");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
if (cell == null) { cell = row.createCell(0); }
//READ
Set<String> vir = new HashSet<String>();
vir.add(sheet.getRow(0).getCell(0).getStringCellValue());
vir.add(sheet.getRow(1).getCell(0).getStringCellValue());
vir.add(sheet.getRow(2).getCell(0).getStringCellValue());
vir.add(sheet.getRow(3).getCell(0).getStringCellValue());
System.out.println("Vir = " + vir);
//WRITE
sheet.getRow(0).createCell(1).setCellValue("zeta");
sheet.getRow(1).createCell(1).setCellValue("theta");
sheet.getRow(2).createCell(1).setCellValue("eta");
sheet.getRow(3).createCell(1).setCellValue("iota");
//END
try (OutputStream fileOutputStream = new FileOutputStream(path)) {
workbook.write(fileOutputStream);
}
workbook.close();
}
}
im trying to create xlsx file with groovy.
i have Soapui open source 5.6.0
i have microsoft office 365
Running with windows 10
i added these jars is lib/ext:
commons-compress-1.20.jar
dom4j-1.6.1.jar
xmlbeans-2.6.0.jar
poi-5.0.0.jar
poi-ooxml-5.0.0.jar
poi-ooxml-schemas-3.9.jar
xmlbeans-4.0.0.jar
With this line: Workbook wb = new HSSFWorkbook();
i can create xls file for excel version 97-2003
With this line: Workbook wb = new XSSFWorkbook();
i can create xlsx file but cannot open.
What is the probleme here, i downloaded the latest jars for POI.
Do i missing something here?
Thank you for your help.
My code is:
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.xssf.usermodel.*
import org.apache.poi.ss.util.*
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.CellStyle;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.*
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class CreateSheet {
public static void main(String[] args)
throws FileNotFoundException, IOException
{
// Creating xls file
//Workbook wb = new HSSFWorkbook();
// Creating xlsx file
Workbook wb = new XSSFWorkbook();
// An output stream accepts output bytes and sends them to sink.
OutputStream fileOut = new FileOutputStream("C:\\temp\\test.xlsx");
// Creating Sheets using sheet object
Sheet sheet1 = wb.createSheet("Test");
wb.write(fileOut);
}
}
It could be you're not closing the stream
try:
class CreateSheet {
static main(args) throws FileNotFoundException, IOException {
// Creating xlsx file
Workbook wb = new XSSFWorkbook()
// Creating Sheets using sheet object
Sheet sheet1 = wb.createSheet("Test")
new File("C:\\temp\\test.xlsx").withOutputStream { fileOut ->
wb.write(fileOut)
}
}
}
I'm trying to tokenize a piece of Chinese text with Stanford NLP but the program throws exceptions all the time.
I tried different ways to load the properties file but they didn't work.
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import java.io.InputStream;
import java.util.*;
public class Spider {
public static void main(String[] args) {
try {
StanfordCoreNLP ppl;
Properties prop = new Properties();
InputStream in = Spider.class.getClassLoader().getResourceAsStream("StanfordCoreNLP-chinese.properties");
prop.load(in);
ppl = new StanfordCoreNLP(prop);
Annotation doc = new Annotation("浮云白日,山川庄严温柔。");
ppl.annotate(doc);
ppl.prettyPrint(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The exceptions are as follows:
java.io.StreamCorruptedException: invalid type code: 3F at
java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1622)
at
java.base/java.io.ObjectInputStream.readArray(ObjectInputStream.java:1993)
at
java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1588)
at
java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
at
edu.stanford.nlp.ie.crf.CRFClassifier.loadClassifier(CRFClassifier.java:2642)
at
edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1473)
at
edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1505)
at
edu.stanford.nlp.ie.crf.CRFClassifier.getClassifier(CRFClassifier.java:2939)
at
edu.stanford.nlp.ie.ClassifierCombiner.loadClassifierFromPath(ClassifierCombiner.java:286)
at
edu.stanford.nlp.ie.ClassifierCombiner.loadClassifiers(ClassifierCombiner.java:270)
at
edu.stanford.nlp.ie.ClassifierCombiner.(ClassifierCombiner.java:142)
at
edu.stanford.nlp.ie.NERClassifierCombiner.(NERClassifierCombiner.java:108)
at
edu.stanford.nlp.pipeline.NERCombinerAnnotator.(NERCombinerAnnotator.java:125)
at
edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:68)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$5(StanfordCoreNLP.java:523)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$null$30(StanfordCoreNLP.java:602)
at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:126) at
edu.stanford.nlp.util.Lazy.get(Lazy.java:31) at
edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:149)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:251)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:192)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:188)
at Spider.main(Spider.java:13)
edu.stanford.nlp.io.RuntimeIOException: java.io.IOException: Couldn't
load classifier from
edu/stanford/nlp/models/ner/chinese.misc.distsim.crf.ser.gz at
edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:70)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$5(StanfordCoreNLP.java:523)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$null$30(StanfordCoreNLP.java:602)
at edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:126) at
edu.stanford.nlp.util.Lazy.get(Lazy.java:31) at
edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:149)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:251)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:192)
at
edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:188)
at Spider.main(Spider.java:13) Caused by: java.io.IOException:
Couldn't load classifier from
edu/stanford/nlp/models/ner/chinese.misc.distsim.crf.ser.gz at
edu.stanford.nlp.ie.ClassifierCombiner.loadClassifierFromPath(ClassifierCombiner.java:296)
at
edu.stanford.nlp.ie.ClassifierCombiner.loadClassifiers(ClassifierCombiner.java:270)
at
edu.stanford.nlp.ie.ClassifierCombiner.(ClassifierCombiner.java:142)
at
edu.stanford.nlp.ie.NERClassifierCombiner.(NERClassifierCombiner.java:108)
at
edu.stanford.nlp.pipeline.NERCombinerAnnotator.(NERCombinerAnnotator.java:125)
at
edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:68)
... 9 more Caused by: java.lang.ClassCastException: class
java.util.ArrayList cannot be cast to class
edu.stanford.nlp.classify.LinearClassifier (java.util.ArrayList is in
module java.base of loader 'bootstrap';
edu.stanford.nlp.classify.LinearClassifier is in unnamed module of
loader 'app') at
edu.stanford.nlp.ie.ner.CMMClassifier.loadClassifier(CMMClassifier.java:1095)
at
edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1473)
at
edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1505)
at
edu.stanford.nlp.ie.AbstractSequenceClassifier.loadClassifier(AbstractSequenceClassifier.java:1495)
at
edu.stanford.nlp.ie.ner.CMMClassifier.getClassifier(CMMClassifier.java:1141)
at
edu.stanford.nlp.ie.ClassifierCombiner.loadClassifierFromPath(ClassifierCombiner.java:292)
... 14 more
Retrieve data from an excel sheet,using Following code:
import java.io.FileInputStream;
import java.io.IOException;
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 poi_excel {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis =new FileInputStream("F:\\Selenium Using Web Driver\\Plugins\\HEC_login.xlsx");
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sheet=wb.getSheet("script");
XSSFRow row=sheet.getRow(1);
XSSFCell cell=row.getCell(1);
String value = cell.getStringCellValue();
System.out.println(value);
} }
following error occur
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.poi.openxml4j.util.ZipSecureFile$1 (file:/F:/Selenium%20Using%20Web%20Driver/Plugins/poi-bin-3.17-20170915/poi-3.17/poi-ooxml-3.17.jar) to field java.io.FilterInputStream.in
WARNING: Please consider reporting this to the maintainers of org.apache.poi.openxml4j.util.ZipSecureFile$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NullPointerException
at poi_excel.main(poi_excel.java:16)
Kindly provide solution to resolve this problem.
XSSFSheet sheet=wb.getSheet("script.type");
Enter the file type (.xlsx or whatever) in place of type and try again.
Edit: Also check whether the file is in the working directory.
I'm using this program in Keyword driven framework for selenium web driver tool. But due to this program i'm getting null pointer exception every time i tried to debug the code but cant find any solution. please suggest me the solution for that error.
package utility;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
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.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
public class ExcelUtils {
private static HSSFSheet ExcelWSheet;
private static HSSFWorkbook ExcelWBook;
private static HSSFCell Cell;
//This method is to set the File path and to open the Excel file
//Pass Excel Path and SheetName as Arguments to this method
public static void setExcelFile(String Path,String SheetName) throws Exception
{
FileInputStream ExcelFile = new FileInputStream(Path);
ExcelWBook = new HSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
//This method is to read the test data from the Excel cell
//In this we are passing parameters/arguments as Row Num and Col Num
public static String getCellData(int RowNum, int ColNum) throws Exception{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
System.out.println(Cell);
String CellData = Cell.getStringCellValue();
System.out.println("."+CellData);
return CellData;
}
}