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;
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 have a requirement to store the uploaded spreadsheet to the disk. I am using the below code to do the same and receiving the file is corrupted error from the excel.
byte[] bytes = null;
File uploadedFile = new File("D://xlsxTest//BNG Issue.xlsx");
File file = new File("D://xlsxTest//sample1.xlsx");
FileOutputStream outStream = null;
InputStream inputStream = null;
try{
if(!file.exists()){
file.createNewFile();
}
outStream = new FileOutputStream(file);
if (uploadedFile != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
inputStream = new FileInputStream(uploadedFile);;
int c = 0;
while (c != -1) {
c = inputStream.read();
byteArrayOutputStream.write((char) c);
}
bytes = byteArrayOutputStream.toByteArray();
}
outStream.write(bytes);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
I tried using a file of 83KB size and the resultant file is 82.5KB.
Any help would be appreciated.
Thanks.
Try using Apace POI, Convert .xls or .xlsx file to byte array using OPCPackage and XSSFWorkbook with below code.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OPCPackage pkg = OPCPackage.open(new File(fullFileName));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
wb.write(baos);
byte[] fileContent = baos.toByteArray();
Apache POI takes care to extract all information of the excel file while converting to byte array.
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"));
Can someone explain how I can get a file object if I have only a ByteArrayOutputStream. How to create a file from a ByteArrayOutputStream?
You can do it with using a FileOutputStream and the writeTo method.
ByteArrayOutputStream byteArrayOutputStream = getByteStreamMethod();
try(OutputStream outputStream = new FileOutputStream("thefilename")) {
byteArrayOutputStream.writeTo(outputStream);
}
Source: "Creating a file from ByteArrayOutputStream in Java." on Code Inventions
You can use a FileOutputStream for this.
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("myFile"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Put data in your baos
baos.writeTo(fos);
} catch(IOException ioe) {
// Handle exception here
ioe.printStackTrace();
} finally {
fos.close();
}
What I'm doing is that the file and each line of data I'm reading for BufferOutputStream will be saved in a FileOutputStream and I want to specify the output data.
If i have understood correct, you want to download the file from S3 and write to your local directory using BufferedOutputStream .
S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
InputStream is = object.getObjectContent();
// Creating file
File file= new File(localFilePath);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos= new BufferedOutputStream(fos);
int read = -1;
while ((read = is.read()) != -1) {
bos.write(read);
}
bos.flush();
bos.close();
is.close();