Fight With Me!!!

导航

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

今天在做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();
		}

 

posted on 2015-08-04 18:19  nickTimer  阅读(2871)  评论(0编辑  收藏  举报