Skip to Main Content
  • Questions
  • How to save an excel file in Windows local directoty from database.

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mohamed.

Asked: May 05, 2016 - 12:42 pm UTC

Last updated: May 06, 2016 - 8:49 am UTC

Version: 11.2.0.3.0

Viewed 1000+ times

You Asked

Hi Tom,

I have created a Java program as below and have compiled it as Java object in my Oracle Database. This Java program works fine and creates an excel file and save it in my Window's C:\ folder when I try it as from my eclipse IDE.
But when I call the same in my database it is not throwing any error, but the file is not generated in the specified path.

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "CreateExcel"
AS package CreateExcel;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class CreateExlFile{
public static java.lang.String GenerateExcel() {
try {
String filename = "C:\\Temp\\NewExcelFile.xls" ;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell(0).setCellValue("No.");
rowhead.createCell(1).setCellValue("Name");
rowhead.createCell(2).setCellValue("Email");
HSSFRow row = sheet.createRow((short)1);
row.createCell(0).setCellValue("1");
row.createCell(1).setCellValue("MohamedAnas");
row.createCell(2).setCellValue("MohamedAnas@gmail.com");
FileOutputStream fileOut = new FileOutputStream(filename);
System.out.println(fileOut);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");

} catch ( Exception ex ) {
System.out.println(ex);
return "1";
}
return "0";
}

}

CREATE or replace function CreateExcelFile return varchar2
AS LANGUAGE JAVA
NAME 'CreateExcel.CreateExlFile.GenerateExcel() return java.lang.String';
/

select CreateExcelFile from dual;

This is giving me an output as 0 as there is no error. But I cannot find the file in my C:\Temp\ path.

and Chris said...

Java in the database... I much prefer PL/SQL myself.

Is the database running on your machine or a server? I think you'll find it's written to c:\temp on the database server (assuming it uses Windows)

In any case, I'll point you to Anton Scheffer's as_xlsx PL/SQL package. This enables you to write Excel files to the database server:

https://technology.amis.nl/2011/02/19/create-an-excel-file-with-plsql/

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Mohamed Anas, May 06, 2016 - 5:37 am UTC

Thanks for the replay. But I think the solution you have provided is again writing the excel in application server. My requirement is to write it in my local Windows directory. And the database is running on the server and not in my machine.
Connor McDonald
May 06, 2016 - 8:49 am UTC

If you write a file using code in the database, it will write it to the database server. This applies whether it's PL/SQL, Java, etc.

If you need it written to your local machine, you need to write an application that runs on your local machine or creates a file you can download to it.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library