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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java操作poi如何更改excel中的数据

發布時間:2024/4/17 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java操作poi如何更改excel中的数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.myexception.cn/j2ee/17951.html

修改文件最后還需要通過IO流操作來保存更改,這其實是很關鍵的一步,你代碼里面沒有IO的關閉操作,導致了數據的修改沒有保存
------解決方案--------------------------------------------------------
修改的只是內存中的副本,還有自己主動寫文件的,如
FileOutputStream stream;
stream = new FileOutputStream(new File(fileToBeRead ));
workbook.write(stream);
stream.close();

------解決方案--------------------------------------------------------
修改完cell的值 還需要保存一下excel

Java code package poi.excel;import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFCell;import java.io.*; import java.util.Date; import java.sql.Timestamp; import java.text.DecimalFormat;/*** Created by IntelliJ IDEA.* User: admin* Date: 2011-10-10* Time: 16:10:29* To change this template use File | Settings | File Templates.*/ public class UpdateExcel2003 {/*** 只是一個demo,這里假設修改的值是String類型* @param exlFile* @param sheetIndex* @param col* @param row* @param value* @throws Exception*/public static void updateExcel(File exlFile,int sheetIndex,int col,int row,String value)throws Exception{FileInputStream fis=new FileInputStream(exlFile);HSSFWorkbook workbook=new HSSFWorkbook(fis); // workbook.HSSFSheet sheet=workbook.getSheetAt(sheetIndex);HSSFRow r=sheet.getRow(row);HSSFCell cell=r.getCell(col); // int type=cell.getCellType();String str1=cell.getStringCellValue();//這里假設對應單元格原來的類型也是String類型cell.setCellValue(value);System.out.println("單元格原來值為"+str1);System.out.println("單元格值被更新為"+value);fis.close();//關閉文件輸入流FileOutputStream fos=new FileOutputStream(exlFile);workbook.write(fos);fos.close();//關閉文件輸出流}private String getCellValue(HSSFCell cell) {String cellValue = "";DecimalFormat df = new DecimalFormat("#");switch (cell.getCellType()) {case XSSFCell.CELL_TYPE_STRING:cellValue = cell.getRichStringCellValue().getString().trim();break;case XSSFCell.CELL_TYPE_NUMERIC:cellValue = df.format(cell.getNumericCellValue()).toString();break;case XSSFCell.CELL_TYPE_BOOLEAN:cellValue = String.valueOf(cell.getBooleanCellValue()).trim();break;case XSSFCell.CELL_TYPE_FORMULA:cellValue = cell.getCellFormula();break;default:cellValue = "";}return cellValue;}/*** @param args*/public static void main(String[] args) throws Exception{// TODO Auto-generated method stub// 下面改成你自己的xls文件進行測試,2003格式的,不能2007File file=new File("resources/excel/stuInfo.xls");//下面嘗試更改第一行第一列的單元格的值UpdateExcel2003.updateExcel(file,0,0,0,"更改測試");} } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的java操作poi如何更改excel中的数据的全部內容,希望文章能夠幫你解決所遇到的問題。

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