java处理Excel

来源:互联网 发布:小岛秀夫离职原因知乎 编辑:程序博客网 时间:2024/06/05 10:54

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);    }
0 0