我编写了一个方法“writeData”,通过 apache poi 将现有 excel 文件中的列数据写入新的 excel 文件。它运作良好。但是当我再次尝试调用此方法时,之前的数据丢失了。为什么?有人可以帮助我吗?
public class ReadData {
/**
* @param args
*/
Workbook wb, wb1;
Sheet sheet, sheet1;
Row row;
Row row1;
static int cell;
Cell cell1;
static ArrayList<Cell> list1 = new ArrayList<Cell>();
static ArrayList<Cell> list2 = new ArrayList<Cell>();
public static ArrayList<Cell> readData(String filename, int cellNum, ArrayList array){
try{
InputStream inp = new FileInputStream(filename);
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
for (int j=sheet.getLastRowNum(); j>0; j--) {
Row row = sheet.getRow(j);
Cell cell = row.getCell(cellNum);
array.add(cell);
System.out.println(cell);
}
}catch(Exception e){
System.out.println("Wrong!" + e.getMessage());
e.getStackTrace();
System.out.println(e.getStackTrace());
}
return array;
}
public static void writeData(int cellNo, ArrayList<Cell> array){
Workbook wb1 = new HSSFWorkbook();
FileOutputStream fos = null;
try{
fos = new FileOutputStream("3.xls");
Sheet sheet1 = wb1.createSheet("Data");
for(int i = 0; i < array.size(); i++){
Row row1 = sheet1.createRow(i);
Cell cell1 = row1.createCell(cellNo);
cell1.setCellValue(array.get(i).getNumericCellValue());
}
wb1.write(fos);
fos.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
readData("1.xls",0,list1);
writeData(0,list1);
readData("2.xls",0,list2);
writeData(1,list2);
}
}