반응형
1. ExcelReader.java
import java.io.File;
import java.io.InputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.SAXHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.StylesTable;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class ExcelReader {
public static void main(String[] args) {
// TODO Auto-generated method stub
//읽어올 엑셀파일 경로
SheetHandler excelData = readExcel(new File("C:\\work\\excel\\mysample.xlsx"));
System.out.println("Header List" + excelData.getHeader().size());
System.out.println("Rows List" + excelData.getRows().size());
}
public static SheetHandler readExcel(File excelFile) {
SheetHandler sheetHandler = new SheetHandler();
try {
OPCPackage pkg = OPCPackage.open(excelFile);
XSSFReader xssfReader = new XSSFReader(pkg);
ReadOnlySharedStringsTable data = new ReadOnlySharedStringsTable(pkg);
StylesTable styles = xssfReader.getStylesTable();
InputStream sheetStream = xssfReader.getSheetsData().next();
InputSource sheetSource = new InputSource(sheetStream);
ContentHandler handler = new XSSFSheetXMLHandler(styles, data, sheetHandler, false);
XMLReader sheetParser = SAXHelper.newXMLReader();
sheetParser.setContentHandler(handle
sheetParser.parse(sheetSource);
sheetStream.close();
}catch (Exception e) {
throw new RuntimeException(e);
}
return sheetHandler;
}
}
2. SheetHandler.java
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
public class SheetHandler implements SheetContentsHandler {
private List<List<String>> rows = new ArrayList<>();
private List<String> row = new ArrayList<>();
private List<String> header = new ArrayList<>();
private int currentCol = -1;
private int currRowNum = 0;
public List<String> getHeader() {
return header;
}
public List<List<String>> getRows() {
return rows;
}
public void startRow(int rowNum) {
this.currentCol = -1;
this.currRowNum = rowNum;
}
public void endRow(int rowNum) {
if(rowNum ==0) {
header = new ArrayList(row);
} else {
if(row.size() < header.size()) {
for (int i = row.size(); i < header.size(); i++) {
row.add("");
}
}
rows.add(new ArrayList(row));
}
row.clear();
}
public void cell(String columnName, String value, XSSFComment var3) {
int iCol = (new CellReference(columnName)).getCol();
int emptyCol = iCol - currentCol - 1;
for(int i = 0 ; i < emptyCol ; i++) {
row.add("");
}
currentCol = iCol;
row.add(value);
}
public void headerFooter(String text, boolean isHeader, String tagName) {
}
}
반응형
'IT' 카테고리의 다른 글
webjars 못 불러 올때 처리방법 (0) | 2022.10.27 |
---|---|
[Spring 강좌]CSV 생성 후 다운로드 하기 (2) | 2022.10.24 |
[스프링 강좌] 파일 다운로드 처리 소스 (0) | 2022.10.23 |
쿠키를(cookie) 이용한 아이디 저장 (0) | 2022.10.22 |
[Spring 강좌]스케줄러 실행하기 (0) | 2022.10.21 |