java生成excel格式xlsx

1,引入jar


    ojdbc7.jar
    commons-collections4-4.4.jar
    poi-3.17.jar
    poi-ooxml-3.17.jar
    poi-ooxml-schemas-3.17.jar
    xmlbeans-3.1.0.jar

2,生成 xlsx格式的excel  

    package main.utils;

    import java.util.List;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    import org.apache.poi.xssf.usermodel.XSSFDataFormat;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class ExcelUtils {

        /**
         * getWorkBook
         * 
         * @param titleList
         * @param contentlist
         * @param sheetName
         * @return xwk
         */
        public XSSFWorkbook getWorkBook(List titleList, List> contentlist, String sheetName) {
            XSSFWorkbook xwk = new XSSFWorkbook();
            XSSFDataFormat format = xwk.createDataFormat();
            XSSFCellStyle cellStyle = xwk.createCellStyle();
            XSSFSheet xssfSheet = xwk.createSheet(sheetName);
            cellStyle.setDataFormat(format.getFormat("@"));//文本格式
            int j = 0;
            createHeader(xssfSheet, cellStyle, titleList, j);
            int size = contentlist.size();
            for (j = 0; j < size; j++) {
                List oneRow = contentlist.get(j);
                createContent(xssfSheet, cellStyle, oneRow, j);
                oneRow = null;
            }
            return xwk;
        }

        /**
         * createHeader
         * 
         * @param xssfSheet
         * @param titleList
         */
        private void createHeader(XSSFSheet xssfSheet, XSSFCellStyle cellStyle, List titleList, int j) {
            XSSFRow rowTitle = xssfSheet.createRow(j);
            for (int cellTitle = 0; cellTitle < titleList.size(); cellTitle++) {
                Cell cellIndex = rowTitle.createCell(cellTitle);
                cellIndex.setCellStyle(cellStyle);
                cellIndex.setCellValue(titleList.get(cellTitle));
            }
        }

        /**
         * createHeader
         * 
         * @param xssfSheet
         * @param oneRow
         * @param j
         */
        private void createContent(XSSFSheet xssfSheet, XSSFCellStyle cellStyle, List oneRow, int j) {
            XSSFRow rowContent = xssfSheet.createRow(j + 1);
            for (int cellContent = 0; cellContent < oneRow.size(); cellContent++) {
                Cell cellIndex = rowContent.createCell(cellContent);
                cellIndex.setCellStyle(cellStyle);
                cellIndex.setCellValue(oneRow.get(cellContent));
            }
        }
    }

3,测试

    package main.java;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

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

    import main.utils.ExcelUtils;

    public class TestExcelUtils {

        public static void main(String[] args) throws IOException  {
            List titleList = new  ArrayList();
            titleList.add("NO");
            titleList.add("ID");
            titleList.add("Name");
            titleList.add("Age");
            List content = null;
            List> contentsList = new  ArrayList>();
            content = new ArrayList();
            content.add("1");
            content.add("180001");
            content.add("Jame");
            content.add("18");
            contentsList.add(content);
            content = new ArrayList();
            content.add("1");
            content.add("180002");
            content.add("Lucky");
            content.add("18");
            contentsList.add(content);
            
            XSSFWorkbook workBook = null;
            FileOutputStream output = null;
            String sheetName = "student";
            String fileName = "D:/export/student.xlsx";
            
            if (contentsList.size() > 0) {
                try {
                    ExcelUtils eu = new ExcelUtils();
                    workBook = eu.getWorkBook(titleList, contentsList, sheetName);
                    output =  new FileOutputStream(fileName);
                    workBook.write(output);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally{
                    if(output != null){
                        output.flush();
                        output.close();
                    }
                    if (workBook != null) {
                        workBook.close();
                    }
                }
            }       

        }

    }

你可能感兴趣的:(Java,POI,xlsx)