zoukankan      html  css  js  c++  java
  • POI 操作 excel表格 (简单整理)

    简单的整理:

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.streaming.SXSSFCell;
    import org.apache.poi.xssf.streaming.SXSSFRow;
    import org.apache.poi.xssf.streaming.SXSSFSheet;
    import org.apache.poi.xssf.streaming.SXSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     * Created by Administrator on 2019/6/12.
     */
    public class SXSSFWorkbook_Main
    {
        public static void main(String[] args)
        {
            long startTime = System.currentTimeMillis();
            String filePath = "E:\111.xlsx";
            SXSSFWorkbook sxssfWorkbook = null;
            BufferedOutputStream outputStream = null;
            try {
                //这样表示SXSSFWorkbook只会保留100条数据在内存中,其它的数据都会写到磁盘里,这样的话占用的内存就会很少
                sxssfWorkbook = new SXSSFWorkbook(getXSSFWorkbook(filePath),100);
                //获取第一个Sheet页
                SXSSFSheet sheet = sxssfWorkbook.getSheetAt(0);
    
                //合并行之后创建第一行
                sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
                SXSSFRow row = sheet.createRow(0);
    
                //添加第一行的单元格
                SXSSFCell cell = row.createCell(0);
                CellStyle cellStyle = getHeadStyle(sxssfWorkbook);
                cell.setCellStyle(cellStyle);
                row.createCell(1).setCellStyle(cellStyle);
                row.createCell(2).setCellStyle(cellStyle);
                row.createCell(3).setCellStyle(cellStyle);   //合并单元格之后要加边框, 所以都要加上
                cell.setCellValue("项目工作清单"); //合并单元格之后设置值
    
    
                //创建第2行
                SXSSFRow row1 = sheet.createRow(1);
                CellStyle style = getHeadStyle(sxssfWorkbook);
                style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
                row1.createCell(0).setCellStyle(style);
    
                outputStream = new BufferedOutputStream(new FileOutputStream(filePath));
                sxssfWorkbook.write(outputStream);
                outputStream.flush();
                sxssfWorkbook.dispose();// 释放workbook所占用的所有windows资源
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(outputStream!=null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
    
        }
    
        private static CellStyle getHeadStyle(SXSSFWorkbook sxssfWorkbook)
        {
            CellStyle cellStyle = sxssfWorkbook.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);   //设置单元格的水平居中
    
            //上下左右的边框
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
    
            //设置单元格背景填充颜色
            cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            return cellStyle;
        }
    
        public static XSSFWorkbook getXSSFWorkbook(String filePath) {
            XSSFWorkbook workbook =  null;
            BufferedOutputStream outputStream = null;
            try {
                File fileXlsxPath = new File(filePath);
                outputStream = new BufferedOutputStream(new FileOutputStream(fileXlsxPath));
                workbook = new XSSFWorkbook();
                workbook.createSheet("测试Sheet");
                workbook.write(outputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(outputStream!=null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return workbook;
        }
    
    }

    官网向导:https://poi.apache.org/components/spreadsheet/quick-guide.html#FillsAndFrills

    官网API:http://poi.apache.org/apidocs/dev/org/apache/poi/xssf/streaming/SXSSFWorkbook.html

  • 相关阅读:
    调用接口直接下载文件
    Oracle函数简单使用
    JAVA面试题刷题资料
    跨域
    ORACLE JOB
    C# 面试知识点网络文档整理
    GetBuffer 与ToArray区别,解决问题场景
    JQuery选择器分类
    C#判断字符串中含有多少个汉字
    XPATH中text()和string()的使用区别
  • 原文地址:https://www.cnblogs.com/yufeng218/p/11013537.html
Copyright © 2011-2022 走看看