Solved

Copy a excel file by Java

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 109
Chrome and Firefox Java 5 88
ejb entity bean example 2 57
java mysql insert application 14 68
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

739 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