0

I am trying to write a program where I have to either

  1. create an exel file and insert a table (and eventually data) into it, OR

  2. duplicate a template exel file that I have made, and copy that over to a new directory to use.

I have gotten the 'duplicate' part working, but I cannot open the duplicated file (It says the file format/extension is not valid).

enter image description here

This is the code:

try {
                    var template = new RandomAccessFile(App.NAME+".xlsx", "rw");
                    var copy = new RandomAccessFile(App.data.getFilePath()+App.NAME+".xlsx", "rw");

                    var sourceChannel = template.getChannel();
                var destinationChannel = copy.getChannel();
                destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());

                System.out.println("Successfully created exel file");

            } catch (IOException e) {
                System.err.println("Error creating exel file: " + e.getMessage());
            }

Does anyone know what I should do to fix this? Thanks in advance.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 4
    [`Files.copy`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.nio.file.Path-java.nio.file.Path-java.nio.file.CopyOption...-) is probably a good thing to try. – Andy Turner Jan 13 '20 at 20:03
  • 1
    You just want to copy a file? That has nothing to do with Excel itself. Do a single call to `Files.copy(...)`, done. – Zabuzard Jan 13 '20 at 20:06
  • Files.copy worked great! thanks :) – user12684959 Jan 13 '20 at 20:17

2 Answers2

0

The following example creates an Excel File named example.xls. The file has a table with two columns ( name, job ) and one row (bayrem, developer).

enter image description here

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Persons");
sheet.setColumnWidth(0, 6000); //style
sheet.setColumnWidth(1, 4000);//style

Row header = sheet.createRow(0);

CellStyle headerStyle = workbook.createCellStyle();//style
headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());//style
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//style

XSSFFont font = ((XSSFWorkbook) workbook).createFont();//style
font.setFontName("Arial");//style
font.setFontHeightInPoints((short) 16);//style
font.setBold(true);//style
headerStyle.setFont(font);//style

Cell headerCell = header.createCell(0);
headerCell.setCellValue("Name");
headerCell.setCellStyle(headerStyle);//style

headerCell = header.createCell(1);
headerCell.setCellValue("Job");
headerCell.setCellStyle(headerStyle);//style

CellStyle style = workbook.createCellStyle();//style
style.setWrapText(true);//style

Row row = sheet.createRow(2);
Cell cell = row.createCell(0);
cell.setCellValue("Bayrem");
cell.setCellStyle(style);//style

cell = row.createCell(1);
cell.setCellValue("Developer");
cell.setCellStyle(style);//style

File currDir = new File(".");
String path = currDir.getAbsolutePath();
String fileLocation = path.substring(0, path.length() - 1) + "example.xlsx";

FileOutputStream outputStream = new FileOutputStream(fileLocation);
workbook.write(outputStream);
workbook.close();
bayrem404
  • 76
  • 2
  • 6
0

This is all you need for a copy, the language level has to be 7 or higher

import java.io.IOException;
import java.nio.file.*;


public class ExcelCopy {

public static void main(String[] args) {

    FileSystem system = FileSystems.getDefault();
    Path original = system.getPath("C:\\etc\\etc\\Desktop\\ExcelTestOne.xlsx");
    Path target = system.getPath("C:\\etc\\etc\\Desktop\\ExcelCopy.xlsx");

    try {
        // Throws an exception if the original file is not found.
        Files.copy(original, target, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException ex) {
        System.out.println("ERROR");
    }
}

}

original post is here,I check that it worked for you. How to copy excel file?