阅读视图

一个解析Excel2007的POI工具类

通过apache-poi解析读取excel2007表格中的文字和图片,数字按照字符形式读取,表格中的图片和文字都按照行和列顺序读取到二维数组中相应的位置上。

package com.util;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ooxml.POIXMLDocumentPart;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.*;import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class POIUtil {    /**     * 读入excel2007文件     *     * @param file     * @throws IOException     */    public static List<String[]> readExcel(String fileName, byte[] bytes, int sheetNum) throws IOException {        // 获取excel文件的io流        InputStream is = new ByteArrayInputStream(bytes);        // 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象        Workbook workbook =  new XSSFWorkbook(is);;        // 创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回        List<String[]> list = new ArrayList<String[]>();        if (workbook != null) {            // for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {            // 获得当前sheet工作表            Sheet sheet = workbook.getSheetAt(sheetNum);            // if (sheet == null) {            // continue;            // }            // 获得当前sheet的开始行            int firstRowNum = sheet.getFirstRowNum();            // 获得当前sheet的结束行            int lastRowNum = sheet.getLastRowNum();            // 循环除了第一行的所有行            for (int rowNum = firstRowNum + 0; rowNum <= lastRowNum; rowNum++) {                // 获得当前行                Row row = sheet.getRow(rowNum);                if (row == null || row.getPhysicalNumberOfCells()==0) {                    continue;                }                // 获得当前行的开始列                int firstCellNum = row.getFirstCellNum();                // 获得当前行的列数                int lastCellNum = row.getPhysicalNumberOfCells();                String[] cells = new String[row.getPhysicalNumberOfCells()];                // 循环当前行                for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {                    Cell cell = row.getCell(cellNum);                    cells[cellNum] = getCellValue(cell);                }                list.add(cells);            }            // }            workbook.close();        }        return list;    }    private static String getCellValue(Cell cell) {        String cellValue = "";        if (cell == null) {            return cellValue;        }        // 把数字当成String来读,避免出现1读成1.0的情况        if (cell.getCellType() == CellType.NUMERIC) {            cell.setCellType(CellType.STRING);        }        // 判断数据的类型        switch (cell.getCellType()) {            case NUMERIC: // 数字                cellValue = String.valueOf(cell.getNumericCellValue());                break;            case STRING: // 字符串                cellValue = String.valueOf(cell.getStringCellValue());                break;            case BOOLEAN: // Boolean                cellValue = String.valueOf(cell.getBooleanCellValue());                break;            case FORMULA: // 公式                cellValue = String.valueOf(cell.getCellFormula());                break;            case BLANK: // 空值                cellValue = "";                break;            case ERROR: // 故障                cellValue = "非法字符";                break;            default:                cellValue = "未知类型";                break;        }        return cellValue;    }    public static Map<String, byte[]> getExcelPictures(String fileName, byte[] bytes, int sheetNum) throws IOException {        Map<String, byte[]> map = new HashMap<String, byte[]>();        // 获取excel文件的io流        InputStream is = new ByteArrayInputStream(bytes);        // 获得Workbook工作薄对象        Workbook workbook =  new XSSFWorkbook(is);;        XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetNum);        List<POIXMLDocumentPart> list = sheet.getRelations();        for (POIXMLDocumentPart part : list) {            if (part instanceof XSSFDrawing) {                XSSFDrawing drawing = (XSSFDrawing) part;                List<XSSFShape> shapes = drawing.getShapes();                for (XSSFShape shape : shapes) {                    XSSFPicture picture = (XSSFPicture) shape;                    XSSFClientAnchor anchor = picture.getPreferredSize();                    CTMarker marker = anchor.getFrom();                    String key = marker.getRow() + "-" + marker.getCol();                    byte[] data = picture.getPictureData().getData();                    map.put(key, data);                }            }        }        return map;    }}
  •  

一个通用的CloseableHttpClient工厂类

一个CloseableHttpClient工厂,基于java知名开源库apache-httpclient,能够忽略SSL,并且超时和状态异常时可以重试

<dependencies>    <dependency>        <groupId>org.apache.httpcomponents</groupId>        <artifactId>httpclient</artifactId>        <version>4.5.9</version>    </dependency>    <dependency>        <groupId>org.apache.httpcomponents</groupId>        <artifactId>httpmime</artifactId>        <version>4.5.9</version>    </dependency></dependencies>
package util;import lombok.extern.slf4j.Slf4j;import org.apache.http.ConnectionClosedException;import org.apache.http.HttpResponse;import org.apache.http.NoHttpResponseException;import org.apache.http.client.CookieStore;import org.apache.http.client.ServiceUnavailableRetryStrategy;import org.apache.http.conn.ConnectTimeoutException;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.impl.client.BasicCookieStore;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.protocol.HttpContext;import org.apache.http.ssl.SSLContextBuilder;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLHandshakeException;import java.net.SocketTimeoutException;@Slf4jpublic class HttpClientUtil {    public static CloseableHttpClient createSSLClientDefault() {        try {            SSLContextBuilder sslContextBuilder = new SSLContextBuilder();            SSLContext sslContext = sslContextBuilder.loadTrustMaterial(null, (chain, authType) -> true).build();            SSLConnectionSocketFactory ssl = new SSLConnectionSocketFactory(                    sslContext,                    new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"},                    null,                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);            return HttpClients.custom()                    .setSSLSocketFactory(ssl)                    .setRetryHandler((e, executionCount, context) -> {                        if (executionCount <= 20) {                            if (e instanceof NoHttpResponseException                                    || e instanceof ConnectTimeoutException                                    || e instanceof SocketTimeoutException                                    || e instanceof SSLHandshakeException) {                                log.info("{} 异常, 重试 {}", e.getMessage(), executionCount);                                return true;                            }                        }                        return false;                    })                    .setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {                        @Override                        public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {                            int statusCode = response.getStatusLine().getStatusCode();                            if (statusCode != 200) {                                if (executionCount <= 20) {                                    log.info("{} 状态码异常, 重试 {}", statusCode, executionCount);                                    return true;                                }                            }                            return false;                        }                        @Override                        public long getRetryInterval() {                            return 0;                        }                    })                    .build();        }        catch (Exception e) {            throw new RuntimeException(e);        }    }}
  •