Solved

Copy a excel file by Java

Posted on 2007-03-22
3
445 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now