package com.pingan.wiseapm.web;java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;web
public class ExcelManage {apache
private HSSFWorkbook workbook = null;測試
public boolean fileExist(String filePath) {.net
boolean flag = false;
File file = new File(filePath);
flag = file.exists();
return flag;
}excel
public boolean sheetExist(String filePath, String sheetName) {
boolean flag = false;
File file = new File(filePath);
if (file.exists()) {
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
// 添加Worksheet(不添加sheet時生成的xls文件打開時會報錯)
HSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet != null)
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
} else { // 文件不存在
flag = false;
}
return flag;
}對象
public void createSheet(String filePath, String sheetName, String titleRow[]) throws FileNotFoundException, IOException {
FileOutputStream out = null;
File excel = new File(filePath); // 讀取文件
FileInputStream in = new FileInputStream(excel); // 轉換爲流
workbook = new HSSFWorkbook(in); // 加載excel的 工做目錄索引
workbook.createSheet(sheetName); // 添加一個新的sheet
// 添加表頭
Row row = workbook.getSheet(sheetName).createRow(0); // 建立第一行
try {
for (int i = 0; i < titleRow.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(titleRow[i]);
}
out = new FileOutputStream(filePath);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}ci
/**
* 建立新excel.
*
* @param filePath
* excel的路徑
* @param sheetName
* 要建立的表格索引
* @param titleRow
* excel的第一行即表格頭
*/
public void createExcel(String filePath, String sheetName, String titleRow[]) {
// 建立workbook
workbook = new HSSFWorkbook();
// 添加Worksheet(不添加sheet時生成的xls文件打開時會報錯)
workbook.createSheet(sheetName);
// 新建文件
FileOutputStream out = null;
try {
// 添加表頭
Row row = workbook.getSheet(sheetName).createRow(0); // 建立第一行
for (int i = 0; i < titleRow.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(titleRow[i]);
}
out = new FileOutputStream(filePath);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}get
/**
* 往excel中寫入.
*
* @param filePath
* 文件路徑
* @param sheetName
* 表格索引
* @param object
*/
public void writeToExcel(String filePath, String sheetName, Object object, String titleRow[]) {
// 建立workbook
File file = new File(filePath);
try {
workbook = new HSSFWorkbook(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = null;
HSSFSheet sheet = workbook.getSheet(sheetName);
// 獲取表格的總行數
int rowCount = sheet.getLastRowNum() + 1; // 須要加一
try {
Row row = sheet.createRow(rowCount); // 最新要添加的一行
// 經過反射得到object的字段,對應表頭插入
// 獲取該對象的class對象
Class<? extends Object> class_ = object.getClass();
for (int i = 0; i < titleRow.length; i++) {
String title = titleRow[i];
String UTitle = Character.toUpperCase(title.charAt(0)) + title.substring(1, title.length()); // 使其首字母大寫;
String methodName = "get" + UTitle;
Method method = class_.getDeclaredMethod(methodName); // 設置要執行的方法
String data = method.invoke(object).toString(); // 執行該get方法,即要插入的數據
Cell cell = row.createCell(i);
cell.setCellValue(data);
}
out = new FileOutputStream(filePath);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String filePath = "fileList/board1.xlsx";
String sheetName = "測試";
// Excel文件易車sheet頁的第一行
String title[] = { "日期", "城市", "新發布車源數" };
// Excel文件易車每一列對應的數據
String titleDate[] = { "date", "city", "newPublish" };
ExcelManage em = new ExcelManage();
// 判斷該名稱的文件是否存在
boolean fileFlag = em.fileExist(filePath);
if (!fileFlag) {
em.createExcel(filePath, sheetName, title);
}
// 判斷該名稱的Sheet是否存在
boolean sheetFlag = em.sheetExist(filePath, sheetName);
// 若是該名稱的Sheet不存在,則新建一個新的Sheet
if (!sheetFlag) {
try {
em.createSheet(filePath, sheetName, title);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
YiCheData user = new YiCheData();
user.setDate("206-12-21");
user.setCity("北京");
user.setNewPublish("5");
// 寫入到excel
em.writeToExcel(filePath, sheetName, user, titleDate);
}
}