poi EXCEL导出

因项目需求 要求实现poi excel导出功能,首先导入poi相关jar包

jar包下载地址:点击打开链接

以下为实现功能代码:

package com.chinact.opendata.datadown.webtier;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

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

import com.chinact.opendata.datadown.domain.OpendataDownloadInfoObj;
import com.chinact.opendata.datadown.domain.OpendataDownloadObj;
import com.chinact.opendata.datadown.service.IExcelService;
import com.chinact.saas.core.basic.webtier.CTSaaSBaseController;


/**
 * Excel下载
 * 
 * @author wd
 *
 */
public class ExcelController extends CTSaaSBaseController {

	private IExcelService excelService;

	public IExcelService getExcelService() {
		return excelService;
	}

	public void setExcelService(IExcelService excelService) {
		this.excelService = excelService;
	}

	public void excelDownLoad(HttpServletRequest request, HttpServletResponse response) {
		String mether = request.getMethod();
		// 获取查询数据,在service层实现
		String logicId = request.getParameter("logicId");
		OpendataDownloadObj bean = excelService.findByLogicId(logicId);
		List<OpendataDownloadInfoObj> list = excelService.findDownloadInfoByLogicId(logicId);
		if (bean == null) {
			return;
		}
		HSSFWorkbook wb = new HSSFWorkbook();// 声明工
		Sheet sheet = wb.createSheet(bean.getFileName());// 新建表
		sheet.setDefaultColumnWidth(15);// 设置表宽
		HSSFCellStyle style = wb.createCellStyle();
		org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
		font.setFontHeightInPoints((short) 12);
		HSSFCellStyle headerStyle = wb.createCellStyle();
		org.apache.poi.hssf.usermodel.HSSFFont headerFont = wb.createFont();
		headerFont.setFontHeightInPoints((short) 14);
		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		headerStyle.setFont(headerFont);
		CellRangeAddress cra0 = new CellRangeAddress(0, 1, 0, 9);
		sheet.addMergedRegion(cra0);
		sheet.setDefaultColumnWidth((short) 15);
		Row row = sheet.createRow(0);
		Cell cell1 = row.createCell(0);

		cell1.setCellValue(bean.getFileName());
		cell1.setCellStyle(headerStyle);
		// 设置字体样式
		org.apache.poi.hssf.usermodel.HSSFFont titleFont = wb.createFont();
		titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setFont(titleFont);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        String fields = "";
		Row row1 = sheet.createRow(2);
		for (int i = 0; i < list.size(); i++) {
			Cell cell = row1.createCell(i);
			cell.setCellValue(list.get(i).getCuloumName());
			cell.setCellStyle(style);
			fields +=list.get(i).getCuloumName()+",";
		}
		// 时间转字符串的格式
		final Map<String, Object> property = new HashMap<String, Object>();
		property.put("TABLENAME", bean.getTableNames());
		String field = fields.substring(0,fields.length()-1);
		property.put("field",field);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Object> beanlist = excelService.findCuloums(property);
		//第四行开始写入
		for(int i = 0, imax = beanlist.size(); i < imax; i++){
			Map<String, Object> a = (Map<String, Object>) beanlist.get(i);
			row1 = sheet.createRow(i+3);
			 for (String key : a.keySet()) {
				    for(int b = 0;b<list.size();b++){
				    	  if(key .equals(list.get(b).getCuloumName())){
					    	  row1.createCell(b).setCellValue(a.get(key).toString());
					      }	
				    }
				  }
		}
		response.reset();
		response.setContentType("application/msexcel;charset=UTF-8");
		try {
			SimpleDateFormat newsdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String date = newsdf.format(new Date());
			response.addHeader("Content-Disposition", "attachment;filename=\""
					+ new String((bean.getFileName() + date + ".xls").getBytes("GBK"), "ISO8859_1") + "\"");
			OutputStream out = response.getOutputStream();
			wb.write(out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "导出失败!");
			e.printStackTrace();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "导出失败!");
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实例:import java.util.Date; import org.xlp.annotation.ExcelOutputed; public class Student { @ExcelOutputed private int id; @ExcelOutputed("姓名") private String name; @ExcelOutputed("生日") private Date timDate; @ExcelOutputed private boolean d; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getTimDate() { return timDate; } public void setTimDate(Date timDate) { this.timDate = timDate; } public boolean isD() { return d; } public void setD(boolean d) { this.d = d; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Student [id=").append(id).append(", name=") .append(name).append(", timDate=").append(timDate) .append(", d=").append(d).append("]"); return builder.toString(); } } public class Test(){ public static void main(String[] args) { anno(); testMap(); } } public static void anno(){ Student student = new Student(); student.setId(1); student.setName("张三;; "); student.setTimDate(new Date()); Student student1 = new Student(); student1.setId(1); student1.setName("张三1"); student1.setTimDate(new Date()); List<Student> list = new ArrayList<Student>(); list.add(student); list.add(student1);System.out.println(list); XLPExcelBean xlpExcel = new XLPExcelBean(); xlpExcel.setSheetDatasOfBeans(list); xlpExcel.writeExcel("e:/xxx.xls"); } public static void testMap(){ Map<String, Object> map = new HashMap<String, Object>(); map.put("qq", 12); map.put("s=s", "=ass"); map.put("12", true); map.put(null, null); map.put("date", new Date()); map.put("现金", 20.365); System.out.println(map.keySet().toString()); System.out.println(map.values().toArray()[0]); System.out.println(Calendar.getInstance()); XLPExcel xlpExcel = new XLPExcel(); List<Map<String, Object>> arrayList = new ArrayList<Map<String,Object>>(); arrayList.add(map); xlpExcel.setSheetDatas(arrayList); xlpExcel.writeExcel(new File("e:/xx1.xls")); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值