EXCEL2007 버전 및 그 이상의 일반적인 작업에 대한 POI 요약
6155 단어 poi-excel
/**
* Excel
*
* @param headList
* @return
*/
public static XSSFWorkbook createWorkBook(List headList) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(" ");
// excel
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFDataFormat dataFormat = wb.createDataFormat();
cellStyle.setDataFormat(dataFormat.getFormat("@"));
for(int column=0;column<=headList.size();column++){
sheet.setDefaultColumnStyle(column,cellStyle);
}
buildHeader(sheet, headList);
return wb;
}
/**
*
*
* @param sheet
*/
private static void buildHeader(XSSFSheet sheet, List headList) {
XSSFRow headerRow = sheet.createRow(0);
int index = 0;
for (String header : headList) {
XSSFCell cell = headerRow.createCell(index, Cell.CELL_TYPE_STRING);
cell.setCellValue(header);
index++;
}
}
현재 셀 내용 가져오기
/**
*
*/
public static String getCellValue(Cell cell) {
String value = "";
if(cell!=null){
cell.setCellType(Cell.CELL_TYPE_STRING);
value = cell.getStringCellValue();
}
return value;
}
어떤 줄의 모든 칸에 값이 있는지 판단하다
/**
*
*
* @param row
* @param cells
* @return
*/
private static boolean isThereAValue(XSSFRow row, int cells) {
for (int i = 0; i < cells; i++) {
XSSFCell rowcell = row.getCell(i);
String cellValue = ExcelUtils.getCellValue(rowcell);
if (null != cellValue && !"".equals(cellValue)) {
return true;
}
}
return false;
}
EXCEL에서 빈 행이 아닌 행 수 가져오기
/**
* excel
*
* @param sheet
* @param lastRows sheet
* @return
*/
public static int getExcelRows(XSSFSheet sheet, int lastRows) {
boolean isThereAValue;
int rows = 0;
for (int i = 0; i <=lastRows; i++) {
XSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
int cells = sheet.getRow(0).getLastCellNum();
isThereAValue = ExcelUtils.isThereAValue(row, cells);
if (!isThereAValue) {
continue;
}
rows++;
}
return rows;
}
첫 번째 열 제목 표시줄 데이터 가져오기
/**
*
*
* @param sheet
* @return map key: (0 ) value
* @throws ExportException
*/
private static Map loadHeader(XSSFSheet sheet) {
Map header = new HashMap();
int HEADER = 0;
XSSFRow row = sheet.getRow(HEADER);
int columns = row.getLastCellNum();
for (int i = 0; i < columns; i++) {
log.info(" :" + row.getCell(i).getStringCellValue());
String value = row.getCell(i).getStringCellValue();
if (null == value) {
throw new RuntimeException(" !");
}
header.put(i, value);
}
return header;
}
확인 EXCEL 템플릿에서 Workbook의 값을 List > Fabric에 넣기
/**
* workbook List
POI 작업 EXCEL FAQ
for (int rowIndex = 1; rowIndex <= lastRowNum; rowIndex++
).