[b][color=red]Java读写Excel之POI超入门[/color][/b] [url]http://rensanning.iteye.com/blog/1538591[/url]
[color=blue]可以根据这个修改成2010的,里面有读取和创建[/color]
Apache POI组件操作Excel,制作报表(二) [url]http://zjkilly.iteye.com/blog/870309[/url]
poi excel 获取列名 [url]http://skying007.iteye.com/blog/1680436[/url]
Excel导入异常Cannot get a text value from a numeric cell
<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>
读取:
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);
}
}
修改:
别人的提示:[color=darkblue]修改文件最后还需要通过IO流操作来保存更改,这其实是很关键的一步,你代码里面没有IO的关闭操作,导致了数据的修改没有保存[/color]
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();
}
}