如何复制.xlsx完整工作簿

时间:2015-11-17 04:42:44

标签: java apache-poi

我有解决方案在java中复制.xls工作簿但无法复制.xlsx工作簿。 任何人都有解决方案。 我搜索了google,stackoverflow并找到了只能复制xls文件的解决方案。

4 个答案:

答案 0 :(得分:1)

如果有人想要一个更简单的程序,只需使用Files.copy:

// Get the file you want to clone
File originalWb = new File("orginalWb.xlsx"); 

// The output file, you don't need to call clonedWb.createNewFile()
File clonedWb = new File("clonedWb.xlsx");

Files.copy(originalWb.toPath(), clonedW.toPath());

就是这样。

答案 1 :(得分:0)

使用Apache POI XSSF库:

public void copyFile(String sourcePath, String destinationPath) throws IOException {
    FileInputStream excelFile = new FileInputStream(new File(sourcePath));
    Workbook workbook = new XSSFWorkbook(excelFile);
    FileOutputStream outputStream = new FileOutputStream(destinationPath);
    workbook.write(outputStream);
    workbook.close();
}

答案 2 :(得分:0)

在内存中克隆工作簿(如果可以的话):

public static Workbook cloneWorkbook(final Workbook workbook) {
  try {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);
    workbook.write(outputStream);
    return WorkbookFactory.create(new ByteArrayInputStream(outputStream.toByteArray()));
  } catch (final IOException ex) {
    log.warn("Error cloning workbook", ex);
    return null; // or throw exception
  }
}

答案 3 :(得分:0)

Apache POI Busy Developers' Guide to HSSF and XSSF Features

POI对XLS/XLSX文件执行这些步骤。 XLS: HSSFWorkbook, XLSX: XSSFWorkbook

  1. 将文件读取为流(FileStream):将SystemFile转换为JavaFileObject
  2. 创建一个WorkBook表单流。流到XSSFWorkbook-JavaObject
  3. 使用POI函数,您可以在Java Workbook Object上执行CURD操作。
  4. 将Java Workbook对象转换为文件/流。

注意:索引从行/列/表的表格开始为0。

在表格上执行以下操作:

带有Sheet1, Sheet2的XLSX文件
getSheet_RemoveOthers(Sheet1)。 XLSX文件:Sheet1

public static XSSFSheet getSheet_RemoveOthers(String sourceFileSheetName) {
    XSSFSheet srcSheet = workBook.getSheet(sourceFileSheetName);
    //Sheet srcSheet = oldWorkbook.getSheetAt(0);
    
    int srcSheetIndex = workBook.getSheetIndex(srcSheet);
    System.out.println("srcSheetIndex:"+srcSheetIndex);
    
    int numberOfSheets = workBook.getNumberOfSheets();
    for (int indexAt = 0; indexAt < numberOfSheets; indexAt++) {
        if (srcSheetIndex == indexAt) {
            System.out.println("sourceFileSheetName:"+indexAt);
        } else {
            String sheetName = workBook.getSheetName(indexAt);
            System.out.println("Removing sheetName:"+sheetName);
            workBook.removeSheetAt(indexAt);
        }
    }
    
    System.out.println("getSheetName : "+ srcSheet.getSheetName() );
    int totalRows = srcSheet.getPhysicalNumberOfRows();
    System.out.println("Total Number of Rows : "+ totalRows );
    
    return srcSheet;
}

XSSFSheet.cloneSheet(Sheet1)。 XLSX文件:Sheet1, Sheet1 (2)

public static XSSFSheet cloneSheet(String sourceFileSheetName) {
    Sheet srcSheet = workBook.getSheet(sourceFileSheetName);
    int srcSheetIndex = workBook.getSheetIndex(srcSheet);
    System.out.println("srcSheetIndex:"+srcSheetIndex);
    XSSFSheet cloneSheet = workBook.cloneSheet(srcSheetIndex);
    return cloneSheet;
}

全长示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class POI_XLSX_Report {
    static String filePath = "C:/Yash/",
            sourceFile = filePath+"POIExcel.xlsx", sourceFileSheetName = "Sheet1",
            destinationFile = filePath+"POIExcelCopy.xlsx";
    
    static XSSFWorkbook workBook;
    public static void main(String[] args) throws Exception {
        File mySrcFile = new File(sourceFile);
        FileInputStream stream = new FileInputStream(mySrcFile);
        
        workBook = (XSSFWorkbook) WorkbookFactory.create( stream );
        
        XSSFSheet sheet_RemoveOthers = getSheet_RemoveOthers(sourceFileSheetName);
        setSheetValue(sheet_RemoveOthers, 4, 6, "Val2");
        
        // New Sheet with exact copy of Source-Sheet
        XSSFSheet cloneSheet = cloneSheet(sourceFileSheetName);
        setSheetValue(cloneSheet, 4, 6, "Val2");
        
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workBook.write( byteArrayOutputStream );
        byteArrayOutputStream.close();
        
        File clonedWb = new File(destinationFile);
        //Files.copy(mySrcFile.toPath(), clonedWb.toPath()); If a file is already available then throws exception
        
        // Write the output to a file
        FileOutputStream fileOutputStream = new FileOutputStream( clonedWb );
        byteArrayOutputStream.writeTo(fileOutputStream);
    }
    public static XSSFSheet getSheet_RemoveOthers(String sourceFileSheetName) {
        // ...
    }
    public static XSSFSheet cloneSheet(String sourceFileSheetName) {
        // ...
    }
    
    public static void setSheetValue(Sheet sheet, int colIndex, int rowIndex, String value) {
        // Row and Column index starts form 0
        rowIndex = rowIndex - 1;
        colIndex = colIndex - 1;
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
            System.out.println("createRow:");
            Row createRow = sheet.createRow(rowIndex);
            row= createRow;
        }
        
        short lastCellNum = row.getLastCellNum();
        System.out.println("Col:"+lastCellNum);
        
        Cell createCell = row.createCell(colIndex, CellType.STRING);
        System.out.println("New cell:"+createCell.getStringCellValue());
        createCell.setCellValue(value);
    }
}