java处理Excel

1,得到上传的文件

FileItem upfile = parser.getParameters().getFileItem("upfile");

等类似方法

2,转成输入流

InputStream is = upfile.getInputStream();

3,通过输入流构建workbook

Workbook wb = WorkbookFactory.create(is);

maven依赖:

            <!-- 微软office处理 api -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.14</version>
                <exclusions>
                    <exclusion>
                        <groupId>stax</groupId>
                        <artifactId>stax-api</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- 微软office处理 api end -->

4,常用方法:

Sheet sheet = wb.getSheetAt(0) : 取第一个sheet页
int lastRowIndex = sheet.getLastRowNum(); 得到行数
workbook = new XSSFWorkbook(); 生成xlsx文件
workbook = new HSSFWorkbook();生成xls文件
sheet = workbook.createSheet(sheetName); 新建一页
/**
     * 初始化样式
     */
    private void initStyle() {
        // 设置表头样式
        headerStyle = workbook.createCellStyle();
        headerStyle.setWrapText(true);
        headerStyle.setBorderTop(CellStyle.BORDER_THIN);
        headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
        headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
        headerStyle.setBorderRight(CellStyle.BORDER_THIN);
        headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
        headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
        Font font = workbook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints((short) 12);
        headerStyle.setFont(font);

        // 设置单元格样式
        cellStyle = workbook.createCellStyle();
        cellStyle.setWrapText(true);
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    }
/**
     * 根据inputStream创建Excel文件(用于读取Excel数据)
     * 
     * @param inputStream
     * @throws IOException
     */
    public NewExcelHelper(InputStream inputStream, boolean isNew) throws IOException {
        if (isNew) {
            workbook = new XSSFWorkbook(inputStream);
        } else {
            workbook = new HSSFWorkbook(inputStream);
        }
        sheet = workbook.getSheetAt(0);
    }
/**
     * 获取单元格
     * 
     * @param rownum
     * @param column
     * @return
     */
    public Cell getCell(int rownum, short column) {
        row = sheet.getRow(rownum);
        if (row == null) {
            return null;
        }
        return row.getCell(column);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值