geoinfo_eGov_work/src/main/java/geoinfo/util/ExcelUtil.java

223 lines
6.6 KiB
Java
Raw Normal View History

2024-02-08 02:26:11 +00:00
package geoinfo.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import org.jfree.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import geoinfo.util.RsWrapper;
public class ExcelUtil {
private static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
public static RsWrapper getExcelSheetName(String excelPath) throws Exception {
RsWrapper rsWp = new RsWrapper();
FileInputStream fis = null;
try {
Workbook wb = Workbook.getWorkbook(new File(excelPath));
int sheetCnt = wb.getNumberOfSheets();
for(short i=0; i < sheetCnt; i++) {
String sheetName = wb.getSheet(i).getName();
Box obox = new Box();
obox.put("sheetNo", i);
obox.put("sheetNm", sheetName);
//System.out.println("sheetName:"+sheetName + ", i:"+i);
rsWp.appendRs(obox);
}
return rsWp;
} catch(IndexOutOfBoundsException ex) {
logger.debug("error", ex);
return null;
} catch(IOException ex) {
logger.debug("error", ex);
return null;
} catch(NumberFormatException ex) {
logger.debug("error", ex);
return null;
} catch(Exception ex) {
logger.debug("error", ex);
throw ex;
} finally {
if (fis != null) { fis.close(); }
}
}
public static String[] getExcelSheetNameArray(String excelPath) throws Exception {
RsWrapper rsWp = new RsWrapper();
FileInputStream fis = null;
try {
Workbook wb = Workbook.getWorkbook(new File(excelPath));
int sheetCnt = wb.getNumberOfSheets();
return wb.getSheetNames();
} catch(IndexOutOfBoundsException ex) {
logger.debug("error", ex);
return null;
} catch(IOException ex) {
logger.debug("error", ex);
return null;
} catch(NumberFormatException ex) {
logger.debug("error", ex);
return null;
} catch(Exception ex) {
logger.debug("error", ex);
throw ex;
} finally {
if (fis != null) { fis.close(); }
}
}
public static Sheet getExcelWorkSheet(String excelPath, int sheetNo) throws Exception {
FileInputStream fis = null;
try {
excelPath.replaceAll("\\.", "").replaceAll("/", "").replaceAll("\\\\", "").replaceAll ("&","");
Workbook wb = Workbook.getWorkbook(new File(excelPath));
return wb.getSheet(sheetNo);
} catch(IndexOutOfBoundsException ex) {
logger.debug("error", ex);
return null;
} catch(IOException ex) {
logger.debug("error", ex);
return null;
} catch(NumberFormatException ex) {
logger.debug("error", ex);
return null;
} catch(Exception ex) {
logger.debug("error", ex);
throw ex;
} finally {
if (fis != null) { fis.close(); }
}
}
public static Sheet getExcelWorkSheet(String excelPath, String sheetName) throws Exception {
FileInputStream fis = null;
try {
excelPath.replaceAll("\\.", "").replaceAll("/", "").replaceAll("\\\\", "").replaceAll ("&","");
Workbook wb = Workbook.getWorkbook(new File(excelPath));
return wb.getSheet(sheetName);
} catch(IndexOutOfBoundsException ex) {
logger.debug("error", ex);
return null;
} catch(IOException ex) {
logger.debug("error", ex);
return null;
} catch(NumberFormatException ex) {
logger.debug("error", ex);
return null;
} catch(Exception ex) {
logger.debug("error", ex);
throw ex;
} finally {
if (fis != null) { fis.close(); }
}
}
//1
public static RsWrapper getRsWp(String excelPath, int sheetNo, int colNmRow, int startRow, int maxCol) throws Exception {
RsWrapper rsWp = new RsWrapper();
Sheet exlSheet = getExcelWorkSheet(excelPath, sheetNo);
if(exlSheet == null) { return rsWp; }
Box hbox = new Box();
for(short i=0; i < exlSheet.getColumns() && i < maxCol; i++) {
Cell cell = exlSheet.getCell(i,colNmRow);
if(cell == null) { break; }
hbox.put(i+"", cell.getContents());
}
//System.out.println("exlSheet.getRows():" + exlSheet.getRows());
for(int i=startRow; i < exlSheet.getRows(); i++) {
Cell[] cells = exlSheet.getRow(i);
if(cells == null) { break; }
boolean isOk = false;
for(short j=0; j < maxCol && j < cells.length; j++) {
if(cells[j] != null && cells[j].getContents() != null && !cells[j].getContents().trim().equals("")) { isOk = true; break; }
}
if(!isOk) { break; }
Box obox = new Box();
for(short j=0; j < hbox.size() && j < cells.length; j++) {
if(cells[j] == null || cells[j].getContents().trim().equals("")) { continue; }
if(cells[j].getType().toString().equals("Date")) {
String val = getDateVal(cells[j]);
obox.put("col"+j, val);
}else {
obox.put("col"+j, cells[j].getContents().trim());
}
}
rsWp.appendRs(obox);
}
return rsWp;
}
//2
public static RsWrapper getRsWp(String excelPath, String sheetName, int colNmRow, int startRow, int maxCol) throws Exception {
RsWrapper rsWp = new RsWrapper();
Sheet exlSheet = getExcelWorkSheet(excelPath, sheetName);
if(exlSheet == null) { return rsWp; }
Box hbox = new Box();
for(short i=0; i < exlSheet.getColumns() && i < maxCol; i++) {
Cell cell = exlSheet.getCell(i,colNmRow);
if(cell == null) { break; }
hbox.put(i+"", cell.getContents());
}
System.out.println("exlSheet.getRows(): " + exlSheet.getRows());
for(int i=startRow; i < exlSheet.getRows(); i++) {
Cell[] cells = exlSheet.getRow(i);
if(cells == null) { break; }
boolean isOk = false;
for(short j=0; j < maxCol && j < cells.length; j++) {
if(cells[j] != null && cells[j].getContents() != null && !cells[j].getContents().trim().equals("")) { isOk = true; break; }
}
if(!isOk) { break; }
Box obox = new Box();
for(short j=0; j < hbox.size() && j < cells.length; j++) {
if(cells[j] == null || cells[j].getContents().trim().equals("")) { continue; }
if(cells[j].getType().toString().equals("Date")) {
String val = getDateVal(cells[j]);
obox.put("col"+j, val);
}else {
obox.put("col"+j, cells[j].getContents().trim());
}
}
rsWp.appendRs(obox);
}
return rsWp;
}
public static RsWrapper getRsWp(String excelPath, int sheetNo, int colNmRow, int startRow) throws Exception {
return getRsWp(excelPath, sheetNo, colNmRow, startRow, 10);
}
public static RsWrapper getRsWp(String excelPath, String sheetName, int colNmRow, int startRow) throws Exception {
return getRsWp(excelPath, sheetName, colNmRow, startRow, 10);
}
public static String getDateVal(Cell cell) {
String rtn = "";
String val = cell.getContents();
val = val.replaceAll("/", "");
val = val.replaceAll("-", "");
if (val.length() == 8) {
return val.substring(4, 8) + val.substring(2, 4) + val.substring(0, 2);
} else {
return val;
}
}
}