您的位置:首页 > 其它

使用poi下载Excel封装的lol类

2019-06-06 08:56 423 查看

poi下载通过反射原理遍历出数据库查询的数据

[code]public class ExcleUtil {
public static <T> void excle( String[] str,/*第一行的数据数组*/ List<T> list,/*数据实体类传值*/ HttpServletResponse response,/*传入当前的响应值*/String[] lal/*获得前台的实体类的方法在此类中通过反射便利赋值输出*/) throws Exception {
HSSFWorkbook wb.new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");//创建工作页类
HSSFRow row;//行
HSSFCell cell;//列
int numrow=0;//变量行
int numcell=0;//变量列
row = sheet.createRow(numrow);//创建行
for(int num=0;num<=row.getLastCellNum();num++){//getLastCellNum获得从开始到最后一行的数据
row.createCell(num).setCellValue(str[num].toString());
}//遍历第一行标题的顺序及内容
for (T t:list) {
numrow++;
row=sheet.createRow(numrow);
for(String s:lal){
cell= row.createCell(numcell);
numcell++;
String strt = "get"+s.substring(0,1).toUpperCase()+s.substring(1);
cell.setCellValue(t.getClass().getMethod(strt).invoke(t).toString());
}
numcell=0;
}//遍历从前台获取过来的值
String fileName = URLEncoder.encode("人员表.xlsx", "utf-8");
response.setHeader("Content-type","xlsx");
response.setHeader("Content-Disposition","attachment;fileName="+fileName);
ServletOutputStream os = response.getOutputStream();
wb.write(os);
os.close();
wb.close();
}
}

下边是我servlet中的方法

[code]            public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String tableName = request.getParameter("tableName");
SerRealize ser = new SerRealize();
List<ResignManageBean> list = ser.selectall();
if (tableName != "") {
String[] split = tableName.split(",");
String [] filedName = {"id","name","mobile","sex","identity","qq","entry","position","positiontype","pay"};
try {
ExcleUtil.excle(split,list,response,filedName);
}catch (Exception e) {
e.printStackTrace();
}
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: