skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 6, 2012

Writing output to Excel Sheet or Spread sheet from servlet

Posted by Raju Gupta at 2:10 AM – 0 comments
 
This solution doesn't use any third party jar files or tools. The program will just use the Setcontent type and printwriter object , which are key features for the solution.


/**
 *
 * The following java program demonstrates a simple way to write the output in to Excel sheets or Spread sheets
 * 
 */
class  Excel_Writing extends HttpServlet 
{
	public void doGet (HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		try
		{
			//This Type specifies that output is in the Excel format
			response.setContentType("application/vnd.ms-excel");
			PrintWriter out = null;
			out = response.getWriter(); 
			//Here the file name in which the printwriter is going to write
			response.setHeader("Content-disposition","attachment;filename=ExcelWriting.xls");
			// By using \t or tab makes the printwriter to write in different columns
			out.print("Name"+"\t");
			out.print("Company"+"\t");
			//To write in new row
			out.println(); 
			out.print("Raj"+"\t");
			out.print("Java Programming Tutorial"+"\t");
			out.println();
			out.close();
		}
		catch (Exception e)
		{
			System.out.println("Exception in Excel_Writing Class"+e.getMessage());
		}
	}
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger