Excel导出

来源:互联网 发布:淘宝怎么找优惠券 编辑:程序博客网 时间:2024/06/07 13:17
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.hssf.util.HSSFColor;


/**
 * 导出execl的基本类
 * @author liuwenqiang
 * @version 1.0
 */
public class ToolExcel {
    
    
    /**
     * 按标题头headerMap,导出execl
     * @param title execl的名字
     * @param list 要导出的数据
     * @param out 输出流
     * @throws Exception
     */
    public static void exportExcel(String title, List<LinkedHashMap<String, Object>> list, OutputStream out) throws Exception {
        try {
            // 声明一个工作薄
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 生成一个表格
            HSSFSheet sheet = workbook.createSheet(title);
            // 设置表格默认列宽度为15个字节
            sheet.setDefaultColumnWidth((short)15);
            // 产生表格标题行
            HSSFRow row = sheet.createRow(0);
            HSSFCellStyle style = workbook.createCellStyle();
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setFillForegroundColor(HSSFColor.BLACK.index);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // style.setFillBackgroundColor(HSSFColor.GREY_40_PERCENT.index);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            // 产生内容
            int k = 0;
            row = sheet.createRow(0);
            if (null == list || list.size() == 0) {
                out.flush();
                out.close();
            }
            List<String> headList = new ArrayList<String>();
            for (String key : list.get(0).keySet()) {
                headList.add(key);
                HSSFCell cell = row.createCell(k++);
                cell.setCellValue(key);
                cell.setCellStyle(style);
                cell.setCellStyle(style);
            }
            style = workbook.createCellStyle();
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setFillForegroundColor(HSSFColor.BLACK.index);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                int j = 0;
                for (String headerName : headList) {
                    HSSFCell cell = row.createCell(j++);
                    Object value = list.get(i).get(headerName);
                    if (null == value) {
                        cell.setCellValue("");
                    } else {
                        cell.setCellValue(value.toString());
                    }
                    
                    cell.setCellStyle(style);
                    cell.setCellStyle(style);
                }
            }
            workbook.write(out);
            out.flush();
            out.close();
        } catch (Throwable localThrowable) {
            throw new Exception("导出Excel失败", localThrowable);
        }
    }
    

}


=======================================================================================================

/**
* 列表,导出 type: 'post'
*/
@RequestMapping(value = "/export", method = RequestMethod.GET)
@ResponseBody
public void exportTest(HttpServletRequest req,
HttpServletResponse resp) {
List<GrainBranchSettle> list = new ArrayList<GrainBranchSettle>();



try {
GrainBranchSettleExample example = new GrainBranchSettleExample();

list = grainBranchSettleMapper.selectByExample(example);


List<LinkedHashMap<String, Object>> lists = new ArrayList<LinkedHashMap<String, Object>>();
LinkedHashMap<String, Object> maps = null;
if (list == null || list.size() == 0) {
maps = new LinkedHashMap<String, Object>();
maps.put("序号", "");
maps.put("日期", "");

lists.add(maps);
} else {
for (int i = 0; i < list.size(); i++) {
GrainBranchSettle emp = list.get(i);
maps = new LinkedHashMap<String, Object>();
maps.put("序号", i);
maps.put("日期", DateUtil.format(emp.getSettleDate(), DateStyle.YYYY_MM_DD));

lists.add(maps);
}
}


resp.setContentType("application/x-msdownload;charset=gb2312");
Date date = new Date();
String time = date.getTime() + "";
String fin = "兑换支付列表列表信息" + time + ".xls";
resp.setHeader("Content-Disposition", "attachment; filename="
+ new String(fin.getBytes("gb2312"), "iso-8859-1"));
OutputStream outputStream = resp.getOutputStream();
ToolExcel.exportExcel("兑换支付列表", lists, outputStream); 
} catch (Exception e) {
log.warn("GrainInventorySiteCtrl website list export error..", e);
}
}