Today, when making a POI modification style, the following error was reported:
Exception in thread "main" Org.apache.poi.POIXMLException:java.io.IOException:Can ' t obtain the input stream from/do Cprops/app.xml at org.apache.poi.POIXMLDocument.getProperties (poixmldocument.java:148) at Org.apache.poi.POIXMLDocument.write (poixmldocument.java:199) at Com.supcon.ChangeXlsxCell.main ( changexlsxcell.java:29) caused By:java.io.IOException:Can ' t obtain the input stream from/docprops/app.xml at Org.apache.poi.openxml4j.opc.PackagePart.getInputStream (packagepart.java:502) at Org.apache.poi.POIXMLProperties. < Init > (poixmlproperties.java:75) at org.apache.poi.POIXMLDocument.getProperties (poixmldocument.java:146) ... 2 More
Online Check the relevant documents, did not find relevant information, and did not give a solution to the information, despair, online find the POI modified data code, run down unexpectedly can pass, the following code:
Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;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;public class Changecell {@SuppressWarnings ("deprecation") public static void main (string[] args) {String filetoberead = "C:\\exp.xls" ; Excel position int Coloum = 1; For example you want to get 1th column try {hssfworkbook workbook = new Hssfworkbook (new FileInputStream (Filetoberead)); Hssfsheet sheet = workbook.getsheet ("Sheet1"); for (int i = 0; I <= sheet.getlastrownum (); i++) {Hssfrow row = Sheet.get Row (short) i), if (null = = row) {continue;} else {Hssfcell cell = Row.getcell ((short) Coloum); if (null = = cell) {continue; } else {System.out.println (Cell.getnumericcellvalue ()); int temp = (int) cell.getnumericcellvalue (); cell.setcellvalue (temp + 1);}} FileOutputStream out = null;try {out = new FileoutputstreaM (filetoberead); Workbook.write (out);} catch (IOException e) {e.printstacktrace ();} finally {try {out.close ();} catch (IOException e) {e.printstacktrace ();}}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}
My error code is as follows:
String filetoberead = "d:\\template.xlsx"; Excel location file Datafile=new file (filetoberead); try {xssfworkbook workbook = new Xssfworkbook (datafile); Xssfsheet sheet = workbook.getsheet ("Sheet1"); FileOutputStream out = null;try {off = new FileOutputStream (filetoberead); Workbook.write (out);} catch (IOException e) {e. Printstacktrace ();} Finally {try {out.close ();} catch (IOException e) {e.printstacktrace ()}}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}
A simple comparison of the following differences at Xssfworkbook initialization:
Xssfworkbook workbook = new Xssfworkbook (new FileInputStream (filetoberead));(correct) xssfworkbook workbook = new Xssfworkbook (Datafiel);(wrong)
The error description is that the input stream could not be obtained: Can not obtain input stream for XML ....
is not a outputstream must correspond to a InputStream?????
The above error code can be modified as follows:
String filetoberead = "d:\\template.xlsx"; Excel location file Datafile=new file (filetoberead), try {xssfworkbook workbook = new Xssfworkbook (New FileInputStream ( Filetoberead)); Xssfsheet sheet = workbook.getsheet ("Sheet1"); FileOutputStream out = null;try {off = new FileOutputStream (filetoberead); Workbook.write (out);} catch (IOException e) {e. Printstacktrace ();} Finally {try {out.close ();} catch (IOException e) {e.printstacktrace ()}}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}
Can ' t obtain the input stream From/docprops/app.xml