生活随笔
收集整理的這篇文章主要介紹了
java操作poi如何更改excel中的数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://www.myexception.cn/j2ee/17951.html
修改文件最后還需要通過IO流操作來保存更改,這其實(shí)是很關(guān)鍵的一步,你代碼里面沒有IO的關(guān)閉操作,導(dǎo)致了數(shù)據(jù)的修改沒有保存
------解決方案--------------------------------------------------------
修改的只是內(nèi)存中的副本,還有自己主動(dòng)寫文件的,如
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 {/*** 只是一個(gè)demo,這里假設(shè)修改的值是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();//這里假設(shè)對(duì)應(yīng)單元格原來的類型也是String類型cell.setCellValue(value);System.out.println("單元格原來值為"+str1);System.out.println("單元格值被更新為"+value);fis.close();//關(guān)閉文件輸入流FileOutputStream fos=new FileOutputStream(exlFile);workbook.write(fos);fos.close();//關(guān)閉文件輸出流}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文件進(jìn)行測試,2003格式的,不能2007File file=new File("resources/excel/stuInfo.xls");//下面嘗試更改第一行第一列的單元格的值UpdateExcel2003.updateExcel(file,0,0,0,"更改測試");}
}
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的java操作poi如何更改excel中的数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。