package excelupload;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

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.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;

public class ExcelUtils {
 public static void main(String[] args) {
  HSSFWorkbook workbook = new HSSFWorkbook(); // 새 엑셀 생성
  HSSFSheet sheet = workbook.createSheet("시트명"); // 새 시트(Sheet) 생성
  HSSFRow row  = null;
  HSSFCell cell = null;
  for (int j = 0; j < 10; j++) {
   row = sheet.createRow(j); // 엑셀의 행은 0번부터 시작
   for (int i = 0; i < 10; i++) {
    cell = row.createCell(i); // 행의 셀은 0번부터 시작
    row.createCell(1, CellType.STRING);
    cell.setCellValue("테스트 데이터" + i); // 생성한 셀에 데이터 삽입
   }
  }

  try {
   FileOutputStream fileoutputstream = new FileOutputStream("C:\\Users\\k\\Desktop\\dd.xls");
   workbook.write(fileoutputstream);
   fileoutputstream.close();
   System.out.println("엑셀파일생성성공");
  } catch (IOException e) {
   e.printStackTrace();
   System.out.println("엑셀파일생성실패");
  }
 }

}

 

 출처 http://boxfoxs.tistory.com/304

블로그 이미지

왕왕왕왕

,