달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

 

 

 

 

 

ExcelRead.zip

 

 

// *.xls 파일 가능( - xlsx파일 불가)
HSSFWorkbook workBook  = new HSSFWorkbook(new FileInputStream(new File(filePath)));
HSSFSheet sheet = null;
HSSFRow row  = null;
HSSFCell cell   null;

 

// *.xlsx 파일 가능( - xls파일 불가)
XSSFWorkbook workBook   = new XSSFWorkbook(file);
XSSFSheet sheet = null;
XSSFRow row  = null;
XSSFCell cell = null;

 

// *.xls, *.xlsx 파일 가능
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = null;
Row   row  = null;
Cell  cell  = null;

 

 


[출처] [Java] POI를 이용한 (*.xls, *.xlsx) 읽기|작성자 마이레몬트리

http://tychejin.blog.me/110182191752

 

Posted by 타카스 류지
|

 /**
  * xlsx 파일(XSSFWorkbook)을 xls 파일(HSSFWorkbook)로 변환하는 메서드.
  * [참조] http://stackoverflow.com/questions/20049922/java-poi-api-convert-from-xlsx-to-xls 
  *
  * @param xssfWorkbook
  * @return
  * @throws Exception
  */
 public static HSSFWorkbook convertXlsxToXls(String filePath) throws Exception {
  
  FileInputStream fis = new FileInputStream(new File(filePath)); 
  XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis);

     HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
     int sheetCnt = xssfWorkbook.getNumberOfSheets();
     for (int i = 0; i < sheetCnt; i++) {
      Sheet sIn = xssfWorkbook.getSheetAt(i);
      Sheet sIn = xssfWorkbook.getSheetAt(0);
         Sheet sOut = hssfWorkbook.createSheet(sIn.getSheetName());
         Iterator rowIt = sIn.rowIterator();
         while (rowIt.hasNext()) {
             Row rowIn = (Row) rowIt.next();
             Row rowOut = sOut.createRow(rowIn.getRowNum());
 
             Iterator cellIt = rowIn.cellIterator();
             while (cellIt.hasNext()) {
                 Cell cellIn = (Cell) cellIt.next();
                 Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType());
                 switch (cellIn.getCellType()) {
                 case Cell.CELL_TYPE_BLANK: break;
                 case Cell.CELL_TYPE_BOOLEAN:
                     cellOut.setCellValue(cellIn.getBooleanCellValue());
                     break;
                 case Cell.CELL_TYPE_ERROR:
                     cellOut.setCellValue(cellIn.getErrorCellValue());
                     break;
                 case Cell.CELL_TYPE_FORMULA:
                     cellOut.setCellFormula(cellIn.getCellFormula());
                     break;
                 case Cell.CELL_TYPE_NUMERIC:
                     cellOut.setCellValue(cellIn.getNumericCellValue());
                     break;
                 case Cell.CELL_TYPE_STRING:
                     cellOut.setCellValue(cellIn.getStringCellValue());
                     break;
                 }
                 CellStyle styleIn = cellIn.getCellStyle();
                 CellStyle styleOut = cellOut.getCellStyle();
                 styleOut.setDataFormat(styleIn.getDataFormat());
                 cellOut.setCellComment(cellIn.getCellComment());
             }
         }
        }
    
     return hssfWorkbook;
 }

Posted by 타카스 류지
|