首页 > 代码库 > Poi对excel的基本操作

Poi对excel的基本操作

1.创建简单excel

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet s1=wb.createSheet("第一个sheet页");//创建Sheet页
        Row row=s1.createRow(0);//创建数据行
        Cell cell=row.createCell(0);//创建单元格
        cell.setCellValue(1);
        
        row.createCell(1).setCellValue(1.2);//单元格可写入不同格式的数据
        row.createCell(2).setCellValue("啦啦");
        row.createCell(3).setCellValue(false);
        
        FileOutputStream file=new FileOutputStream("e://poi_createCell.xls");
        wb.write(file);
        file.close();
        wb.close();
        
    }

2.创建指定日期格式

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("diyigeSheet页");
        Row row=sheet.createRow(0);
        Cell cell=row.createCell(0);
        cell.setCellValue(new Date());//默认日期类型 42796.89489

        CreationHelper creationHelper=wb.getCreationHelper();
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yy-MM-dd:hh ss"));
        
        cell=row.createCell(1);//创建指定日期类型数据 17-03-02:21 39
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);
        
        cell=row.createCell(2);//创建指定日期类型数据(第二种方式) 17-03-02:21 39
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);
        
        FileOutputStream fs=new FileOutputStream("e://poi_createData.xls");
        wb.write(fs);
        fs.close();
        wb.close();
    }

3.读取excel内容,以文本格式展示

    public static void main(String[] args) throws Exception {
        InputStream is=new FileInputStream("e:\\poi_read.xls");
        POIFSFileSystem fs=new POIFSFileSystem(is);
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        
        @SuppressWarnings("resource")
        ExcelExtractor excelExtractor=new ExcelExtractor(wb);
        excelExtractor.setIncludeSheetNames(false);//控制是否读取sheet页名字
        System.out.println(excelExtractor.getText());
    }

4.设置excel中单元格内容样式

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {
        Workbook wb=new HSSFWorkbook();
        Sheet s1=wb.createSheet("第一个sheet页");
        Row row=s1.createRow(0);
        row.setHeightInPoints(30);
        
        createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);//内容底部居中
        createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_FILL,HSSFCellStyle.VERTICAL_CENTER);//内容靠左垂直居中
        createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);//内容左上
        createCell(wb,row,(short)3,HSSFCellStyle.ALIGN_RIGHT,HSSFCellStyle.VERTICAL_TOP);//内容右上
        
        FileOutputStream fileOut=new FileOutputStream("e:\\demo.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
    
    //设置单元格样式
    private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
        Cell cell=row.createCell(column);//创建单元格
        cell.setCellValue(new HSSFRichTextString("Align It"));//设置值
        CellStyle cellStyle=wb.createCellStyle();//创建单元格样式
        cellStyle.setAlignment(halign);//设置单元格水平方向对齐方式
        cellStyle.setVerticalAlignment(valign);//设置单元格垂直方向对齐方式
        cell.setCellStyle(cellStyle);//设置单元格样式
    }

5.设置excel中单元格样式

    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("sheet1");
        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.RED.getIndex());//右边边框颜色
        
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);//上边边框
        cellStyle.setTopBorderColor(IndexedColors.BLUE.getIndex());//上边边框颜色
        
        cell.setCellStyle(cellStyle);
        
        FileOutputStream fileOut=new FileOutputStream("e:\\createExcelDemo08.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }

6.设置excel中单元格前景色和背景色

    public static void main(String[] args) throws Exception {
        HSSFWorkbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet");
        Row row=sheet.createRow(1);
        
        Cell cell=row.createCell(1);
        cell.setCellValue("sjdsk");
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());//设置背景色
        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
        cell.setCellStyle(cellStyle);
        
        Cell cell2=row.createCell(2);
        cell2.setCellValue("sjdsk");
        CellStyle cellStyle2=wb.createCellStyle();
        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex());//设置前景色
        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell2.setCellStyle(cellStyle2);
        
        FileOutputStream fileOut=new FileOutputStream("e://createDemo10a.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }

7.设置excel中合并单元格

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个sheet页");
        Row row=sheet.createRow(1);
        
        Cell cell=row.createCell(1);
        cell.setCellValue("单元格合并测试");
        
        sheet.addMergedRegion(new CellRangeAddress(
            1,//起始行
            2,//结束行
            1,//起始列
            2//结束列
            ));
        
        FileOutputStream file=new FileOutputStream("e:\\createExcelDemo11a.xls");
        wb.write(file);
        file.close();
        wb.close();
    }

8.设置excel文字样式

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet页啦");
        Row row=sheet.createRow(1);

        //创建一个字体处理类
        Font font=wb.createFont();
        font.setFontHeightInPoints((short)24);//设置字体大小
        font.setItalic(true);//设置斜体
        font.setStrikeout(true);//设置文字是否划线
        
        CellStyle style=wb.createCellStyle();
        style.setFont(font);
        
        Cell cell=row.createCell(1);
        cell.setCellValue("there are build a font");
        cell.setCellStyle(style);
        
        FileOutputStream file=new FileOutputStream("e:\\createExcelDemo12.xls");
        wb.write(file);
        file.close();
        wb.close();
    }

9.设置添加数据到已有的单元格

    public static void main(String[] args) throws Exception {
        InputStream input=new FileInputStream("e:\\createExcelDemo12.xls");
        POIFSFileSystem fs=new POIFSFileSystem(input);
        Workbook wb=new HSSFWorkbook(fs);
        Sheet sheet=wb.getSheetAt(0);
        Row row=sheet.getRow(1);
        Cell cell=row.getCell(1);
        if(cell==null){
            cell=row.createCell(3);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("测试POI添加数据到已有");
        
        FileOutputStream fos=new FileOutputStream("e:\\createExcelDemo12.xls");
        wb.write(fos);
        fos.close();
        wb.close();
    }

10.设置excel单元格中可换行

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet页");
        Row row=sheet.createRow(0);
        Cell cell=row.createCell(0);
        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:\\createExceDemo14.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();    
    }

11.设置excel中单元格数据精度

    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        FileOutputStream file=new FileOutputStream("e:\\createExcelPOI_DEMO15.xls");
        Sheet sheet=wb.createSheet();
        CellStyle style;
        DataFormat format=wb.createDataFormat();
        Row row;
        Cell cell;
        short rowNum=0;
        short cellNum=0;
        
        row=sheet.createRow(rowNum++);
        cell=row.createCell(cellNum++);
        cell.setCellValue(12.908);
        
        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("0.0"));
        cell.setCellStyle(style);//输出12.9
        
        row=sheet.createRow(rowNum++);
        cell=row.createCell(cellNum);
        cell.setCellValue(111111111111111.90899);
        
        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("#,##0,000"));
        cell.setCellStyle(style);//输出111,111,111,111,112
        
        wb.write(file);
        file.close();
        wb.close();    
    }

 

Poi对excel的基本操作