按工作表读写Excel数据表

时间:2018-01-11 13:08:13

标签: java excel apache

您好我已经编写了一个Java类,可以逐页读取和写入Excel数据。

    package com.ge.dwf.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeSet;

    import org.apache.poi.EncryptedDocumentException;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.WorkbookFactory;
    import org.apache.poi.ss.util.CellRangeAddress;

    public class Util {
        public static void main(String[] args) throws IOException {
            FileInputStream file = new FileInputStream(new File("SiteData.xls"));
            // Create Workbook instance holding reference to .xlsx file
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                HSSFSheet sheet = workbook.getSheetAt(i);
                // create new sheet in the excel file.
                System.out.println("Sheet Name is" + sheet.getSheetName());
                HSSFSheet newSheet = createSheet(sheet.getSheetName());
                copySheets(newSheet, sheet, true);
            }
            workbook.close();
            file.close();
        }

        public static HSSFSheet createSheet(String SheetName) throws IOException {
            // Create a workbook object.
             Workbook workbook = new HSSFWorkbook();
             // Create two sheet by calling createSheet of workbook.
             HSSFSheet sheet=(HSSFSheet) workbook.createSheet(SheetName);
             // Create a FileOutputStream by passing the excel file name.
             FileOutputStream outputStream = new FileOutputStream("POINewSheetExample.xls");
             // Write the FileOutputStream to workbook object.
             workbook.write(outputStream);
             // Finally close the FileOutputStream.
             outputStream.close();
             return sheet;
        }

        public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet) {
            copySheets(newSheet, sheet, true);
        }

        public static void copySheets(HSSFSheet newSheet, HSSFSheet sheet, boolean copyStyle) {
            int maxColumnNum = 0;
            Map<Integer, HSSFCellStyle> styleMap = (copyStyle) ? new HashMap<Integer, HSSFCellStyle>() : null;
            for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
                HSSFRow srcRow = sheet.getRow(i);
                HSSFRow destRow = newSheet.createRow(i);
                if (srcRow != null) {
                    Util.copyRow(sheet, newSheet, srcRow, destRow, styleMap);
                    if (srcRow.getLastCellNum() > maxColumnNum) {
                        maxColumnNum = srcRow.getLastCellNum();
                    }
                }
            }
            for (int i = 0; i <= maxColumnNum; i++) {
                newSheet.setColumnWidth(i, sheet.getColumnWidth(i));
            }
        }

        public static void copyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow,
                Map<Integer, HSSFCellStyle> styleMap) {
            Set<CellRangeAddress> mergedRegions = new TreeSet<CellRangeAddress>();
            destRow.setHeight(srcRow.getHeight());
            for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
                HSSFCell oldCell = srcRow.getCell(j);
                HSSFCell newCell = destRow.getCell(j);
                if (oldCell != null) {
                    if (newCell == null) {
                        newCell = destRow.createCell(j);
                    }
                    copyCell(oldCell, newCell, styleMap);
                    CellRangeAddress mergedRegion = getMergedRegion(srcSheet, srcRow.getRowNum(),
                            (short) oldCell.getColumnIndex());
                    if (mergedRegion != null) {
                        CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.getFirstRow(),
                                mergedRegion.getFirstColumn(), mergedRegion.getLastRow(), mergedRegion.getLastColumn());
                        if (isNewMergedRegion(newMergedRegion, mergedRegions)) {
                            mergedRegions.add(newMergedRegion);
                            destSheet.addMergedRegion(newMergedRegion);
                        }
                    }
                }
            }

        }

        public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
            if (styleMap != null) {
                if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
                    newCell.setCellStyle(oldCell.getCellStyle());
                } else {
                    int stHashCode = oldCell.getCellStyle().hashCode();
                    HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
                    if (newCellStyle == null) {
                        newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                        styleMap.put(stHashCode, newCellStyle);
                    }
                    newCell.setCellStyle(newCellStyle);
                }
            }
            switch (oldCell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                newCell.setCellValue(oldCell.getStringCellValue());
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case HSSFCell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            default:
                break;
            }

        }

        public static CellRangeAddress getMergedRegion(HSSFSheet sheet, int rowNum, short cellNum) {
            for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress merged = sheet.getMergedRegion(i);
                if (merged.isInRange(rowNum, cellNum)) {
                    return merged;
                }
            }
            return null;
        }

        private static boolean isNewMergedRegion(CellRangeAddress newMergedRegion,
                Collection<CellRangeAddress> mergedRegions) {
            return !mergedRegions.contains(newMergedRegion);
        }

    }

执行此代码时,我收到以下错误: -

Exception in thread "main" java.lang.ClassCastException: org.apache.poi.ss.util.CellRangeAddress cannot be cast to java.lang.Comparable
    at java.util.TreeMap.getEntry(TreeMap.java:349)
    at java.util.TreeMap.containsKey(TreeMap.java:232)
    at java.util.TreeSet.contains(TreeSet.java:234)
    at com.ge.dwf.windresource.util.Util.isNewMergedRegion(Util.java:157)
    at com.ge.dwf.windresource.util.Util.copyRow(Util.java:95)
    at com.ge.dwf.windresource.util.Util.copySheets(Util.java:67)
    at com.ge.dwf.windresource.util.Util.main(Util.java:36)

任何人都可以帮助我知道我在哪里做错了吗?任何帮助都将受到高度赞赏。

我想逐页复制excel数据,因为我的原始Excel工作表包含我在复制详细信息时要删除的图表和其他图表。

0 个答案:

没有答案