Solved

Copy a excel file by Java

Posted on 2007-03-22
3
448 Views
Last Modified: 2012-06-27
I have a xls sheet with Macro, Initially I want to use Java to copy the file. Since there is no copy method in java.io.File. I have to read it to BufferedReader and write it to new File by BufferedWriter. Codes are below:
But the new xls is corrupted if using following codes. Do you know some other way? I have an alternative:
I write a bat file; it says copy originalFileName newFileName. Then in Java program, I just call this bat file. But I do not know how to call a bat file in Java. I would put this to another question, since there are two questions here. Please also bother to answer another. So I could give points.




FileReader templateFile = null;
        try{
            templateFile= new FileReader("c:/temp/temp/temp/template.xls");
        }catch(FileNotFoundException fe){
            System.out.println("template file not found");
            System.exit(-1);
        }
        BufferedReader reader = new BufferedReader(templateFile);
       
        String inputLine;
        try
        {
            BufferedWriter writer = new BufferedWriter(new FileWriter("c:/temp/temp/temp/pastedName.xls"));
            while((inputLine = reader.readLine())!=null){
                writer.write(inputLine);
                writer.flush();
            }
            reader.close();
            writer.close();
        }
        catch (IOException e1)
        {    
            String msg = "An error occurred: " + e1.getMessage();
            System.out.println(msg);
            System.exit(-1);
        }
0
Comment
Question by:JianJunShen
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 18770485
Use BufferedOutputStream/InputStream, not Read/Writer
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 18770492
use something like the following

http://www.objects.com.au/java/qa/1579575043.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18770574
:-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
how to see all occupied ports on windows 10 laptop 15 77
Running JavaFX on JDeveloper 12C 1 55
Java: The Public Class Main 4 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question