Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
Servlet - Excel Sheet - DownLoads Data as excel Sheet.
This Example shows how to download Data as a Excel sheet By Using Java POI.
Step 1 :
Create a
FileDownloadServlet.java
-------------------------------------------
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public class FileDownloadServlet extends HttpServlet {
private static
final long serialVersionUID = 8305367618713715640L;
@Override
protected
void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected
void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
try {
DownloadHandler downloadHandler = new
DownloadHandler(req,resp);
String Result = downloadHandler.processAction();
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"error
occurred while processing: " + e.getMessage());
}
}
}
Step 2
:
Implement DownloadHandler.java
-------------------------------------------------------------------------------------
import java.io.OutputStream;
import
java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession;
import
org.apache.poi.hssf.usermodel.HSSFRow;
import
org.apache.poi.hssf.usermodel.HSSFSheet;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook;
import
com.comcast.commsales.trunking.client.model.CustomerDataVO;
import
com.comcast.commsales.trunking.client.model.validator.ValueValidator;
import
com.comcast.commsales.trunking.client.proxy.TrunkingService;
public class DownloadHandler {
HttpServletRequest req;
HttpServletResponse resp;
public DownloadHandlerImpl(){
}
public DownloadHandlerImpl(HttpServletRequest req,
HttpServletResponse resp){
this.req
= req;
this.resp = resp;
}
@SuppressWarnings("unchecked")
public String processAction() throws Exception
{
OutputStream
os = resp.getOutputStream();
HttpSession session =
req.getSession();
Boolean isTemplate =
(Boolean)session.getAttribute("isTemplate");
ArrayList<CustomerDataVO> CustomerDataVOList
= (
ArrayList<CustomerDataVO>)session.getAttribute("exportedCustomerData");
session.removeAttribute("isTemplate"); //Remove
the Session Attribute
session.removeAttribute("exportedTFS"); //Remove
the Session Attribute
resp.setContentType("application/vnd.ms-excel");
String
displayname="tollFree.xls";
resp.setHeader("Content-Disposition","inline;
filename=\"" + displayname +
"\"");
try{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel
Sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell((short) 0).setCellValue("Customer
FistName");
rowhead.createCell((short) 1).setCellValue("Customer
LastName");
if(isTemplate != null &&
!isTemplate.booleanValue()){
int index = 1;
for(CustomerDataVO vo : customerDataVOList){
HSSFRow row1 = sheet.createRow((short)
index);
row1.createCell((short) 0).setCellValue(vo.getFirstName());
row1.createCell((short) 1).setCellValue(vo.getLastName());
index++;
}
}else{
HSSFRow row1 =
sheet.createRow((short)1);
row1.createCell((short)
0).setCellValue("xxxxxxxx");
row1.createCell((short)
1).setCellValue("xxxxxxxx");
HSSFRow row2 = sheet.createRow((short) 2);
row2.createCell((short)
0).setCellValue("Required");
row2.createCell((short)
1).setCellValue("Required");
}
wb.write(os);
}catch(Exception e){
}finally{
os.flush();
os.close();
resp.flushBuffer();
}
return
"";
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.