2015/12/21
今天的任务是读写excel,参考了这篇博客
用到了apache的poi解析:
HSSFWorkbook 对应excel文件;
//读取方法。
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(InputStream is);
//创建方法
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
```
HSSFSheet 对应excel里的页;
````java
//读取方法。
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
//创建方法
HSSFSheet hssfSheet = hssfWorkbook.createSheet("sheetName");
```
HSSFRow 对应excel里的行;
````java
//读取方法。
/*getCell(int num);
*获取hssfRow里的cell对象。
*/
hssfRow.getCell(0);
//创建方法
HSSFCell hssfCell = row.createCell(int num);
```
HSSFCell 对应excel里具体的格子。
````java
//读取方法。
HSSFCell hssfCell = hssfRow.getCell(int num);
//创建方法
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
OutputStream out = new FileOutputStream("路径"); hssfWorkbook.write(out);
```
以下是poi中的常用方法
````java
/*write(OutputStream out)
*根据文件输出流,输出hssfWorkbook。
*/
OutputStream out = new FileOutputStream("路径"); hssfWorkbook.write(out);
/*getNumberOfSheets()
*获取hssfWorkbook里的页数。
*/
hssfWorkbook.getNumberOfSheets();
/*getLastRowNum();
*获取hssfSheet里的行数。
*/
hssfSheet.getLastRowNum();
/*判断hssfcell的属性
*
*/
hssfCell.getCellType();
/**
* Numeric Cell type (0)
* @see #setCellType(int)
* @see #getCellType()
*/
public final static int CELL_TYPE_NUMERIC = 0;
/**
* String Cell type (1)
*/
public final static int CELL_TYPE_STRING = 1;
/**
* Formula Cell type (2)
*/
public final static int CELL_TYPE_FORMULA = 2;
/**
* Blank Cell type (3)
*/
public final static int CELL_TYPE_BLANK = 3;
/**
* Boolean Cell type (4)
*/
public final static int CELL_TYPE_BOOLEAN = 4;
/**
* Error Cell type (5)
*/
public final static int CELL_TYPE_ERROR = 5;
```
这是读取Excel的部分
````java
public List<Student> readXls() throws IOException {
InputStream is = new FileInputStream(Common.EXCEL_PATH);//EXCEL_PATH存放路径
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
Student student = null;
List<Student> list = new ArrayList<Student>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row,第一行是列名,所以跳过
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
student = new Student();
HSSFCell no = hssfRow.getCell(0);
HSSFCell name = hssfRow.getCell(1);
HSSFCell lastName = hssfRow.getCell(2);
HSSFCell age = hssfRow.getCell(3);
HSSFCell score = hssfRow.getCell(4);
student.setNo(getValue(no));
student.setName(getValue(name)+" "+getValue(lastName));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
return list;
}
```
我们需要一个函数去判断excel里数据的类型
````java
private String getValue(HSSFCell hssfCell) {
if(hssfCell!= null){
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}else{
return "";
}
}
```
下面是导入excel的代码
````java
public static void writeExcel(List<Student> xls) throws Exception {
// 获取总列数
int CountColumnNum = xls.size();
// 创建Excel文档
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
Student student = null;
// sheet 对应一个工作页
HSSFSheet sheet = hssfWorkbook.createSheet("第一页");
HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "学号";
names[1] = "姓名";
names[2] = "年龄";
names[3] = "成绩";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
student = xls.get(i);
for (int colu = 0; colu <= 4; colu++) {
// 在一行内循环
HSSFCell no = row.createCell(0);
no.setCellValue(student.getNo());
HSSFCell name = row.createCell(1);
name.setCellValue(student.getName());
HSSFCell age = row.createCell(2);
age.setCellValue(student.getAge());
HSSFCell score = row.createCell(3);
score.setCellValue(student.getScore());
}
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream("d:/学生记录.xls");
hssfWorkbook.write(out);
out.close();
System.out.println("数据导出成功");
}
```