开发工具与关键技术: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();
}
}
}