I am working in a project which is already developed. Now I have a task to generate excel file using Apache poi API.
My problem is I have PrinWriter object available instead of OutputStream.
workbook.write(outputStream);
How to tackle this situation?
File f=new File("c://newfolder//lijo.xls");
PrintWriter out=new PrintWriter(f);
FileOutputStream fout=new FileOutputStream(f);
for converting from one output stream to another just pass the reference value
example to convert file to fileoutput stream
File file=new File("c://lijo.xls");
FileOutputStream outputStream=new FileOutputStream(file);
If you work on web project you may use ServletOutputStream and doing something like this
Workbook wb = new HSSFWorkbook();
ServletOutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
Or you can use FileInputStream
Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));
Related
I am working on a Java function which is expected to return a protected Excel file (xlsx) in byte array.
public byte[] genProtectedExcel() {
SXSSFWorkbook workbook = new SXSSFWorkbook();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Code of cells fill in...
//Should be code of workbook encryption...
workbook.write(baos);
return baos.toByteArray();
}
I found a solution which used the Encryption support of Apache POI: How to Protect Excel Workbook Via SXSSF?
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");
InputStream is = <open the SXSSF workbook as stream>
OutputStream os = enc.getDataStream(fs);
IOUtils.copy(is, os);
However, OutputStream cannot be cast into ByteArrayOutputStream. I tried to find a way to convert the OutputStream into ByteArray but all failed.
Is there anyway to create a protect excel byte array?
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fs.writeFilesystem(baos );
return baos.toByteArray();
I want to protect a .doc file with password using apache poi. I am getting this error while running code. please help me
Exception in thread "main"
org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The
supplied data appears to be in the OLE2 Format. You are calling the
part of POI that deals with OOXML (Office Open XML) Documents. You
need to call a different part of POI to process this data (eg HSSF
instead of XSSF) at
org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179)
at
org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:237)
at
org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:134)
at
org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:117)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
POIFSFileSystem fs=new POIFSFileSystem();
EncryptionInfo info=new EncryptionInfo(EncryptionMode.agile);
Encryptor enc=info.getEncryptor();
enc.confirmPassword("user");
OPCPackage opc=OPCPackage.open("D:/Amar.doc", PackageAccess.READ_WRITE);
OutputStream os=enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream stream=new FileOutputStream("D:/ao.doc");
fs.writeFilesystem(stream);
stream.close();
System.out.println("running");
I checked and reference to Apache POI documentation, it says that the password encryption for .doc file support since version 3.17, so I give it a try.
It must use HWPFDocument instead to open your doc file.
Then you need to set password by:
Biff8EncryptionKey.setCurrentUserPassword(password);
Complete method:
public static void encryptDocFile(File inputDocFile, File outputDocFile, String password) {
try {
FileInputStream fileInput = new FileInputStream(inputDocFile);
BufferedInputStream bufferInput = new BufferedInputStream(fileInput);
POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);
// Setting password
Biff8EncryptionKey.setCurrentUserPassword(password);
HWPFDocument wordDoc = new HWPFDocument(poiFileSystem);
FileOutputStream fileOut = new FileOutputStream(outputDocFile);
wordDoc.write(fileOut);
bufferInput.close();
fileOut.close();
wordDoc.close();
System.out.println("Encrypted successfully");
} catch (IOException e) {
System.out.println("Failed to encrypt doc file");
e.printStackTrace();
}
}
Or you can checkout the complete code here:
If you have further question or feedback, please let me know. Thank you
I'm trying to decrypt a large file using
https://github.com/martinwithaar/Encryptor4j/blob/master/src/main/java/org/encryptor4j/util/FileEncryptor.java
the following is part of decrypt method that i modified
//the following does not work
FileInputStream fis=new FileInputStream(src);
fis.skip(83);
is = encryptor.wrapInputStream(fis);
os = new FileOutputStream(dest);
//the copy method is a default method from FileEncryptor.java
copy(is, os);
the following works fine.(i have to decrypt entire file and then read/save part of file to another file then delete the old one and rename the new one to the old file name.
is = encryptor.wrapInputStream(new FileInputStream(src));
os = new FileOutputStream(dest);
copy(is, os);
FileInputStream fis=new FileInputStream(dest);
fis.skip(67);
FileOutputStream fos=new FileOutputStream(dest+".2");
copy(fis,fos);
new File(dest).delete();
new File(dest+".2").renameTo(new File(dest));
fis.close();
fos.close();
my question is, why the code on top does not work?
I followed http://www.tutorialspoint.com/java/io/fileinputstream_skip.htm on how to skip some bytes.
Because the encrypted stream has state. The encryption of the 84th byte depends on the prior encryption history. Try this:
FileInputStream fis=new FileInputStream(src);
is = encryptor.wrapInputStream(fis);
is.skip(83);
os = new FileOutputStream(dest);
// ...
in a java-class i just created workbook like this
XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(fis);
and filled cells with its corresponding celltype.
now i need to write it as a xxx.xlsx file same time return a BuyteArrayOutputStream.
to do so,
FileOutputStream fos = new FileOutputStream(tempFile);
workbook.write(fos);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
return outputStream;
here writing xxx.xlsx fils done successfully but same time it returns outputStream as null?
How do achieve it both?
first i write it into the outputstream and get byte[]. after that through IOUtils.write() from Apache Commons IO writing as a file later.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
byte[] b = outputStream.toByteArray();
FileOutputStream fos = new FileOutputStream(tempFile);
IOUtils.write(b, fos);
return outputStream;
5i need to convert a HSSFWorkbook (Apache's POI) to an ByteArray and later to convert the ByteArray back to the HSSFWorkbook. The following TestCase illustrates my problem:
#Test
public void testXLSExportImport(){
try {
InputStream is = new FileInputStream(FILEPATH);
HSSFWorkbook wb = new HSSFWorkbook(is);
byte[] exported = wb.getBytes();
HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(exported));
//in the line above the exception is thrown
} catch (Exception e) {
assertTrue(false);
}
}
the testcase fails with the exception: java.io.IOException: Invalid header signature; read 0x0005060000100809, expected 0xE11AB1A1E011CFD0
(I am using Apaches POI 3.5-beta3)
I hope somebody can help me... how can I get it work?!
You're not writing the workbook correctly! You need to use the write(Outputstream) call.
As shown in the various examples on the website, your code should instead be:
InputStream is = new FileInputStream(FILEPATH);
HSSFWorkbook wb = new HSSFWorkbook(is);
ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out);
HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
Also, don't open a Workbook from a FileInputStream, if you have a File use that directly. Opening from a File uses less memory than from a Stream.