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("數據導出成功");
}
```