日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java poi 更新excel_在Java Apache POI中更新现有的Excel文件

發布時間:2025/3/12 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java poi 更新excel_在Java Apache POI中更新现有的Excel文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在嘗試編寫一個每天運行的

Java程序(使用任務調度程序),并且每次運行時都會在Excel電子表格中附加一列.我遇到的問題是它只是重寫文件,而不是附加到它.我正在使用Apache POI,這里是相關代碼:

public static void toExcel(List results,List notActive)throws IOException{

try {

FileInputStream fIPS= new FileInputStream("test.xls"); //Read the spreadsheet that needs to be updated

HSSFWorkbook wb;

HSSFSheet worksheet;

if(fIPS.available()>=512) {

wb = new HSSFWorkbook(fIPS); //If there is already data in a workbook

worksheet = wb.getSheetAt(0);

}else{

wb = new HSSFWorkbook(); //if the workbook was just created

worksheet = wb.createSheet("Data");

}

//Access the worksheet,so that we can update / modify it

HSSFRow row1 = worksheet.createRow(0); //0 = row number

int i=0;

Cell c = row1.getCell(i);

while (!(c == null || c.getCellType() == Cell.CELL_TYPE_BLANK)) { //cell is empty

i++;

c=row1.getCell(i);

}

HSSFRow rowx;

int x=0;

for(String s : results) {

rowx = worksheet.createRow(x);

HSSFCell cellx = rowx.createCell(i); //0 = column number

cellx.setCellValue(s);

x++;

}

fIPS.close(); //Close the InputStream

FileOutputStream output_file =new FileOutputStream("test.xls");//Open FileOutputStream to write updates

wb.write(output_file); //write changes

output_file.close(); //close the stream

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

我認為你是一次又一次地創建新的行和單元格并導致重寫excel.

Essentially you need to get the rows and cells instead of creating

them in your program.

HSSFRow row1 = worksheet.createRow(0);

您可能需要獲取行而不是創建它.

HSSFRow row1 = worksheet.getRow(0);

這個小例子更新了第二行的第二個單元格:

//Read the spreadsheet that needs to be updated

FileInputStream fsIP= new FileInputStream(new File("C:\\Excel.xls"));

//Access the workbook

HSSFWorkbook wb = new HSSFWorkbook(fsIP);

//Access the worksheet,so that we can update / modify it.

HSSFSheet worksheet = wb.getSheetAt(0);

// declare a Cell object

Cell cell = null;

// Access the second cell in second row to update the value

cell = worksheet.getRow(1).getCell(1);

// Get current cell value value and overwrite the value

cell.setCellValue("OverRide existing value");

//Close the InputStream

fsIP.close();

//Open FileOutputStream to write updates

FileOutputStream output_file =new FileOutputStream(new File("C:\\Excel.xls"));

//write changes

wb.write(output_file);

//close the stream

output_file.close();

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的java poi 更新excel_在Java Apache POI中更新现有的Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。