POI生成excel並設置過濾範圍

package com.xiaobu.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.awt.Color;
import java.io.FileOutputStream;

/**  1048576  設置excel篩選的  SXSSFWorkbook的最大100w  生成的表格邊框有效(過時方法)
 */
public class TestExcel {
    public static void main(String[] args) throws Throwable {
        // keep 100 rows in memory, exceeding rows will be flushed to disk
        Workbook wb = new SXSSFWorkbook(100);
        Sheet sh = wb.createSheet();
        for(int rownum = 0; rownum < 10; rownum++){
            Row row = sh.createRow(rownum);
            for(int cellnum = 0; cellnum < 10; cellnum++){
                Cell cell = row.createCell(cellnum);
				XSSFCellStyle xssfCellStyle = (XSSFCellStyle) wb.createCellStyle();
				xssfCellStyle.setFillForegroundColor(new XSSFColor(Color.YELLOW));
				xssfCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
				xssfCellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
				xssfCellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
				xssfCellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
				xssfCellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
				cell.setCellStyle(xssfCellStyle);
				//設置過濾範圍
				CellRangeAddress cellRangeAddress=	CellRangeAddress.valueOf("A1:R1");
				//設置過濾
				sh.setAutoFilter(cellRangeAddress);
               // System.out.println(new CellReference(cell) );//org.apache.poi.ss.util.CellReference [A1]
                //  'A1'
                String address = new CellReference(cell).formatAsString();
                cell.setCellValue(address);
            }
        }
        FileOutputStream out = new FileOutputStream("E:/sxssf.xlsx");
        System.out.println(123);
        wb.write(out);
        out.close();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章