(二)JAVA使用POI操作excel

链接:https://www.cnblogs.com/wishwzp/p/5494038.html
出现版本错误,链接:https://www.cnblogs.com/xx0405/p/5305566.html

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

Demo4.java

package com.wajpzywj.exceldemo.main;

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

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

public class Demo4 {
    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("d:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

(二)JAVA使用POI操作excel
(二)JAVA使用POI操作excel

2,处理不同内容格式的单元格

Demo5.java

package com.wajpzywj.exceldemo.main;

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;

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

public class Demo5 {
    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("d:\\工作簿.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

(二)JAVA使用POI操作excel
(二)JAVA使用POI操作excel

3,遍历工作簿的行和列并获取单格内容

(二)JAVA使用POI操作excel
(二)JAVA使用POI操作excel
我们遍历这个excel。

Demo6.java

package com.wajpzywj.exceldemo.main;

import com.wajpzywj.exceldemo.utils.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.FileInputStream;

public class Demo6 {
    public static void main(String[] args) throws Exception{
//        InputStream is=new FileInputStream("d:\\个人名单.xlsx");
//        POIFSFileSystem fs=new POIFSFileSystem(is);
//        HSSFWorkbook wb=new HSSFWorkbook(fs);
        Workbook wb = WorkbookFactory.create(new FileInputStream("d:\\个人名单.xlsx"));

        XSSFSheet xssfSheet= (XSSFSheet) wb.getSheetAt(0); // 获取第一个Sheet页
        if(xssfSheet==null){
            return;
        }
        // 遍历行Row
        for(int rowNum=0;rowNum<=xssfSheet.getLastRowNum();rowNum++){
            XSSFRow xssfRow=xssfSheet.getRow(rowNum);
            if(xssfRow==null){
                continue;
            }
            // 遍历列Cell
            for(int cellNum=0;cellNum<=xssfRow.getLastCellNum();cellNum++){
                XSSFCell xssfCell=xssfRow.getCell(cellNum);
                if(xssfCell==null){
                    continue;
                }
                System.out.print(" "+getValue(xssfCell));
            }
            System.out.println();
        }
    }

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

(二)JAVA使用POI操作excel

4,文本提取

我们文本提取上面个人名单.xls表格。。。

Demo7.java

package com.wajpzywj.exceldemo.main;

import com.wajpzywj.exceldemo.utils.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;

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

import java.io.FileInputStream;

public class Demo7 {
    public static void main(String[] args) throws Exception{
        Workbook wb = WorkbookFactory.create(new FileInputStream("d:\\个人名单.xlsx"));

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

控制台显示:
(二)JAVA使用POI操作excel
excelExtractor.setIncludeSheetNames(false);这个如果为true的话就会显示Sheet页了

excelExtractor.setIncludeSheetNames(true):

控制台输出:
(二)JAVA使用POI操作excel