자바 poi 작업 1 엑셀 xlsx 만 들 기

3127 단어
package com.tanruyu.demo;

import java.io.File;

import java.io.FileOutputStream;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.IndexedColors;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CreateXlsx {

    public static void main(String[] args) throws Exception {

        // 1.        

        XSSFWorkbook workbook = new XSSFWorkbook();

        //           

        FileOutputStream out = new FileOutputStream(new File("f:\\createworkbook.xlsx"));

        // 2.        

        XSSFSheet spreadsheet = workbook.createSheet("Sheet1");

        //   

        Map empinfo = new TreeMap();

        empinfo.put("1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });

        empinfo.put("2", new Object[] { "tp01", "Gopal", "Technical Manager" });

        empinfo.put("3", new Object[] { "tp02", "Manisha", "Proof Reader" });

        empinfo.put("4", new Object[] { "tp03", "Masthan", "Technical Writer" });

        empinfo.put("5", new Object[] { "tp04", "Satish", "Technical Writer" });

        empinfo.put("6", new Object[] { "tp05", "Krishna", "Technical Writer" });

        Set keyid = empinfo.keySet();//      key 

        int rowid = 0;

        for (String key : keyid) {

            // 3.     

            XSSFRow row = spreadsheet.createRow(rowid++);

            Object[] objectArr = empinfo.get(key);

            //         

            XSSFCellStyle style5 = workbook.createCellStyle();

            style5.setAlignment(XSSFCellStyle.ALIGN_CENTER);

            style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);

            style5.setBottomBorderColor(IndexedColors.BLUE.getIndex());

            style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);

            style5.setLeftBorderColor(IndexedColors.GREEN.getIndex());

            style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);

            style5.setRightBorderColor(IndexedColors.RED.getIndex());

            style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);

            style5.setTopBorderColor(IndexedColors.CORAL.getIndex());

            // cell.setCellStyle(style5);

            int cellid = 0;

            for (Object obj : objectArr) {

                //           

                Cell cell = row.createCell(cellid++);

                //        

                cell.setCellValue((String) obj);

                //        

                cell.setCellStyle(style5);
            }
        }
        //   
        workbook.write(out);
        out.close();
        System.out.println(" createworkbook.xlsx written successfully");
    }
}

좋은 웹페이지 즐겨찾기