Java 코드를 사용하여 내 응용 프로그램에서 Excel 파일을 읽었습니다. 모든 필드를 올바른 순서로 읽지 만 빈 셀이있을 때마다 이전 셀 레코드로 자동 채워집니다.
ServletContext servletContext = session.getServletContext();
File reportFile = new File(servletContext.getRealPath(sTempFile));
FileInputStream fIS = new FileInputStream(reportFile);
POIFSFileSystem theFile = new POIFSFileSystem(fIS);
m_workBook = new HSSFWorkbook(theFile);
HSSFSheet theSheet = m_workBook.getSheetAt(0);
//Take the first Sheet
int col = 12;
theSheet.setAutobreaks(true);
int row = theSheet.getPhysicalNumberOfRows() + 1;
String data[][] = new String[col][row];
int rowCount = 1;
while (rowCount < theSheet.getLastRowNum() + 1) {
//Inserts all the rows except the first_row
HSSFRow theSICDataRow = theSheet.getRow(rowCount++);
row = rowCount - 1;
HSSFCell cPaidAmount = null;
if (theSICDataRow.getCell((short) 11) != null) {
cPaidAmount = theSICDataRow.getCell((short) 11);
if (cPaidAmount.getCellType()== HSSFCell.CELL_TYPE_NUMERIC) {
BigDecimal bd = new BigDecimal(cPaidAmount.getNumericCellValue());
if (bd.intValue() == 0) {
sPaidAmount = "0";
}
else {
sPaidAmount = String.valueOf(bd.setScale(2, BigDecimal.ROUND_HALF_EVEN));
}
}
else if (cPaidAmount.getCellType()== HSSFCell.CELL_TYPE_STRING) {
sPaidAmount = "" + cPaidAmount.getStringCellValue();
}
}
else if (theSICDataRow.getCell((short) 11) == null) {
sPaidAmount = "";
}
출력은 ...
지불 금액
1200 1200 1200 1200 1690 .. .. .. .. .. ..
하지만 나타납니다
지불 금액
1200 1200 1200 1200 1690 1690 1690 1690 1690 1690 ..
소스 및 생성 된 임시 파일은 정상이지만 테이블의 출력 표시가 잘못되었습니다. 몇 가지 해결책을 제공하십시오.
마침내 나는 내 대답을 얻었고 그것을 공유하고 싶습니다. 내 코드에서 빈 셀을 처리하지 않았으므로 POIFSFileSystem이 빈 셀을 이전 레코드로 자동 채 웁니다. 여기 내 코드가 있습니다.
HSSFCell cPaidAmount = null;
if (theSICDataRow.getCell((short) 11) != null) {
cPaidAmount = theSICDataRow.getCell((short) 11);
if (cPaidAmount.getCellType()== HSSFCell.CELL_TYPE_NUMERIC) {
BigDecimal bd = new BigDecimal(cPaidAmount.getNumericCellValue());
if (bd.intValue() == 0) {
sPaidAmount = "0";
}
else {
sPaidAmount = String.valueOf(bd.setScale(2, BigDecimal.ROUND_HALF_EVEN));
}
}
else if (cPaidAmount.getCellType()== HSSFCell.CELL_TYPE_STRING) {
sPaidAmount = "" + cPaidAmount.getStringCellValue();
}
else if (cPaidAmount.getCellType()== HSSFCell.CELL_TYPE_BLANK) {
sPaidAmount = "0";
}
}
else if (theSICDataRow.getCell((short) 11) == null) {
sPaidAmount = "0";
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다