【くどくど、校長】JAVA WEB Excelレポートのエクスポート


package com.daodao.servlet;

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.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class TestServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] header={"  ","  ","  "};
		String[][] data={
				{"A1","A2","A3"},
				{"B1","B2","B3"},
				{"C1","C2","C3"}
		};
		HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("  ");
        HSSFRow row = sheet.createRow((int) 0);
        HSSFCellStyle style = wb.createCellStyle();
        for (int i = 0; i < header.length; i++) {
            HSSFCell cell = row.createCell((short) i);
            cell.setCellValue(header[i]);
            sheet.autoSizeColumn((short) i);
        }

        for (int i = 0; i < data.length; i++) {
            row = sheet.createRow(i + 1);
            row.createCell((short) 0).setCellValue(data[i][0]);
            row.createCell((short) 1).setCellValue(data[i][1]);
            row.createCell((short) 2).setCellValue(data[i][2]);
        }
		 response.setContentType("application/vnd.ms-excel");  
		 response.setHeader("Content-disposition", "attachment;filename=test.xls");  
		 OutputStream ouputStream = response.getOutputStream();  
		 wb.write(ouputStream);  

	}
}