Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.conecta.sat.utils; import com.conecta.sat.utils.dto.PdfDTO; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.springframework.web.servlet.view.document.AbstractExcelView; /** * * @author Daniel */ public class BuildXls extends AbstractExcelView { String logoPath; String[] sColumnas = {}; int fontSize = 8; int widthLogo; int heightLogo; public String[] getsColumnas() { return sColumnas; } public void setsColumnas(String[] sColumnas) { this.sColumnas = sColumnas; } public String getLogoPath() { return logoPath; } public void setLogoPath(String logoPath) { this.logoPath = logoPath; } public int getFontSize() { return fontSize; } public void setFontSize(int fontSize) { this.fontSize = fontSize; } public int getWidthLogo() { return widthLogo; } public void setWidthLogo(int widthLogo) { this.widthLogo = widthLogo; } public int getHeightLogo() { return heightLogo; } public void setHeightLogo(int heightLogo) { this.heightLogo = heightLogo; } @Override protected void buildExcelDocument(Map<String, Object> map, org.apache.poi.hssf.usermodel.HSSFWorkbook workbook, HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception { hsr1.setContentType("application/vnd.ms-excel"); DateFormat name = new SimpleDateFormat("ddMMyyyyhhmmss"); hsr1.setHeader("Content-disposition", "attachment; filename=Reporte" + name.format(new Date()) + ".xls"); HSSFSheet sheet = workbook.createSheet("Inventario"); sheet.setDefaultColumnWidth(30); CellStyle style = workbook.createCellStyle(); List<PdfDTO> list = (List<PdfDTO>) map.get("list"); HSSFRow header = sheet.createRow(0); for (int i = 0; i < sColumnas.length; i++) { header.createCell(i).setCellValue(sColumnas[i]); header.getCell(i).setCellStyle(style); } int rowCount = 1; HSSFCellStyle my_style_0 = workbook.createCellStyle(); for (PdfDTO pdfDto : list) { HSSFRow aRow = sheet.createRow(rowCount++); aRow.createCell(0).setCellValue(pdfDto.getSerial()); aRow.createCell(1).setCellValue(pdfDto.getEntrega()); aRow.createCell(2).setCellValue(pdfDto.getEntrega()); aRow.createCell(3).setCellValue(pdfDto.getActivacion()); aRow.createCell(4).setCellValue(pdfDto.getCentro()); aRow.createCell(5).setCellValue(pdfDto.getTipo()); aRow.createCell(6).setCellValue(pdfDto.getCliente()); aRow.createCell(7).setCellValue(pdfDto.getFolioPivotal()); // aRow.createCell(8).setCellValue( pdfDto.getIdUnico() ); aRow.createCell(8).setCellValue(pdfDto.getLastUpdate()); } } }