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