java操作poi如何更改excel中的数据
来源:互联网 发布:java数组泛型转换 编辑:程序博客网 时间:2024/05/28 15:07
http://www.myexception.cn/j2ee/17951.html
修改文件最后还需要通过IO流操作来保存更改,这其实是很关键的一步,你代码里面没有IO的关闭操作,导致了数据的修改没有保存
------解决方案--------------------------------------------------------
修改的只是内存中的副本,还有自己主动写文件的,如
FileOutputStream stream;
stream = new FileOutputStream(new File(fileToBeRead ));
workbook.write(stream);
stream.close();
------解决方案--------------------------------------------------------
修改完cell的值 还需要保存一下excel
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格式的,不能2007 File file=new File("resources/excel/stuInfo.xls"); //下面尝试更改第一行第一列的单元格的值 UpdateExcel2003.updateExcel(file,0,0,0,"更改测试"); }}
- java操作poi如何更改excel中的数据
- 【JAVA】poi操作,excel读取数据。
- Java操作Excel Poi
- Java操作Excel Poi
- POI Java操作Excel
- java poi excel 操作
- java poi操作EXCEL
- Java操作Excel Poi
- java poi操作excel
- java POI操作excel
- java poi Excel操作
- java-poi-操作excel
- java poi操作Excel
- Java-poi操作Excel
- JAVA POI操作EXCEL
- POI对Excel操作——java读取Excel数据
- POI 把java项目中的数据信息导入到Excel中,详细操作
- 用Java POI操作Excel,读取数据导入DB2数据库
- 12个有效的提高编程技能的方法
- Javascript Select操作大集合
- 斯坦福研究减少trusted code的操作系统
- 理解和使用ICMP协议
- C#基础知识整理:基础知识(14) 数组
- java操作poi如何更改excel中的数据
- 服务器返回的各种HTTP状态码介绍
- iOS开发技巧5-获取系统字体
- Oracle不常用基础之union、union all、 Intersect、Minus
- boost xml
- uClinux console设置
- 通过COOKIE实现自动登录
- readonly和const的区别
- 实现数据驱动Coded UI Tests