无法使用Apache POI再次写入xls文件

时间:2016-10-25 05:51:05

标签: java apache-poi

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class WriteToExcel 
{
        public static int rowCount=0;
        public static void main(String[] args) throws IOException
        {
            File xlsFile =new File("C:\\Users\\Atul.Patel_New\\Desktop\\IDFC_MerchantOnBoarding_DocDwnld\\Reports\\Test.xls");
            FileInputStream lFin = null;
            HSSFWorkbook lWorkBook = null;
            HSSFSheet lWorkSheet = null;
            FileOutputStream lFout = null;
            //POIFSFileSystem lPOIfs = null;
            HSSFRow lRow=null;
            int columnCount=0;

            if(xlsFile.exists())
            {
                System.out.println("In Exists");
                lFout=new FileOutputStream(xlsFile,true);
                lFin=new FileInputStream(xlsFile);
                //lPOIfs = new POIFSFileSystem(lFin);
                lWorkBook=new HSSFWorkbook(lFin);
                lWorkSheet = lWorkBook.getSheet("Java Books");
                System.out.println(lWorkSheet.toString());
                System.out.println("lWorkSheet.getLastRowNum():"+lWorkSheet.getLastRowNum()+1);
                lRow = lWorkSheet.createRow(lWorkSheet.getLastRowNum());


            }
            else
            {
                xlsFile.createNewFile();
                lFout = new FileOutputStream(xlsFile,true);
                lWorkBook = new HSSFWorkbook();
                lWorkSheet = lWorkBook.createSheet("Java Books");
                System.out.println("Else: lWorkSheet.getLastRowNum():"+lWorkSheet.getLastRowNum());
                lRow = lWorkSheet.createRow(lWorkSheet.getLastRowNum());
                //columnCount = 0;
            }
            Object[][] bookData = 
                {
                    {"Head First Java", "Kathy Serria", 79},
                    {"Effective Java", "Joshua Bloch", 36},
                    {"Clean Code", "Robert martin", 42},
                    {"Thinking in Java", "Bruce Eckel", 35},
                };
            for (Object[] aBook : bookData)
            {
                Row row = lWorkSheet.createRow(rowCount);
                columnCount = 0;
                for (Object field : aBook) 
                {
                    Cell cell = row.createCell(columnCount);
                    if (field instanceof String)
                    {
                        cell.setCellValue((String) field);
                    } 
                    else if (field instanceof Integer) 
                    {
                        cell.setCellValue((Integer) field);
                    }
                    ++columnCount;

                }
                ++rowCount;
            }
            System.out.println("rowCount: "+rowCount);
             try
             {
                 lFin=new FileInputStream(xlsFile);
                 lFout=new FileOutputStream(xlsFile,true);
                 lWorkBook.write(lFout);
                 lFout.flush();
             }
             catch(Exception e)
             {
                e.printStackTrace();
             }
             finally
             {
                 lFout.close();
                 lFin.close();

             }
             System.out.println("Done");
        }
}

首次执行成功将数据写入xls。但是在执行第二次时,相同的数据出现在第一次执行期间写入的xls中。

1 个答案:

答案 0 :(得分:1)

我可以看到为什么你在第二次运行中写入相同数据的两个原因。

首先,我认为这是因为Object[][] bookData永远不会改变。 您将始终将以下数据写入excel文件:

{"Head First Java", "Kathy Serria", 79},
{"Effective Java", "Joshua Bloch", 36},
{"Clean Code", "Robert martin", 42},
{"Thinking in Java", "Bruce Eckel", 35}

其次,您还始终将rowCountcolumnCount重置为0,并且您的代码会在每次运行时覆盖相同的行和列。