본문 바로가기

프로그래밍/JAVA

java - 엑셀 조작하기

반응형


======================================================================
java로 엑셀 파일을 조작해볼까요...^^
======================================================================

***** 먼저 첨부된 jxl api를 설치하세요.^^*****

----------------------------------------------------------------------
1.내 컴퓨터에 myExcelFile.xls(51행,10열)을 만들어 보자.
----------------------------------------------------------------------
import java.io.*;
import java.util.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;

public class ExcelTest2{

public ExcelTest2() {
   super();
}
public static void  main(String[] argv) throws FileNotFoundException, IOException,WriteException{

WritableWorkbook workbook = Workbook.createWorkbook(new File("c:/myExcelFile.xls"));
WritableSheet sheet = workbook.createSheet("Sheet1", 0);

jxl.write.WritableCellFormat  format= new WritableCellFormat();
jxl.write.WritableCellFormat  format0= new WritableCellFormat();
  
format.setBackground(jxl.format.Colour.GRAY_25 );
format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN );
format.setAlignment(jxl.format.Alignment.CENTRE);
format0.setBackground(jxl.format.Colour.WHITE );
format0.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN );
format0.setAlignment(jxl.format.Alignment.CENTRE);
sheet.setColumnView(0,8);

jxl.write.Label  label = null;  
jxl.write.Blank blank = null;
label = new jxl.write.Label(0,0,"결재란",format);
sheet.addCell(label);
blank = new jxl.write.Blank(1,0,format0);
sheet.addCell(blank);  
label = new jxl.write.Label(2,0,"결재일",format);
sheet.addCell(label);
blank = new jxl.write.Blank(1,0,format0);
sheet.addCell(blank);   
  
for(int h=1;h<51;h++){
     for(int i=0;i<10;i++){
        label = new jxl.write.Label(i, h, "("+i+","+h+")",format0);
        sheet.addCell(label);  
     }
}

   
workbook.write();
workbook.close();
}
}//ExcelTest2 class END

----------------------------------------------------------------------
2.내 컴퓨터에 있는 myExcelFile.xls 읽어서 화면에 보여주기.
----------------------------------------------------------------------
import java.io.*;
import java.util.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;

public class ExcelTest{
public ExcelTest(){
   super();
}
public static void  main(String[] argv) throws FileNotFoundException, IOException,jxl.read.biff.BiffException{
  Workbook workbook =
  Workbook.getWorkbook(new File("c:/myExcelFile.xls"));
  Sheet sheet = workbook.getSheet(0);
  Cell myCell = null;
  for(int h=1;h<51;h++){
      for(int i=0;i<10;i++){
         myCell = sheet.getCell(i,h);
 System.out.println(myCell.getContents());  
      }
  }
}
}//ExcelTest class END

 

반응형