Java读写Excel之POI超入门 http://rensanning.iteye.com/blog/1538591
java的poi技术读取和导入Excel [非2010]http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html
可以根据这个修改成2010的,里面有读取和创建
Apache POI组件操作Excel,制作报表(二) http://zjkilly.iteye.com/blog/870309
java 操作 excel 2010 http://blog.csdn.net/dallas16/article/details/6954264
poi excel 获取列名 http://skying007.iteye.com/blog/1680436
Excel导入异常Cannot get a text value from a numeric cell解决 http://blog.csdn.net/ysughw/article/details/9288307
POI颜色索引 http://www.cnblogs.com/xy2401/p/3295965.html
java的poi技术读取和导入Excel [非2010]http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html
可以根据这个修改成2010的,里面有读取和创建
Apache POI组件操作Excel,制作报表(二) http://zjkilly.iteye.com/blog/870309
java 操作 excel 2010 http://blog.csdn.net/dallas16/article/details/6954264
poi excel 获取列名 http://skying007.iteye.com/blog/1680436
Excel导入异常Cannot get a text value from a numeric cell解决 http://blog.csdn.net/ysughw/article/details/9288307
POI颜色索引 http://www.cnblogs.com/xy2401/p/3295965.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
Xml代码 <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-beta2</version> <!--<version>3.9</version>--> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.1</version> </dependency> </dependencies> |
读取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
Java代码 package com.pandy.excel; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; import java.util.Map; /** * Created by pandy on 13-12-29. */ public class ExcelProcess { public void process(String path, Map<String, Object> paramters, int startRow, int endRow, int startCol, int endCol) { try { InputStream is = new FileInputStream("/mnt/E/a.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(is); // 循环工作表Sheet for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { XSSFSheet sheet = workbook.getSheetAt(numSheet); if (sheet == null) { continue; } // 循环行Row for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row == null) { continue; } int colNum = row.getLastCellNum(); for(int j=0; j<colNum; j++){ XSSFCell xh = row.getCell(j); if (xh != null){ row.getCell(j).setCellType(Cell.CELL_TYPE_STRING); System.out.print(xh.getStringCellValue()+" "); } } System.out.println("\n-----------------------------"); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ExcelProcess process = new ExcelProcess(); process.process(null, null,0,0,0,0); } } |
修改:
别人的提示:修改文件最后还需要通过IO流操作来保存更改,这其实是很关键的一步,你代码里面没有IO的关闭操作,导致了数据的修改没有保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Java代码 private void outExcel(String path){ try { InputStream is = new FileInputStream(path); XSSFWorkbook workbook = new XSSFWorkbook(is); //XSSFWorkbook workbook = new XSSFWorkbook(path); XSSFSheet sheet = workbook.createSheet("newSheet"); XSSFRow firstrow = sheet.createRow(0); XSSFCell[] firstcell = new XSSFCell[3]; String[] names = new String[]{"AAA","BBB","CCC"}; for (int j = 0; j < 3; j++) { firstcell[j] = firstrow.createCell(j); firstcell[j].setCellValue(names[j]); } is.close(); FileOutputStream fileOut = new FileOutputStream(path); workbook.write(fileOut); fileOut.close(); } catch (Exception e) { e.printStackTrace(); } } |