导出Excel

开发工具与关键技术:MyEclipse 10、java
作者:梁添荣
撰写时间:2019-07-28

public class ExcelUtil {
/*用法:例
//导出Excel
private void daying(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType(“octets/stream”);
String excelName = “安检不合格单”;
//转码防止乱码
response.addHeader(“Content-Disposition”, “attachment;filename=”+new String( excelName.getBytes(“gb2312”), “ISO8859-1” )+".xls");
String[] headers = new String[]{“车辆编号”,“车型”,“安检车站ID”,“安检站”,“安检员”,“电脑操作员”,“检查结果”,“有效期”};
try {
OutputStream out = response.getOutputStream();
String[] st={“carnumber”,“modelofcar”,“siteOperationCode”,“siteMC”,“screeners”,“keypuncher”,“checkResult”,“securityperiod”};
List<Pw_carsafetcheck> list=carsafetcheck.selectNocome();
ExcelUtil.exportExcel(excelName,headers, list, out, Pw_carsafetcheck.class,st);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
ps:变量headers是头部信息(字段名),st是头部信息的值,所以headers和st数组长度一样,而且元素对应(例:车辆编号 对应 carnumber)
}
 * */
/** ps:必须引入poi架包是poi-3.7-20101029.jar
 * @param name Excel表的名字
 * @param headers Excel表的第一列头部名称
 * @param list 查询的的数据
 * @param out OutputStream
 * @param cla po对象的类class
 * @param valueKey po类的变量,即要在Excel要显示的字段
 */  
public static <T> void exportExcel(String name,String[] headers,List<T> list,OutputStream out,Class<T> cla,String[] valueKey){  
    //声明一个工作簿  
    HSSFWorkbook workbook = new HSSFWorkbook();  
    //生成一个表格  
    HSSFSheet sheet = workbook.createSheet(name);  
    //设置表格默认列宽度为15个字符  
    sheet.setDefaultColumnWidth(20);  
    //生成一个样式,用来设置标题样式  
    HSSFCellStyle style = workbook.createCellStyle();  
    //设置这些样式  
    style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
    //生成一个字体  
    HSSFFont font = workbook.createFont();  
    font.setColor(HSSFColor.WHITE.index);//颜色对照表:(index是必须有的)https://blog.csdn.net/for_china2012/article/details/29844661
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
    //把字体应用到当前的样式  
    style.setFont(font);  
    // 生成并设置另一个样式,用于设置内容样式  
    HSSFCellStyle style2 = workbook.createCellStyle();  
    style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);  
    style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
    style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
    style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
    style2.setBorderRight(HSSFCellStyle.BORDER_THIN);  
    style2.setBorderTop(HSSFCellStyle.BORDER_THIN);  
    style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
    style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
    // 生成另一个字体  
    HSSFFont font2 = workbook.createFont();  
    font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
    // 把字体应用到当前的样式  
    style2.setFont(font2);  
    //产生表格标题行  
    HSSFRow row = sheet.createRow(0);  
    for(int i = 0; i<headers.length;i++){  
        HSSFCell cell = row.createCell(i);  
        cell.setCellStyle(style);  
        HSSFRichTextString text = new HSSFRichTextString(headers[i]);  
        cell.setCellValue(text);  
    }  
    //利用反射导入数据
    for (int i=0;i<list.size();i++) { 
    	T newClass;
		try {
			newClass = cla.newInstance();
		} catch (InstantiationException e2) {
			e2.printStackTrace();
		} catch (IllegalAccessException e2) {
			e2.printStackTrace();
		}
		newClass =list.get(i);  
    	
    	Class<?> getClass=newClass.getClass();
		Object object = null;
		int j = 0;  
		row = sheet.createRow(i+1);  
    	for (String value : valueKey) {
			
				PropertyDescriptor pd;
				try {
					pd = new PropertyDescriptor(value,getClass);
					Method method=pd.getReadMethod();
					object=method.invoke(newClass);
				} catch (IntrospectionException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
            row.createCell(j++).setCellValue(String.valueOf(object));  
		}
    }  
    try {  
        workbook.write(out);  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值