Poi基本操作

阅读更多
导入jar包 poi-3.9-20121203.jar


1. 创建新工作簿

Demo01.java

package com.andrew.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


2. 创建新Sheet页

Demo02.java

package com.andrew.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo02 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        wb.createSheet("第二个Sheet页"); // 创建第二个Sheet页
        FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的Sheet页.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


3. 创建单元格

Demo03.java

package com.andrew.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo03 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(0); // 创建一个行
        Cell cell = row.createCell(0); // 创建一个单元格 第1列
        cell.setCellValue(1); // 给单元格设置值
        row.createCell(1).setCellValue(1.2); // 创建一个单元格 第2列 值是1.2
        row.createCell(2).setCellValue("这是一个字符串类型"); // 创建一个单元格 第3列 值为一个字符串
        row.createCell(3).setCellValue(false); // 创建一个单元格 第4列 值为布尔类型
        FileOutputStream fileOut = new FileOutputStream("E:\\用Poi创建的Cell.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


4. 创建一个时间格式的单元格

Demo04.java

package com.andrew.poi;

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

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 Demo04 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(0); // 创建一个行
        Cell cell = row.createCell(0); // 创建一个单元格 第1列
        cell.setCellValue(new Date()); // 给单元格设置值

        CreationHelper createHelper = wb.getCreationHelper();
        CellStyle cellStyle = wb.createCellStyle(); // 单元格样式类
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss"));
        cell = row.createCell(1); // 第二列
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);

        cell = row.createCell(2); // 第三列
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);

        FileOutputStream fileOut = new FileOutputStream("E:\\工作簿04.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


5. 处理不同内容格式的单元格

Demo05.java

package com.andrew.poi;

import java.io.FileOutputStream;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo05 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(0); // 创建一个行
        Cell cell = row.createCell(0); // 创建一个单元格 第1列
        cell.setCellValue(new Date()); // 给单元格设置值

        row.createCell(1).setCellValue(1);
        row.createCell(2).setCellValue("一个字符串");
        row.createCell(3).setCellValue(true);
        row.createCell(4).setCellValue(HSSFCell.CELL_TYPE_NUMERIC);
        row.createCell(5).setCellValue(false);

        FileOutputStream fileOut = new FileOutputStream("E:\\工作簿05.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


6. 遍历工作簿的行和列并获取单元格内容

Demo06.java

package com.andrew.poi;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
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.poifs.filesystem.POIFSFileSystem;

public class Demo06 {
    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("E:\\二货名单.xls");
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet hssfSheet = wb.getSheetAt(0); // 获取第一个Sheet页
        if (hssfSheet == null) {
            return;
        }
        // 遍历行Row
        for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
            HSSFRow hssfRow = hssfSheet.getRow(rowNum);
            if (hssfRow == null) {
                continue;
            }
            // 遍历列Cell
            for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
                HSSFCell hssfCell = hssfRow.getCell(cellNum);
                if (hssfCell == null) {
                    continue;
                }
                System.out.print(" " + getValue(hssfCell));
            }
            System.out.println();
        }
    }

    private static String getValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }
}

运行结果:
1.0 tom 12.0 true
2.0 cat 15.0 false


7. 文本提取

Demo07.java

package com.andrew.poi;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Demo07 {
    public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("E:\\二货名单.xls");
        POIFSFileSystem fs = new POIFSFileSystem(is);
        HSSFWorkbook wb = new HSSFWorkbook(fs);

        ExcelExtractor excelExtractor = new ExcelExtractor(wb);
        excelExtractor.setIncludeSheetNames(false);// 我们不需要Sheet页的名字
        System.out.println(excelExtractor.getText());
    }
}

运行结果:
1    tom    12    true
2    cat    15    false


8. 单元格对齐方式

Demo08.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo08 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(1); // 创建一个行

        Cell cell = row.createCell(1);
        cell.setCellValue("XX");
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
        cell.setCellStyle(cellStyle);

        Cell cell2 = row.createCell(2);
        cell2.setCellValue("YYY");
        CellStyle cellStyle2 = wb.createCellStyle();
        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell2.setCellStyle(cellStyle2);

        FileOutputStream fileOut = new FileOutputStream("E:\\工作簿08.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


9. 单元格边框处理

Demo09.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo09 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(1); // 创建一个行

        Cell cell = row.createCell(1); // 创建一个单元格
        cell.setCellValue(4);

        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色

        cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框
        cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); // 左边边框颜色

        cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框
        cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色

        cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色

        cell.setCellStyle(cellStyle);
        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿09.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


10. 单元格填充色和颜色操作

Demo10.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo10 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(1); // 创建一个行

        Cell cell = row.createCell(1);
        cell.setCellValue("XX");
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
        cell.setCellStyle(cellStyle);

        Cell cell2 = row.createCell(2);
        cell2.setCellValue("YYY");
        CellStyle cellStyle2 = wb.createCellStyle();
        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell2.setCellStyle(cellStyle2);

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿10.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


11. 单元格合并

Demo11.java

package com.andrew.poi;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo11 {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(1); // 创建一个行

        Cell cell = row.createCell(1);
        cell.setCellValue("单元格合并测试");

        sheet.addMergedRegion(new CellRangeAddress(1, // 起始行
                2, // 结束行
                1, // 其实列
                2 // 结束列
        ));

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿11.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


12. 字体处理

Demo12.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo12 {

    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(1); // 创建一个行

        // 创建一个字体处理类
        Font font = wb.createFont();
        font.setFontHeightInPoints((short) 24);
        font.setFontName("Courier New");
        font.setItalic(true);
        font.setStrikeout(true);

        CellStyle style = wb.createCellStyle();
        style.setFont(font);

        Cell cell = row.createCell((short) 1);
        cell.setCellValue("This is test of fonts");
        cell.setCellStyle(style);

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿12.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


13. 读取和重写工作簿

Demo13.java

package com.andrew.poi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo13 {
    public static void main(String[] args) throws Exception {
        InputStream inp = new FileInputStream("e:\\工作簿.xls");
        POIFSFileSystem fs = new POIFSFileSystem(inp);
        Workbook wb = new HSSFWorkbook(fs);
        Sheet sheet = wb.getSheetAt(0); // 获取第一个Sheet页
        Row row = sheet.getRow(0); // 获取第一行
        Cell cell = row.getCell(0); // 获取单元格
        if (cell == null) {
            cell = row.createCell(3);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("测试单元格");

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿04.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


14. 单元格中使用换行

Demo14.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo14 {

    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        Row row = sheet.createRow(2); // 创建一个行
        Cell cell = row.createCell(2);
        cell.setCellValue("我要换行 \n 成功了吗?");

        CellStyle cs = wb.createCellStyle();
        // 设置可以换行
        cs.setWrapText(true);
        cell.setCellStyle(cs);

        // 调整下行的高度
        row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
        // 调整单元格宽度
        sheet.autoSizeColumn(2);

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿14.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}


15. 创建用户自定义数据格式

Demo15.java

package com.andrew.poi;

import java.io.FileOutputStream;

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.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo15 {

    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
        CellStyle style;
        DataFormat format = wb.createDataFormat();
        Row row;
        Cell cell;
        short rowNum = 0;
        short colNum = 0;

        row = sheet.createRow(rowNum++);
        cell = row.createCell(colNum);
        cell.setCellValue(111111.25);

        style = wb.createCellStyle();
        style.setDataFormat(format.getFormat("0.0")); // 设置数据格式
        cell.setCellStyle(style);

        row = sheet.createRow(rowNum++);
        cell = row.createCell(colNum);
        cell.setCellValue(1111111.25);
        style = wb.createCellStyle();
        style.setDataFormat(format.getFormat("#,##0.000"));
        cell.setCellStyle(style);

        FileOutputStream fileOut = new FileOutputStream("e:\\工作簿15.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

你可能感兴趣的:(poi)