excel 데이터 가져오기
8422 단어 Excel
데이터 내보내기
@Override
public void exportExcelData(File file, PageIdxPagetion pagetion,
Map<String, Object> searchParam) {
List<ProdVo> list = null;
Workbook workbook;
try {
workbook = WorkbookFactory.create(FileUtils.openInputStream(file));
do {
list = prodVoService.list(searchParam, pagetion);
Sheet sheet = workbook.createSheet((pagetion.getItemIdx() + 1)
+ "-" + pagetion.getNextPageItemIdx());
Row row = sheet.createRow((int) 0);
Cell cell = row.createCell(0);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); //
style.setBorderBottom((short) 1);
style.setBorderRight((short) 1);
String[] header = new String[] { "ID", " ", " ", "MCC ",
" ", " ", " ", " 0: 1. " };
sheet.setColumnWidth(0, 30 * 256);
sheet.setColumnWidth(1, 50 * 256);
sheet.setColumnWidth(2, 50 * 256);
sheet.setColumnWidth(7, 30 * 256);
for (int j = 0; j < header.length; j++) {
cell = row.createCell((short) j);
cell.setCellValue(header[j]);
cell.setCellStyle(style);
}
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
ProdVo prodVo = (ProdVo) list.get(i);
// ,
cell = row.createCell((short) 0);
cell.setCellValue(prodVo.getId().toString());
cell = row.createCell((short) 1);
cell.setCellValue(prodVo.getCompName());
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue(prodVo.getShopName());
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue(prodVo.getMcc());
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue(prodVo.getChannelNo());
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue(prodVo.getProdNo());
cell.setCellStyle(style);
cell = row.createCell((short) 6);
String mchProp = "";
if (null != prodVo.getMchProp()) {
if (prodVo.getMchProp().ordinal() == 1) {
mchProp = " ";
} else if (prodVo.getMchProp().ordinal() == 2) {
mchProp = " ";
} else if (prodVo.getMchProp().ordinal() == 3) {
mchProp = " ";
} else if (prodVo.getMchProp().ordinal() == 4) {
mchProp = " ";
}
}
cell.setCellValue(mchProp);
cell = row.createCell((short) 7);
cell.setCellValue("");
CellStyle style1 = workbook.createCellStyle();
style1.setLocked(false);
style1.setFillBackgroundColor(IndexedColors.YELLOW
.getIndex());
cell.setCellStyle(style1);
}
sheet.protectSheet("123");
pagetion.setPageIdx(pagetion.getPageIdx() + 1);
} while (pagetion.getPageIdx() <= pagetion.getTotalPage());
FileOutputStream fout = FileUtils.openOutputStream(file);
workbook.write(fout);
fout.close();
logger.info(" " + file.getName());
} catch (EncryptedDocumentException e) {
logger.error(" " + e.getMessage());
} catch (InvalidFormatException e) {
logger.error(" " + e.getMessage());
} catch (IOException e) {
logger.error(" " + e.getMessage());
}
}
인용하다
xls/xlsx 파일 가져오기
@Override
public Boolean importExcel(String fileName) {
Workbook workbook = null;
File file = new File(localTmpPath + fileName);
logger.info(" " + file.getPath());
// , true
boolean flag = false;
try {
workbook = WorkbookFactory.create(FileUtils.openInputStream(file));
int st = workbook.getNumberOfSheets();
for (int i = 0; i < st; i++) {
Sheet sheet = workbook.getSheetAt(i);
Drawing drw = sheet.createDrawingPatriarch();
Iterator<Row> rows = sheet.rowIterator(); //
rows.next();//
while (rows.hasNext()) {
Row row = rows.next(); //
ProdVo prodVo = new ProdVo();
Cell cell = row.getCell(0);
prodVo.setId(UUID.fromString(cell.getStringCellValue()));
Cell cell7 = row.getCell(7);
int flagValue = 0;
if (cell7.getCellType() == Cell.CELL_TYPE_STRING) {
if (StringUtils.isBlank(cell7.getStringCellValue())) {
createComment(drw, cell7, " ");
flag = true;
} else {
try {
flagValue = Integer.parseInt(cell7
.getStringCellValue());
} catch (Exception e) {
createComment(drw, cell7, " ");
flag = true;
}
}
} else if (cell7.getCellType() == Cell.CELL_TYPE_NUMERIC) {
double value = cell7.getNumericCellValue();
flagValue = (int) value;
}
if (!flag) {
prodVo.setOnlineFlag(flagValue);
prodVoService.modify(prodVo);
}
}
}
FileOutputStream fout = FileUtils.openOutputStream(file);
workbook.write(fout);
fout.close();
} catch (Exception e) {
logger.error(" " + fileName);
}
return flag;
}
private void createComment(Drawing drw, Cell cell, String string) {
Comment comment = drw.createCellComment(drw.createAnchor(0, 0, 0, 0,
cell.getColumnIndex() + 1, cell.getRowIndex(),
cell.getColumnIndex() + 2, cell.getRowIndex() + 1));
CreationHelper ch = cell.getSheet().getWorkbook().getCreationHelper();
comment.setString(ch.createRichTextString(string));
comment.setAuthor("admin");
cell.setCellComment(comment);
}
인용하다
공통 셀 포맷 값 가져오기
private static String getCellContent(Cell cell) {
return FORMATTER.formatCellValue(cell);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Excel Grep toolExcel Grep tool ■히나가타 ■ 시트 구성 ExcelGrep.cls...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.