1

I would like to create a copy of word or excel file using poi. I know that poi is also used when reading a word or excel file. Reading means not only values but also attribute such as font size or table color and backgroud colors for each cells. Reading values and attribute of the xlsx or docx document, I want to make a copy of the word or Excel document as it is. Is it possible that the related source is open at open source on the any site?

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Grape111
  • 59
  • 5
  • 2
    If you want to *copy* the file content; why don't you just copy the file on a file system view? – GhostCat Apr 24 '17 at 13:11
  • 2
    You can use this API if it is just copy: https://docs.oracle.com/javase/tutorial/essential/io/copy.html – hubpixel Apr 24 '17 at 13:11

2 Answers2

1

read Apache POI or docx4j for dealing with docx documents

you can find the techniques related to adding text into document you can found out on https://www.slideshare.net/plutext/document-generation-2012osdcsydney

use POI's HWPF support. this is often enclosed in docx4j as a dependency. however its not an excellent approach, since it does not convert the doc to docx4j's internal representation:- you are kind of stuck in HWPF land use JODConverter to convert the doc to a docx, and if necessary, back again. this is often the simplest .

Community
  • 1
  • 1
1

To open an excel from one file, and save it to another file I use this code.

//open source excel
InputStream template = new FileInputStream("C:\\source excel path\\input.xlsx");
Workbook wb = WorkbookFactory.create(template);

//Saving excel to a different location or filename. 
FileOutputStream out = new FileOutputStream("C:\\path to copy excel to\\output.xlsx");
wb.write(out);
wb.close();
out.close();
template.close();
Matt
  • 60
  • 1
  • 8
  • If you're not changing the file, why bother opening it with POI to write out again? Why not just [do a straight copy](https://docs.oracle.com/javase/tutorial/essential/io/copy.html)? – Gagravarr Apr 24 '17 at 20:28
  • The question asked how to copy using POI. Personally I use it as a template before filling in data. If you use a lot of formatting, making a template in excel can save time coding. Also if you want to output an excel with macros, this is the only way to do that. – Matt Apr 24 '17 at 20:55