JAvaでexcel生成とファイルダウンロードをローカルに保存(3)
5329 ワード
package com.dragon.action;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ExcelServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ExcelServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType("text/html");
// PrintWriter out = response.getWriter();
// out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");
// out.println("");
// out.println(" A Servlet ");
// out.println(" ");
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the GET method");
// out.println(" ");
// out.println("");
// out.flush();
// out.close();
this.doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
OutputStream out = response.getOutputStream();
// ,
//attachment 。 , inline
response.setHeader("Content-Disposition", "attachment; filename=TestExcel1.xls ");
// workbook
Workbook workbook = new HSSFWorkbook();
//
Sheet sheet = workbook.createSheet(" ");
//
Sheet sheet2 = workbook.createSheet();
//
workbook.setSheetName(1, " ");
//
HSSFCellStyle hssfCellStyle = (HSSFCellStyle) workbook.createCellStyle();
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//
//
Row row = sheet.createRow(0);
//
Cell cell = row.createCell(0);
//
cell.setCellValue(" ");
//
cell.setCellStyle(hssfCellStyle);
//
Cell cell1 = row.createCell(1);
//
cell1.setCellValue(" ");
cell1.setCellStyle(hssfCellStyle);
//
Cell cell2 = row.createCell(2);
//
cell2.setCellValue(" ");
cell2.setCellStyle(hssfCellStyle);
//
Cell cell3 = row.createCell(3);
//
cell3.setCellValue(" ");
cell3.setCellStyle(hssfCellStyle);
for (int i = 1; i <= 5; i++) {
//
Row rows = sheet.createRow(i);
//
Cell cells = rows.createCell(0);
//
cells.setCellValue(" "+i);
//
Cell cell1s = rows.createCell(1);
//
cell1s.setCellValue(" ");
//
Cell cell2s = rows.createCell(2);
//
cell2s.setCellValue(18+i);
//
Cell cell3s = rows.createCell(3);
//
cell3s.setCellValue(" "+i);
}
workbook.write(out);
System.out.println(" !");
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
index.jsp
My JSP 'index.jsp' starting page
poi : http://apache.fayea.com/poi/dev/bin/poi-bin-3.12-beta1-20150228.zip jar