I am creating a web app where I want to show a excel icon in my results. When user clicks on icon, it should open up a spread sheet on clients machine with some data sent by server (clients will have excel installed).
I wrote some code to create excel on my local machine from the webservice.
public class App
{
public static void main( String[] args )
{
try {
URL oracle = new URL("http://someService.com");
URLConnection yc =null;
yc = oracle.openConnection();
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
int rowNum =0;
while ((inputLine = in.readLine()) != null) {
Row row = sheet.createRow(rowNum++);
String[] coloumns = inputLine.split("\t");
int cellNum =0;
for(String coloumn: coloumns){
coloumn.
Cell cell = row.createCell(cellNum++);
cell.setCellValue(coloumn);
}
System.out.println(inputLine);
}
in.close();
FileOutputStream out =
new FileOutputStream(new File("C:\\libraries\\new.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This works fine. All it does is it reads data coming from web service and creates a spreadsheet in my machine at C:\libraries\new.xls.
Now if I am on web app I want to open a spread sheet on clients machine. I don't have to save the sheet. Just open with the data.
How can I open spread sheet on client machine with this data from web service?
EDIT
Here is my new server code:
@RequestMapping(value = "/Excel")
public void getFile(HttpServletResponse response){
OutputStream out =null;
try {
out = response.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response.setContentType("application/x-ms-excel");
try {
URL oracle = new URL("http://someService.com");
URLConnection yc =null;
yc = oracle.openConnection();
//Get the workbook instance for XLS file
IOUtils.copy(yc.getInputStream(),out);
out.flush();
System.out.println("Excel written successfully..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
So now i ran the web app and nothing happened? Should i do something on front end to invoke this stream.