흠.. 3.5 버전으로 작업함. 3.6 부터는 밑에 코드를 사용할 수 없음. 사용할 수 없는 클래스가 존재하는 듯..

참조

오피스 버전에 따라서 사용하는 API가 다름.
   : 2007 을 기준으로( ooo.xls, ooo.xlsx)
 
본 문서는 ooo.xls 버전을 기준으로 작업할거임. 뭐.. 상위 버전이랑 큰 차이는 없는듯


1. workbook.xls 만들기
package com.excel.sample;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;


import org.apache.poi.hssf.usermodel.HSSFCell;
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 Launcher {

 final static int rowNumber  = 100;
 
 public void makeRowHeader(CreationHelper createHelper, Sheet sheet) {
  Row row = sheet.createRow(0);
 
  row.createCell(0).setCellValue(createHelper.createRichTextString("번호"));
     row.createCell(1).setCellValue(createHelper.createRichTextString("value"));
     row.createCell(2).setCellValue(createHelper.createRichTextString("Description(설 명)"));
     row.createCell(3).setCellValue(createHelper.createRichTextString("상태 값"));
     row.createCell(4).setCellValue(createHelper.createRichTextString("Date - new Date()"));
     row.createCell(5).setCellValue(createHelper.createRichTextString("Date - m/d/yy h:mm"));
     row.createCell(6).setCellValue(createHelper.createRichTextString("Date - Calendar.getInstance()"));
   
     row.createCell(7).setCellValue(createHelper.createRichTextString("Date - 비고"));
 }
 
 public void makeRow(Workbook wb, CreationHelper createHelper, Sheet sheet) {
  for(int i = 1;i<rowNumber;i++) {
   Row row = sheet.createRow(i);
  
   Cell cell = row.createCell(0);
      cell.setCellValue(1);
      row.createCell(1).setCellValue(1.2);
      row.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));
      row.createCell(3).setCellValue(true);
    
      row.createCell(4).setCellValue(new Date());
    
      // we style the second cell as a date (and time).  It is important to
      // create a new cell style from the workbook otherwise you can end up
      // modifying the built in style and effecting not only this cell but other cells.
      CellStyle cellStyle = wb.createCellStyle();
      cellStyle.setDataFormat(
          createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
      cell = row.createCell(5);
      cell.setCellValue(new Date());
      cell.setCellStyle(cellStyle);

    
      //you can also set date as java.util.Calendar
      cell = row.createCell(6);
      cell.setCellValue(Calendar.getInstance());
      cell.setCellStyle(cellStyle);
    
    
      row.createCell(7).setCellType(HSSFCell.CELL_TYPE_ERROR);
    
  }
 }
 
 public void makeExcel() throws IOException {
  Workbook wb = new HSSFWorkbook();

  CreationHelper createHelper = wb.getCreationHelper();
  Sheet sheet = wb.createSheet("sheet1");
     
  makeRowHeader(createHelper, sheet);
 
  makeRow(wb, createHelper, sheet);
 
 

   
     FileOutputStream fileOut;
     fileOut = new FileOutputStream("D:\\Launcher\\excel\\workbook.xls");

  wb.write(fileOut);
  fileOut.close();
 }
 
 public static void main(String[] args) throws IOException {
 
  new Launcher().makeExcel();
 }
}
 
2. 만든넘 읽기.
public void readExcel() throws InvalidFormatException, IOException {
InputStream inp = new FileInputStream("D:\\Launcher\\excel\\workbook.xls");

   Workbook wb = WorkbookFactory.create(inp);
   Sheet sheet = wb.getSheetAt(0);

   for(Row row : sheet) {
    for(Cell cell : row) {
     CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
     System.out.print(cellRef.formatAsString());
     System.out.print(" - ");
     switch(cell.getCellType()) {
      case Cell.CELL_TYPE_STRING:
             System.out.println("string : " + cell.getRichStringCellValue().getString());
             break;
           case Cell.CELL_TYPE_NUMERIC:
             if(DateUtil.isCellDateFormatted(cell)) {
               System.out.println("date : " + cell.getDateCellValue());
             } else {
               System.out.println("number : " + cell.getNumericCellValue());
             }
             break;
           case Cell.CELL_TYPE_BOOLEAN:
             System.out.println("boolean : " + cell.getBooleanCellValue());
             break;
           case Cell.CELL_TYPE_FORMULA:
             System.out.println("formula : " + cell.getCellFormula());
             break;
           default:
             System.out.println(cell.toString());
     }
    }
   }
}

3.6이상 부터는 위 코드를 사용할 수 없었음.. 뭐.. 소스를 다운로드 받으면 그 속에 예제가 있기 때문에 그것보고 사용해도 좋을듯.

'java > excel' 카테고리의 다른 글

[excel] - Apache POI를 사용해서 office2007 가지고 놀기  (0) 2010.06.30
Posted by 짱가쟁이
/**
  *
  * @param command
  *     ex> "java -jar jface/util/tool/trang.jar D:\\runtest\\tableconfig.xml D:\\runtest\\TableConfig.xsd"
  */
 public void launcher(String command) {
 
  try {
   String s;
   String error = "";
  
   Process process = new ProcessBuilder("cmd", "/c", command).start();
  
   // 외부 프로그램 출력 읽기
      BufferedReader stdOut   =
       new BufferedReader(new InputStreamReader(process.getInputStream()));
      BufferedReader stdError =
       new BufferedReader(new InputStreamReader(process.getErrorStream()));
 
      // "표준 출력"과 "표준 에러 출력"을 출력
      while ((s =   stdOut.readLine()) != null) {
       System.out.print(s);
      }
      while ((s = stdError.readLine()) != null) {
       error = error + s;      
      }
 
      if(process.exitValue() == 0) {
       MessageDialog.openInformation(parent.getParent().getShell(),
         "XSD generate", parent.getXsdLocationText().getText() + " 를 생성했습니다.");
      } else {
       MessageDialog.openInformation(parent.getParent().getShell(),
         "XSD generate", error + "\n\n" + parent.getXsdLocationText().getText() + " 생성에 실패했습니다.");
      }

  } catch(Exception e) {
   e.printStackTrace();
  }
 }

'java > util' 카테고리의 다른 글

[java] - base54 String decode  (0) 2010.06.30
[java] - 타입 변환  (0) 2010.06.30
[java] - byte array to int  (0) 2010.06.29
[java] - int to byte array  (0) 2010.06.29
[java] - Big Endian to Little Endian  (0) 2010.06.29
Posted by 짱가쟁이
리플렉션은 프로그램이 자신의 모습을 보고 수정하는 행위.
 
ex> Spring 의 DI(dependecy injection)

Example
Class<?> dymClass = null;
Method dymMethod  = null;

try {

    // 다이나믹 클래스 인스턴스 생성
    dymClass  = Class.forName(child.getListener());
    Object obj  = dymClass.newInstance();
 
    dymMethod = obj.getClass().getMethod("setShell", new Class[]{Shell.class});
  
    dymMethod.invoke(obj, new Object[]{parent.getShell()});
    toolItem.addListener(SWT.Selection, ((Listener)obj));
      
    toolItemMap.put(child.getId(), toolItem);
     
} catch(Exception e) {

}

Posted by 짱가쟁이
jdbc.driverClassName=sun.jdbc.odbc.JdbcOdbcDriver

jdbc.url=jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=config/db.mdb
jdbc.username=Admin
jdbc.password=

jdbc.maxActive=20
jdbc.maxIdle = 5
jdbc.maxWait = 10000

Posted by 짱가쟁이
<script src="http://java.com/js/deployJava.js"></script>

<script>
 if (deployJava.isPluginInstalled()) {
  if (!deployJava.versionCheck('1.6.0_10+')) {
   deployJava.installJRE('1.6.0_10+');
  }
 } else {
  deployJava.installLatestJRE();
 }
</script>

요 스크립트를 추가하면 .. JRE 다운로드 페이지로 자동이동 됨.
Posted by 짱가쟁이
public static int byteToInt(byte[] src) {

        int newValue = 0;

        newValue |= (((int)src[0])<<24)&0xFF000000;
        newValue |= (((int)src[1])<<16)&0xFF0000;
        newValue |= (((int)src[2])<<8)&0xFF00;
        newValue |= (((int)src[3]))&0xFF;

        DebugPrint.writeDebug("aaaa : " + newValue);
      
        return newValue;
}

'java > util' 카테고리의 다른 글

[java] - 타입 변환  (0) 2010.06.30
[java] - 외부 프로그램 실행하기..  (0) 2010.06.29
[java] - int to byte array  (0) 2010.06.29
[java] - Big Endian to Little Endian  (0) 2010.06.29
[java] - Little Endian to Big Endian  (0) 2010.06.29
Posted by 짱가쟁이
public static byte[] intTobyte(int value) {
        byte[] bytes=new byte[4];
        bytes[0]=(byte)((value&0xFF000000)>>24);
        bytes[1]=(byte)((value&0x00FF0000)>>16);
        bytes[2]=(byte)((value&0x0000FF00)>>8);
        bytes[3]=(byte) (value&0x000000FF);


        return bytes;

}

'java > util' 카테고리의 다른 글

[java] - 외부 프로그램 실행하기..  (0) 2010.06.29
[java] - byte array to int  (0) 2010.06.29
[java] - Big Endian to Little Endian  (0) 2010.06.29
[java] - Little Endian to Big Endian  (0) 2010.06.29
[java] - float to byte array  (1) 2010.06.29
Posted by 짱가쟁이
public static int swapLittleEndian(byte[] bytes) {
     
        int newValue = 0;
        newValue |= (((int)bytes[3])<<24)&0xFF000000;
        newValue |= (((int)bytes[2])<<16)&0xFF0000;
        newValue |= (((int)bytes[1])<<8)&0xFF00;
        newValue |= (((int)bytes[0]))&0xFF;


        return newValue;

}

'java > util' 카테고리의 다른 글

[java] - byte array to int  (0) 2010.06.29
[java] - int to byte array  (0) 2010.06.29
[java] - Little Endian to Big Endian  (0) 2010.06.29
[java] - float to byte array  (1) 2010.06.29
[java] - byte array to float  (0) 2010.06.29
Posted by 짱가쟁이
public static int swapBigEndian(String args) {

        int value=Integer.parseInt(args);

        // all java integer is BIG-ENDIAN(network byte-order)
        byte[] bytes=new byte[4];
        bytes[3]=(byte)((value&0xFF000000)>>24);
        bytes[2]=(byte)((value&0x00FF0000)>>16);
        bytes[1]=(byte)((value&0x0000FF00)>>8);
        bytes[0]=(byte) (value&0x000000FF);


        int newValue = 0;
        newValue |= (((int)bytes[0])<<24)&0xFF000000;
        newValue |= (((int)bytes[1])<<16)&0xFF0000;
        newValue |= (((int)bytes[2])<<8)&0xFF00;
        newValue |= (((int)bytes[3]))&0xFF;

        return newValue;
}

'java > util' 카테고리의 다른 글

[java] - int to byte array  (0) 2010.06.29
[java] - Big Endian to Little Endian  (0) 2010.06.29
[java] - float to byte array  (1) 2010.06.29
[java] - byte array to float  (0) 2010.06.29
[java] - byte array to short  (0) 2010.06.29
Posted by 짱가쟁이
public static byte[] float2bytes(float value) {
      
        byte[] array = new byte[4];
      
        int intBits=Float.floatToIntBits(value);
      
        array[0]=(byte)((intBits&0x000000ff)>>0);
        array[1]=(byte)((intBits&0x0000ff00)>>8);
        array[2]=(byte)((intBits&0x00ff0000)>>16);
        array[3]=(byte)((intBits&0xff000000)>>24);
      
        return array;
}

Posted by 짱가쟁이