Poi 기반
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(); } }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.