Can't obtain the input stream from /docProps/app.xml

來源:互聯網
上載者:User

標籤:

今天在做poi修改樣式時,報了以下錯誤:

Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can‘t obtain the input stream from /docProps/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

網上查了相關文檔,沒找到相關資料,也沒有給出解決資料,絕望之餘,網上尋找了poi修改資料的代碼,運行了下竟然可以通過,以下為代碼:

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位置int coloum = 1; // 比如你要擷取第1列try {HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));HSSFSheet sheet = workbook.getSheet("Sheet1");for (int i = 0; i <= sheet.getLastRowNum(); i++) {HSSFRow row = sheet.getRow((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();}}}

 我的錯誤碼如下:

String fileToBeRead = "D:\\template.xlsx"; // excel位置File dataFile=new File(fileToBeRead);try {XSSFWorkbook workbook = new XSSFWorkbook(dataFile);XSSFSheet sheet = workbook.getSheet("Sheet1");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();}

 

簡單比較以下差異處在XSSFWorkbook初始化的時候:

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));(正確的)XSSFWorkbook workbook = new XSSFWorkbook(dataFiel);(錯誤的)

錯誤的描述是無法獲得輸入資料流: can  not  obtain  input  stream  for  xml....

 是不是一個outputStream一定要對應一個InputStream呢?????

以上錯誤碼修改如下,即可通過:

 

String fileToBeRead = "D:\\template.xlsx"; // excel位置File dataFile=new File(fileToBeRead);try {XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));XSSFSheet sheet = workbook.getSheet("Sheet1");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();}

 

Can't obtain the input stream from /docProps/app.xml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.