Solved

Copy a excel file by Java

Posted on 2007-03-22
3
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

617 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