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.kcs.core.actions; import com.kcs.core.service.ManageService; import com.kcs.core.utilities.DateUtil; import com.kcs.core.utilities.PaggingList; import com.kcs.core.utilities.StringUtil; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.util.Calendar; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; /** * * @author vorawats */ public class ExportDataAction extends BaseAction { Logger logger = Logger.getLogger(ExportDataAction.class); private PaggingList pagging; private File file; private String sql; private String tmpFile; private List list; private String fileName; private InputStream excelStream; @Autowired private ManageService manageService; @Override public String success() throws Exception { return super.success(); //To change body of generated methods, choose Tools | Templates. } public String browserFile() throws Exception { sql = ""; BufferedReader in = new BufferedReader(new FileReader(new File(tmpFile))); while (in.ready()) { sql += in.readLine(); } in.close(); return SUCCESS; } public String execSql() throws Exception { logger.debug("sql >>> " + sql); list = manageService.findListDinamicSql(sql); return "output"; } public String exportExcel() throws Exception { logger.debug("exportExcel : begin..."); list = manageService.findListDinamicSql(sql); logger.debug("exportExcel : list >>> " + list); Calendar calendar = Calendar.getInstance(); HSSFWorkbook myWorkBook = new HSSFWorkbook(); HSSFSheet mySheet = myWorkBook.createSheet(DateUtil.getCurrentDateString(DateUtil.DATE_FORMAT_YYYYMMDD)); setFileName(DateUtil.getCurrentDateString(DateUtil.DATE_FORMAT_YYYYMMDD).concat(".xls")); Row headerRow = mySheet.createRow(0); if (null != list && list.size() > 0) { /* create header excel */ int i = 0; Map mapHedder = (Map) list.get(0); Set h = mapHedder.keySet(); Iterator it = h.iterator(); while (it.hasNext()) { String its = (String) it.next(); Cell titleCell = headerRow.createCell(i++); titleCell.setCellValue(its); } /* create content excel */ int rowNum = 1; HSSFRow myRow = null; for (Iterator iter = list.iterator(); iter.hasNext();) { Map map = (Map) iter.next(); Set s = map.keySet(); Iterator tmp = s.iterator(); myRow = mySheet.createRow(rowNum++); logger.debug("***************************************************"); int j = 0; while (tmp.hasNext()) { String its = (String) tmp.next(); String str = String.valueOf(map.get(its)); // logger.debug("str["+j+"] >>> "+str); Cell cell = myRow.createCell(j++); cell.setCellValue(str); } } } ByteArrayOutputStream boas = new ByteArrayOutputStream(); myWorkBook.write(boas); setExcelStream(new ByteArrayInputStream(boas.toByteArray())); logger.debug("exportExcel : end..."); return "excel"; } public PaggingList getPagging() { return pagging; } public void setPagging(PaggingList pagging) { this.pagging = pagging; } public String getSql() { return sql; } public void setSql(String sql) { this.sql = sql; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getTmpFile() { return tmpFile; } public void setTmpFile(String tmpFile) { this.tmpFile = tmpFile; } public List getList() { return list; } public void setList(List list) { this.list = list; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getExcelStream() { return excelStream; } public void setExcelStream(InputStream excelStream) { this.excelStream = excelStream; } }