我开始执行一个新过程,将excel文件中的行添加到另一个excel文件中,换句话说:将行追加到现有的Excel工作表中。
为此,我正在测试以下代码,基于我在Internet上找到的代码:
私人全球声明:
private HSSFRow row;
private HSSFCell cell;
private FileInputStream inFile;
private POIFSFileSystem poiFile;
private HSSFSheet excelSheet = null;
private HSSFWorkbook excelBook = null;
private FileOutputStream outFile = null;
private static String outputFileName;
我用main方法开始一切:
public static void main(String args[]){
outputFileName = "C:\\test1.xls";
List<String> newContent = new ArrayList<>();
newContent.add("Test Val1");
newContent.add("Test Val2");
newContent.add("Test Val3");
testingClass test = new testingClass();
test.overwriteExcel(outputFileName,newContent);
}
有了这个我创建了文件,并且我假装更新它:
public void overwriteExcel(String outputFileName, List<String> newContent){
try{
if(new File(outputFileName).exists()){ //--If the file exists/is already created
outFile = new FileOutputStream(new File(outputFileName),true); /*With the "true" parameter, the change must been applied but, nothing happens...*/
inFile = new FileInputStream(new File(outputFileName));
poiFile = new POIFSFileSystem(inFile);
excelBook = new HSSFWorkbook(poiFile);
excelSheet = excelBook.getSheetAt(0);
}else{ //--Create the file
outFile = new FileOutputStream(new File(outputFileName));
excelBook = new HSSFWorkbook();
excelSheet = excelBook.createSheet("Sheet1");
}
int i = 0;
for(Iterator iter = newContent.iterator(); iter.hasNext();){
Object val = iter.next();
if(val!=null){
row = excelSheet.createRow(excelSheet.getLastRowNum()+1);
cell = row.createCell(0); //--Temporaly, I change this val manually
cell.setCelval(newContent.get(i++));
}
}
excelBook.write(outFile);
outFile.flush();
outFile.close();
}catch(Exception ex){
JOptionPane.showMessageDialog(null,"Error: "+ex.getMessage(),"Warning!",JOptionPane.ERROR_MESSAGE);
}
}
当课程结束他的过程时,我看到了文件并且我可以感知到文件大小的变化,但是当我打开它时,一切都是一样的,在我关闭文件后,这会返回到之前的大小... < / p>
试着理解发生了什么,我在row.createCell(0)中将“0”改为“1”,我删除文件并重复该过程,一切正常,但是当我再次将值更改为“0”之后“,尚未应用更改...我不知道它是否是某些文件权限...
我认为代码有问题,这不是我第一次使用excel文件,但这是我第一次尝试修改它。
事先,谢谢!!!
答案 0 :(得分:5)
我遇到了同样的情况并且很奇怪,你不必在追加模式下打开你的输出流:
总之; 更改
outFile = new FileOutputStream(new File(outputFileName),true);
要强>
outFile = new FileOutputStream(new File(outputFileName));
原因是写方法倾向于将完整的工作簿写入输出流,并且以追加模式打开将意味着“附加”现有工作簿以及由此创建的新工作簿并且没有发生(似乎'失败',除了种类)。
相反,输出流应该在'new'模式下打开(没有布尔值为true),这样就有一个最新的和最好的工作簿作为一个整体写入流中。
希望它有意义! :)
我对您的代码进行了一些更改,这些更改与我对下面POI版本的理解一致,这非常合适:
private FileInputStream inFile;
private POIFSFileSystem poiFile;
private XSSFRow row;
private XSSFCell cell;
private XSSFSheet excelSheet = null;
private XSSFWorkbook excelBook = null;
private static String outputFileName;
public void overwriteExcel(File file, List<String> newContent) {
try {
if (file.exists()) {
inFile = new FileInputStream(file);
// poiFile = new POIFSFileSystem(inFile);
excelBook = new XSSFWorkbook(inFile);
excelSheet = excelBook.getSheetAt(0);
System.out.println("Here -> " + excelSheet.getLastRowNum());
} else { // --Create the file
excelBook = new XSSFWorkbook();
excelSheet = excelBook.createSheet("Sheet1");
}
int i = 0;
for (String val : newContent.toArray(new String[newContent.size()])) {
if (val != null) {
System.out.println("Row " + excelSheet.getLastRowNum());
row = excelSheet.createRow(excelSheet.getLastRowNum() + 1);
cell = row.createCell(0);
cell.setCellValue(newContent.get(i++));
}
}
FileOutputStream outFile = new FileOutputStream(file);
excelBook.write(outFile);
// outFile.flush();
outFile.close();
} catch (Exception ex) {
/*
* JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage(),
* "Warning!", JOptionPane.ERROR_MESSAGE);
*/
ex.printStackTrace();
}
}
public static void main(String args[]) {
outputFileName = "demo4.xlsx";
List<String> newContent = new ArrayList<>();
newContent.add("Test Val1");
newContent.add("Test Val2");
newContent.add("Test Val3");
WriteExcel test = new WriteExcel();
test.overwriteExcel(new File(outputFileName), newContent);
}