POI 작업 Excel 문서(2)

이 글은 인터넷에서 왔습니다.
// load   
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));  
HSSFWorkbook wb = new HSSFWorkbook(fs);  
for (int i = 0; i < wb.getNumberOfSheets(); i++) {  
    HSSFSheet sheet = wb.getSheetAt(i);  
    for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i ++) {  
    HSSFRow row = sheet.getRow(i);  
            if (row != null) {  
        。。。 }  
       }  
     }  
//    
FileOutputStream fos = new FileOutputStream(objectPath);  
//   
swb.write(fos);  
fos.close();  

2, 열 및 셀 얻기
HSSFRow row = sheet.getRow(i);  
HSSFCell cell = row.getCell((short) j);  

3. sheet 이름과 칸 내용을 중국어로 설정
wb.setSheetName(n, " ",HSSFCell.ENCODING_UTF_16);      
cell.setEncoding((short) 1);  
cell.setCellValue(" ");  

4. 셀 내용이 공식이나 수치가 아니므로 이렇게 셀을 읽을 수 있습니다.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  cell.getNumericCellValue() 5, 열 너비, 행 높이 설정
sheet.setColumnWidth((short)column,(short)width);  
row.setHeight((short)height); 

6. 영역 추가, 셀 병합
Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo,(short)columnTo);  
sheet.addMergedRegion(region);  
//   
sheet.getNumMergedRegions()  

7. 일반적인 방법은 칸의 다른 속성에 따라 문자열 수치를 되돌려준다
public String getCellStringValue(HSSFCell cell) {  
        String cellValue = "";  
        switch (cell.getCellType()) {  
        case HSSFCell.CELL_TYPE_STRING:  
            cellValue = cell.getStringCellValue();  
            if(cellValue.trim().equals("")||cellValue.trim().length()<=0)  
                cellValue=" ";  
            break;  
        case HSSFCell.CELL_TYPE_NUMERIC:  
            cellValue = String.valueOf(cell.getNumericCellValue());  
            break;  
        case HSSFCell.CELL_TYPE_FORMULA:  
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);  
            cellValue = String.valueOf(cell.getNumericCellValue());  
            break;  
        case HSSFCell.CELL_TYPE_BLANK:  
            cellValue=" ";  
            break;  
        case HSSFCell.CELL_TYPE_BOOLEAN:  
            break;  
        case HSSFCell.CELL_TYPE_ERROR:  
            break;  
        default:  
            break;  
        }  
        return cellValue;  
    }  

8, 일반적인 셀 테두리 형식인 점선 HSSFCellStyle.BORDER_DOTTED 실선 HSSFCellStyle.BORDER_THIN
public static HSSFCellStyle getCellStyle(short type)  
    {     
       HSSFWorkbook wb = new HSSFWorkbook();  
       HSSFCellStyle style = wb.createCellStyle();  
       style.setBorderBottom(type);//    
        style.setBorderLeft(type);//    
        style.setBorderRight(type);//    
        style.setBorderTop(type);//    
       return style;  
    }  

9. 글꼴 및 내용 위치 설정
 
HSSFFont f  = wb.createFont();  
f.setFontHeightInPoints((short) 11);//   
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//   
style.setFont(f);  
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//   
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//   
style.setRotation(short rotation);//   
HSSFDataFormat df = wb.createDataFormat();  
style1.setDataFormat(df.getFormat("0.00%"));//   
cell.setCellFormula(string);//   
style.setRotation(short rotation);//   
cell.setCellStyle(style);  

10. 사진 포럼에 삽입하여 본
// ByteArrayOutputStream , ByteArray  
      ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
      BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));  
      ImageIO.write(bufferImg,"jpg",byteArrayOut);  
// excel   
FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");   
fs = new POIFSFileSystem(fos);  
//   
HSSFWorkbook wb = new HSSFWorkbook(fs);  
HSSFSheet sheet = wb.getSheetAt(0);  
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();  
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);       
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPE

좋은 웹페이지 즐겨찾기