1. 程式人生 > >Java POI 操作Excel(讀取/寫入)

Java POI 操作Excel(讀取/寫入)

del sep ces 價值 name fill ber 路徑 stc

pom.xml依賴:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>

Java類ExcelUtils代碼:

package com.dzpykj.files.excel;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.google.common.collect.Maps; /** * Excel操作工具類 * @author ChaiXY */ public class ExcelUtils { // @Value("${file_base_path}") // private static String fileBasePath;//文件的基礎路徑 // private static String fileBasePath = System.getProperty("user.dir") + File.separator + "excel" + File.separator;;//文件的基礎路徑 public static final String OFFICE_EXCEL_XLS = "xls"; public static final String OFFICE_EXCEL_XLSX = "xlsx"; /** * 讀取指定Sheet也的內容 * @param filepath filepath 文件全路徑 * @param sheetNo sheet序號,從0開始,如果讀取全文sheetNo設置null */ public static String readExcel(String filepath, Integer sheetNo) throws EncryptedDocumentException, InvalidFormatException, IOException { StringBuilder sb = new StringBuilder(); Workbook workbook = getWorkbook(filepath); if (workbook != null) { if (sheetNo == null) { int numberOfSheets = workbook.getNumberOfSheets(); for (int i = 0; i < numberOfSheets; i++) { Sheet sheet = workbook.getSheetAt(i); if (sheet == null) { continue; } sb.append(readExcelSheet(sheet)); } } else { Sheet sheet = workbook.getSheetAt(sheetNo); if (sheet != null) { sb.append(readExcelSheet(sheet)); } } } return sb.toString(); } /** * 根據文件路徑獲取Workbook對象 * @param filepath 文件全路徑 */ public static Workbook getWorkbook(String filepath) throws EncryptedDocumentException, InvalidFormatException, IOException { InputStream is = null; Workbook wb = null; if (StringUtils.isBlank(filepath)) { throw new IllegalArgumentException("文件路徑不能為空"); } else { String suffiex = getSuffiex(filepath); if (StringUtils.isBlank(suffiex)) { throw new IllegalArgumentException("文件後綴不能為空"); } if (OFFICE_EXCEL_XLS.equals(suffiex) || OFFICE_EXCEL_XLSX.equals(suffiex)) { try { is = new FileInputStream(filepath); wb = WorkbookFactory.create(is); } finally { if (is != null) { is.close(); } if (wb != null) { wb.close(); } } } else { throw new IllegalArgumentException("該文件非Excel文件"); } } return wb; } /** * 獲取後綴 * @param filepath filepath 文件全路徑 */ private static String getSuffiex(String filepath) { if (StringUtils.isBlank(filepath)) { return ""; } int index = filepath.lastIndexOf("."); if (index == -1) { return ""; } return filepath.substring(index + 1, filepath.length()); } private static String readExcelSheet(Sheet sheet) { StringBuilder sb = new StringBuilder(); if(sheet != null){ int rowNos = sheet.getLastRowNum();// 得到excel的總記錄條數 for (int i = 0; i <= rowNos; i++) {// 遍歷行 Row row = sheet.getRow(i); if(row != null){ int columNos = row.getLastCellNum();// 表頭總共的列數 for (int j = 0; j < columNos; j++) { Cell cell = row.getCell(j); if(cell != null){ cell.setCellType(CellType.STRING); sb.append(cell.getStringCellValue() + " "); // System.out.print(cell.getStringCellValue() + " "); } } // System.out.println(); } } } return sb.toString(); } /** * 讀取指定Sheet頁的表頭 * @param filepath filepath 文件全路徑 * @param sheetNo sheet序號,從0開始,必填 */ public static Row readTitle(String filepath, int sheetNo) throws IOException, EncryptedDocumentException, InvalidFormatException { Row returnRow = null; Workbook workbook = getWorkbook(filepath); if (workbook != null) { Sheet sheet = workbook.getSheetAt(sheetNo); returnRow = readTitle(sheet); } return returnRow; } /** * 讀取指定Sheet頁的表頭 */ public static Row readTitle(Sheet sheet) throws IOException { Row returnRow = null; int totalRow = sheet.getLastRowNum();// 得到excel的總記錄條數 for (int i = 0; i < totalRow; i++) {// 遍歷行 Row row = sheet.getRow(i); if (row == null) { continue; } returnRow = sheet.getRow(0); break; } return returnRow; } /** * 創建Excel文件 * @param filepath filepath 文件全路徑 * @param sheetName 新Sheet頁的名字 * @param titles 表頭 * @param values 每行的單元格 */ public static boolean writeExcel(String filepath, String sheetName, List<String> titles, List<Map<String, Object>> values) throws IOException { boolean success = false; OutputStream outputStream = null; if (StringUtils.isBlank(filepath)) { throw new IllegalArgumentException("文件路徑不能為空"); } else { String suffiex = getSuffiex(filepath); if (StringUtils.isBlank(suffiex)) { throw new IllegalArgumentException("文件後綴不能為空"); } Workbook workbook; if ("xls".equals(suffiex.toLowerCase())) { workbook = new HSSFWorkbook(); } else { workbook = new XSSFWorkbook(); } // 生成一個表格 Sheet sheet; if (StringUtils.isBlank(sheetName)) { // name 為空則使用默認值 sheet = workbook.createSheet(); } else { sheet = workbook.createSheet(sheetName); } // 設置表格默認列寬度為15個字節 sheet.setDefaultColumnWidth((short) 15); // 生成樣式 Map<String, CellStyle> styles = createStyles(workbook); // 創建標題行 Row row = sheet.createRow(0); // 存儲標題在Excel文件中的序號 Map<String, Integer> titleOrder = Maps.newHashMap(); for (int i = 0; i < titles.size(); i++) { Cell cell = row.createCell(i); cell.setCellStyle(styles.get("header")); String title = titles.get(i); cell.setCellValue(title); titleOrder.put(title, i); } // 寫入正文 Iterator<Map<String, Object>> iterator = values.iterator(); // 行號 int index = 1; while (iterator.hasNext()) { row = sheet.createRow(index); Map<String, Object> value = iterator.next(); for (Map.Entry<String, Object> map : value.entrySet()) { // 獲取列名 String title = map.getKey(); // 根據列名獲取序號 int i = titleOrder.get(title); // 在指定序號處創建cell Cell cell = row.createCell(i); // 設置cell的樣式 if (index % 2 == 1) { cell.setCellStyle(styles.get("cellA")); } else { cell.setCellStyle(styles.get("cellB")); } // 獲取列的值 Object object = map.getValue(); // 判斷object的類型 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (object instanceof Double) { cell.setCellValue((Double) object); } else if (object instanceof Date) { String time = simpleDateFormat.format((Date) object); cell.setCellValue(time); } else if (object instanceof Calendar) { Calendar calendar = (Calendar) object; String time = simpleDateFormat.format(calendar.getTime()); cell.setCellValue(time); } else if (object instanceof Boolean) { cell.setCellValue((Boolean) object); } else { if (object != null) { cell.setCellValue(object.toString()); } } } index++; } try { outputStream = new FileOutputStream(filepath); workbook.write(outputStream); success = true; } finally { if (outputStream != null) { outputStream.close(); } if (workbook != null) { workbook.close(); } } return success; } } /** * 設置格式 */ private static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = Maps.newHashMap(); // 標題樣式 XSSFCellStyle titleStyle = (XSSFCellStyle) wb.createCellStyle(); titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平對齊 titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直對齊 titleStyle.setLocked(true); // 樣式鎖定 titleStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 16); titleFont.setBold(true); titleFont.setFontName("微軟雅黑"); titleStyle.setFont(titleFont); styles.put("title", titleStyle); // 文件頭樣式 XSSFCellStyle headerStyle = (XSSFCellStyle) wb.createCellStyle(); headerStyle.setAlignment(HorizontalAlignment.CENTER); headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); // 前景色 headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 顏色填充方式 headerStyle.setWrapText(true); headerStyle.setBorderRight(BorderStyle.THIN); // 設置邊界 headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderLeft(BorderStyle.THIN); headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderTop(BorderStyle.THIN); headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); headerStyle.setBorderBottom(BorderStyle.THIN); headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); Font headerFont = wb.createFont(); headerFont.setFontHeightInPoints((short) 12); headerFont.setColor(IndexedColors.WHITE.getIndex()); titleFont.setFontName("微軟雅黑"); headerStyle.setFont(headerFont); styles.put("header", headerStyle); Font cellStyleFont = wb.createFont(); cellStyleFont.setFontHeightInPoints((short) 12); cellStyleFont.setColor(IndexedColors.BLUE_GREY.getIndex()); cellStyleFont.setFontName("微軟雅黑"); // 正文樣式A XSSFCellStyle cellStyleA = (XSSFCellStyle) wb.createCellStyle(); cellStyleA.setAlignment(HorizontalAlignment.CENTER); // 居中設置 cellStyleA.setVerticalAlignment(VerticalAlignment.CENTER); cellStyleA.setWrapText(true); cellStyleA.setBorderRight(BorderStyle.THIN); cellStyleA.setRightBorderColor(IndexedColors.BLACK.getIndex()); cellStyleA.setBorderLeft(BorderStyle.THIN); cellStyleA.setLeftBorderColor(IndexedColors.BLACK.getIndex()); cellStyleA.setBorderTop(BorderStyle.THIN); cellStyleA.setTopBorderColor(IndexedColors.BLACK.getIndex()); cellStyleA.setBorderBottom(BorderStyle.THIN); cellStyleA.setBottomBorderColor(IndexedColors.BLACK.getIndex()); cellStyleA.setFont(cellStyleFont); styles.put("cellA", cellStyleA); // 正文樣式B:添加前景色為淺黃色 XSSFCellStyle cellStyleB = (XSSFCellStyle) wb.createCellStyle(); cellStyleB.setAlignment(HorizontalAlignment.CENTER); cellStyleB.setVerticalAlignment(VerticalAlignment.CENTER); cellStyleB.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); cellStyleB.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyleB.setWrapText(true); cellStyleB.setBorderRight(BorderStyle.THIN); cellStyleB.setRightBorderColor(IndexedColors.BLACK.getIndex()); cellStyleB.setBorderLeft(BorderStyle.THIN); cellStyleB.setLeftBorderColor(IndexedColors.BLACK.getIndex()); cellStyleB.setBorderTop(BorderStyle.THIN); cellStyleB.setTopBorderColor(IndexedColors.BLACK.getIndex()); cellStyleB.setBorderBottom(BorderStyle.THIN); cellStyleB.setBottomBorderColor(IndexedColors.BLACK.getIndex()); cellStyleB.setFont(cellStyleFont); styles.put("cellB", cellStyleB); return styles; } /** * 將源文件的內容復制到新Excel文件(可供理解Excel使用,使用價值不大) * @param srcFilepath 源文件全路徑 * @param desFilepath 目標文件全路徑 */ public static void writeExcel(String srcFilepath, String desFilepath) throws IOException, EncryptedDocumentException, InvalidFormatException { FileOutputStream outputStream = null; String suffiex = getSuffiex(desFilepath); if (StringUtils.isBlank(suffiex)) { throw new IllegalArgumentException("文件後綴不能為空"); } Workbook workbook_des; if ("xls".equals(suffiex.toLowerCase())) { workbook_des = new HSSFWorkbook(); } else { workbook_des = new XSSFWorkbook(); } Workbook workbook = getWorkbook(srcFilepath); if (workbook != null) { int numberOfSheets = workbook.getNumberOfSheets(); for (int k = 0; k < numberOfSheets; k++) { Sheet sheet = workbook.getSheetAt(k); Sheet sheet_des = workbook_des.createSheet(sheet.getSheetName()); if (sheet != null) { int rowNos = sheet.getLastRowNum(); for (int i = 0; i <= rowNos; i++) { Row row = sheet.getRow(i); Row row_des = sheet_des.createRow(i); if(row != null){ int columNos = row.getLastCellNum(); for (int j = 0; j < columNos; j++) { Cell cell = row.getCell(j); Cell cell_des = row_des.createCell(j); if(cell != null){ cell.setCellType(CellType.STRING); cell_des.setCellType(CellType.STRING); cell_des.setCellValue(cell.getStringCellValue()); } } } } } } } try { outputStream = new FileOutputStream(desFilepath); workbook_des.write(outputStream); } finally { if (outputStream != null) { outputStream.close(); } if (workbook != null) { workbook_des.close(); } } } public static void main(String[] args) { } }

Java POI 操作Excel(讀取/寫入)