Poi 기반

26053 단어
①: excel에 물건을 출력
package poi; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo1 implements Runnable{ public static void main(String[] args) throws Exception { Demo1 demo1=new Demo1(); Thread thread=new Thread(demo1); thread.start(); } @Override public void run() { Workbook wb=new HSSFWorkbook(); FileOutputStream outputStream = null; try { outputStream = new FileOutputStream("E://poi.xls");  // 
        } catch (FileNotFoundException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } Sheet sheet=wb.createSheet();  // 
        Row row=sheet.createRow(0);   // ( )
        Cell cell1=row.createCell(0);  // ( )
        Cell cell2=row.createCell(1); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cell1.setCellValue(format.format(new Date())); try { Thread.sleep(5000); } catch (InterruptedException e1) { // TODO Auto-generated catch block
 e1.printStackTrace(); } cell2.setCellValue(format.format(new Date())); System.out.println("!!!!"); try { wb.write(outputStream); // 
        } catch (IOException e) { // TODO Auto-generated catch block
 e.printStackTrace(); }finally{ try { outputStream.close(); //  
            } catch (IOException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } } } }

②: poi 처리 날짜:
package poi; import java.io.FileOutputStream; import java.util.Calendar; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo2 { public static void main(String[] args) throws Exception { Workbook wb=new HSSFWorkbook(); FileOutputStream outputStream=new FileOutputStream("E://poi2.xls"); Sheet sheet=wb.createSheet(); Row row=sheet.createRow(0); Cell cell=row.createCell(0); cell.setCellValue(new Date()); // 
        CreationHelper helper=wb.getCreationHelper(); CellStyle cellStyle=wb.createCellStyle(); cellStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss")); cell=row.createCell(1); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); // 
        cell=row.createCell(2); cell.setCellValue(Calendar.getInstance()); cell.setCellStyle(cellStyle); wb.write(outputStream); outputStream.close(); } }

③: for 순환을 이용하여 반복
package poi; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Demo3 { public static void main(String[] args) throws Exception { FileInputStream inputStream=new FileInputStream("E://poi.xls"); POIFSFileSystem fs=new POIFSFileSystem(inputStream); HSSFWorkbook wb=new HSSFWorkbook(fs);  // 
        HSSFSheet sheet=wb.getSheetAt(0);  // 
        if(sheet==null){ return; } // 
        for(int rowNum=0;rowNum<=sheet.getLastRowNum();rowNum++){ HSSFRow row=sheet.getRow(rowNum); // 
            if(row==null){ continue; } // 
            for(int cellNum=0;cellNum<=row.getLastCellNum();cellNum++){ HSSFCell cell=row.getCell(cellNum); // 
                if(cell==null){ continue; } System.out.print(" "+getValue(cell)); } System.out.println(); } } // 
    private static String getValue(HSSFCell hssfCell){ if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){ return String.valueOf(hssfCell.getBooleanCellValue()); //boolean 
        }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){ return String.valueOf(hssfCell.getNumericCellValue());  // 
        }else{ return String.valueOf(hssfCell.getStringCellValue());  // 
 } } }

④: 순환할 필요 없음
package poi; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.extractor.ExcelExtractor; public class Demo4 { public static void main(String[] args) throws Exception { FileInputStream inputStream=new FileInputStream("E://poi.xls"); POIFSFileSystem fs=new POIFSFileSystem(inputStream); HSSFWorkbook wb=new HSSFWorkbook(fs); ExcelExtractor extractor=new org.apache.poi.hssf.extractor.ExcelExtractor(wb); System.out.println(extractor.getText()); } }

⑤: 셀의 문자 정렬 설정
package poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo5 { public static void main(String[] args) throws Exception { FileOutputStream outputStream=new FileOutputStream("E://poi3.xls"); Workbook wb=new HSSFWorkbook(); Sheet sheet=wb.createSheet(); Row row=sheet.createRow(0); createCell(wb,row,(short)0,HSSFCellStyle.ALIGN_LEFT,HSSFCellStyle.VERTICAL_BOTTOM); createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_CENTER); createCell(wb,row,(short)2,HSSFCellStyle.ALIGN_RIGHT,HSSFCellStyle.VERTICAL_JUSTIFY); createCell(wb,row,(short)3,HSSFCellStyle.ALIGN_CENTER_SELECTION,HSSFCellStyle.VERTICAL_JUSTIFY); wb.write(outputStream); outputStream.close(); } private static void createCell(Workbook wb,Row row,short column,short halign,short valign){ Cell cell=row.createCell(column);  // 
        cell.setCellValue(new HSSFRichTextString("hehe")); // 
        CellStyle cellStyle=wb.createCellStyle();  // 
        cellStyle.setAlignment(halign);  //  
        cellStyle.setVerticalAlignment(valign); // 
        cell.setCellStyle(cellStyle); // 
 } }

⑥: 셀 테두리 설정 방법
package poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo6 { public static void main(String[] args) throws Exception { FileOutputStream outputStream=new FileOutputStream("E://poi4.xls"); Workbook wb=new HSSFWorkbook(); CellStyle cellStyle=wb.createCellStyle(); Sheet sheet=wb.createSheet(); Row row=sheet.createRow(1); Cell cell=row.createCell(5); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(IndexedColors.SKY_BLUE.getIndex()); cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM_DASH_DOT); cellStyle.setTopBorderColor(IndexedColors.DARK_RED.getIndex()); cellStyle.setBorderLeft(CellStyle.BORDER_SLANTED_DASH_DOT); cellStyle.setLeftBorderColor(IndexedColors.DARK_YELLOW.getIndex()); cellStyle.setBorderRight(CellStyle.BORDER_THICK); cellStyle.setRightBorderColor(IndexedColors.GREEN.getIndex()); cell.setCellStyle(cellStyle); wb.write(outputStream); outputStream.close(); } }

⑦: 셀 배경색 및 패턴 설정 방법
package poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class Demo7 { public static void main(String[] args) throws Exception { FileOutputStream outputStream=new FileOutputStream("E://poi5.xls"); Workbook wb=new HSSFWorkbook(); CellStyle cellStyle=wb.createCellStyle(); Sheet sheet=wb.createSheet(); Row row=sheet.createRow(1); Cell cell=row.createCell(5); cell.setCellValue("yyyy"); cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex()); cellStyle.setFillPattern(CellStyle.DIAMONDS); cell.setCellStyle(cellStyle); CellStyle cellStyle2=wb.createCellStyle(); Cell cell2=row.createCell(6); cell2.setCellValue("tttt"); cellStyle2.setFillForegroundColor(IndexedColors.GREEN.getIndex()); cellStyle2.setFillPattern(CellStyle.THICK_BACKWARD_DIAG); cell2.setCellStyle(cellStyle2); wb.write(outputStream); outputStream.close(); } }

⑧: 셀 병합 방식
package poi; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; public class Demo8 { public static void main(String[] args) throws Exception { FileOutputStream outputStream=new FileOutputStream("E://poi6.xls"); Workbook wb=new HSSFWorkbook(); Sheet sheet=wb.createSheet(); Row row=sheet.createRow(0); Cell cell=row.createCell(5); cell.setCellValue(" "); sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 6)); wb.write(outputStream); outputStream.close(); } }

좋은 웹페이지 즐겨찾기