123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package com.gyee.impala.common.util;
- import org.apache.poi.hssf.usermodel.*;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.ss.util.CellRangeAddress;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- public class ExcelUtil2 {
- public static void createExcel(List<String> header, List<String[]> data, OutputStream out) throws IOException {
-
- HSSFWorkbook workbook = new HSSFWorkbook();
-
- HSSFSheet sheet = workbook.createSheet("sheet1");
-
- HSSFRow hssfRow = sheet.createRow(0);
-
- HSSFCellStyle cellStyle = workbook.createCellStyle();
-
- for (int i = 0; i < header.size(); i++) {
- HSSFCell headCell = hssfRow.createCell(i);
- headCell.setCellValue(header.get(i));
- headCell.setCellStyle(cellStyle);
- }
-
- for (int i = 0; i < data.size(); i++) {
- String[] strings = data.get(i);
- hssfRow = sheet.createRow(i + 1);
- for (int j = 0; j < strings.length; j++) {
- HSSFCell cell = hssfRow.createCell(j);
- cell.setCellValue(strings[j]);
- cell.setCellStyle(cellStyle);
- }
- }
-
- sheet.autoSizeColumn(2,true);
-
- workbook.write(out);
- }
- public static void createExcel(String title,List<String> header, List<String[]> data, OutputStream out) throws IOException {
-
- HSSFWorkbook workbook = new HSSFWorkbook();
-
- HSSFSheet sheet = workbook.createSheet("sheet1");
-
- HSSFRow hssfRow = sheet.createRow(0);
- HSSFCell cellTitle = hssfRow.createCell(0);
-
- HSSFCellStyle cellStyle = workbook.createCellStyle();
-
- HSSFCellStyle titleCellStyle = workbook.createCellStyle();
-
- HSSFFont font = workbook.createFont();
- font.setFontHeightInPoints((short) 12);
- titleCellStyle.setFont(font);
-
- cellTitle.setCellValue(title);
- int lastCol = header.size() > 1 ? header.size() : 2;
- CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) lastCol - 1);
- sheet.addMergedRegion(region1);
- hssfRow = sheet.createRow(1);
- hssfRow = sheet.createRow(2);
- cellTitle.setCellStyle(titleCellStyle);
-
- for (int i = 0; i < header.size(); i++) {
- HSSFCell headCell = hssfRow.createCell(i);
- headCell.setCellValue(header.get(i));
- headCell.setCellStyle(cellStyle);
- }
-
- for (int i = 0; i < data.size(); i++) {
- String[] strings = data.get(i);
- hssfRow = sheet.createRow(i + 3);
- for (int j = 0; j < strings.length; j++) {
- HSSFCell cell = hssfRow.createCell(j);
- cell.setCellValue(strings[j]);
- cell.setCellStyle(cellStyle);
- }
- }
-
- workbook.write(out);
- }
-
- public static List<String[]> readData(String fileType, int startRows, boolean ignoreRowBlank, InputStream is) throws IOException {
- List<String[]> result = new ArrayList<>();
- Workbook wb = readExcel(fileType, is);
- for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
- Sheet sheet = wb.getSheetAt(sheetIndex);
- for (int rowIndex = startRows, z = sheet.getLastRowNum(); rowIndex <= z; rowIndex++) {
- Row row = sheet.getRow(rowIndex);
- if (row == null) {
- continue;
- }
- int rowSize = sheet.getRow(0).getLastCellNum();
- String[] values = new String[rowSize];
- boolean hasValue = false;
- for (int columnIndex = 0; columnIndex < rowSize; columnIndex++) {
- String value = "";
- Cell cell = row.getCell(columnIndex);
- if (cell != null) {
-
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_STRING:
- value = cell.getStringCellValue();
- break;
- case HSSFCell.CELL_TYPE_NUMERIC:
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- Date date = cell.getDateCellValue();
- if (date != null) {
- value = new SimpleDateFormat("yyyy-MM-dd")
- .format(date);
- } else {
- value = "";
- }
- } else {
-
- if (HSSFDateUtil.isCellDateFormatted(cell)) {
- value = String.valueOf(cell.getDateCellValue());
- } else {
- cell.setCellType(Cell.CELL_TYPE_STRING);
- String temp = cell.getStringCellValue();
-
- if (temp.indexOf(".") > -1) {
- value = String.valueOf(new Double(temp)).trim();
- } else {
- value = temp.trim();
- }
- }
- }
- break;
- case HSSFCell.CELL_TYPE_FORMULA:
-
- if (!cell.getStringCellValue().equals("")) {
- value = cell.getStringCellValue();
- } else {
- value = cell.getNumericCellValue() + "";
- }
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- break;
- case HSSFCell.CELL_TYPE_ERROR:
- value = "";
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- value = (cell.getBooleanCellValue() == true ? "Y"
- : "N");
- break;
- default:
- value = "";
- }
- }
- values[columnIndex] = value;
- if (!value.isEmpty()) {
- hasValue = true;
- }
- }
- if (!ignoreRowBlank || hasValue) {
- result.add(values);
- }
- }
- }
- return result;
- }
-
- private static Workbook readExcel(String fileType, InputStream is) throws IOException {
- if ("xls".equals(fileType)) {
- return new HSSFWorkbook(is);
- } else if ("xlsx".equals(fileType)) {
- return new XSSFWorkbook(is);
- } else {
- throw new IllegalArgumentException("不支持的文件类型,仅支持xls和xlsx");
- }
- }
-
- private static String rightTrim(String str) {
- if (str == null) {
- return "";
- }
- int length = str.length();
- for (int i = length - 1; i >= 0; i--) {
- if (str.charAt(i) != 0x20) {
- break;
- }
- length--;
- }
- return str.substring(0, length);
- }
- }
|