java - How can I load an MS Excel file, add content and send to user? -


i need load template ms excel file, add content cells , download via user's browser, can open and/or save file.

i managed create ms excel workbooks in past, seems different. how it?

thanks in advance,

gtludwig

there tutorials on apache-poi on official site

final workbook wb; filein =new fileinputstream(fname); wb = workbookfactory.create(filein);//opening file final sheet sheet = wb.createsheet("report");// create sheet 

then use row , cell classes add contents in cell

row row; cell cell; row=sheet.getrow(0); //get existing row if(row==null) row=sheet.createrow(0);//if row not present create row cell=row.getcell(0); if(cell==null) cell=row.createcell(0); cell.setcelltype(cell.cell_type_numeric); cell.setcellvalue(0);//set cell value 

update : forgot mention need write contents on file

fout=new fileoutputstream(fname); wb.write(fout); 

and close fout in finally block

if(fout!=null) fout.close(); 

once done excel file create download servlet

file file=new file(fname); fileinputstream fis=new fileinputstream(file);          response.setheader("content-length", string.valueof(file.length()));             response.setheader("content-disposition",                              "attachment;filename="+fname+".xlsm");             servletcontext ctx = getservletcontext();             inputstream input = new bufferedinputstream(new fileinputstream(file), 1024 * 10);             outputstream output = new bufferedoutputstream(response.getoutputstream(), 1024 * 10);              byte[] buffer = new byte[1024 * 10];             int length;             while ((length = input.read(buffer)) > 0) {                 output.write(buffer, 0, length);             }         output.flush();         output.close();         input.close();         fis.close(); 

Comments