一,当for 循环 list push 对象时,每push 一次后都要清空对象。
二,java.lang.NoSuchMethodException:com.sss.getserialVersionUID
不能添加这个东西。
三,spring mvc 传输json 对象时,可以转成 json 字符串。
public class ExportExcelHSSFTest<T> { public void exportExcel(String[] headers,Collection<T> exportlist, String fileName,HttpServletResponse response) { // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(fileName); //样式对象 HSSFCellStyle style=workbook.createCellStyle(); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(15); // 产生表格标题行 HSSFRow row = sheet.createRow(0); //设置行高 row.setHeightInPoints(30);//设置行高 for (int i = 0; i < headers.length; i++) { HSSFCell cell=row.createCell(i); //设置背景 style.setFillBackgroundColor((short)13); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //设置字体 HSSFFont font2 = workbook.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font2.setFontHeightInPoints((short) 12); //字体大小 font2.setColor(HSSFColor.RED.index);//设置字体颜色 style.setFont(font2);//选择需要用到的字体格式 HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellStyle(style); cell.setCellValue(text); } try { // 遍历集合数据,产生数据行 Iterator<T> it = (Iterator<T>) exportlist.iterator(); int index = 0; while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = t.getClass().getDeclaredFields(); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); Field field = fields[i]; String fieldName = field.getName(); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); // 判断值的类型后进行强制类型转换 String textValue = null; // 其它数据类型都当作字符串简单处理 if(value != null && value != ""){ textValue = value.toString(); } if (textValue != null) { HSSFRichTextString richString = new HSSFRichTextString(textValue); cell.setCellValue(richString); } } } getExportedFile(workbook, fileName,response); } catch (Exception e) { e.printStackTrace(); } } /** * * 方法说明: 指定路径下生成EXCEL文件 * @return */ public void getExportedFile(HSSFWorkbook workbook, String name,HttpServletResponse response) throws Exception { System.out.println("name:"+name); BufferedOutputStream fos = null; try { String fileName = name + ".xls"; response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" )); fos = new BufferedOutputStream(response.getOutputStream()); workbook.write(fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { System.out.println("ok"); fos.close(); } } } } @RequestMapping("/trendAnalysis") @ResponseBody public void trendAnalysis(HttpServletRequest request,HttpServletResponse response,String data){ System.out.println("data:"+data); /*JSONArray jsonArray = JSONArray.fromObject(data); ExportDateTest exportDateTest=(ExportDateTest) JSONArray.toList(jsonArray, ExportDateTest.class);*/ // List<ExportDateTest> list = JSONArray.fromObject(data); List<ExportDateTest> exportlist = JsonUtils.jsonToList(data, ExportDateTest.class); System.out.println("exportDateTest:"+exportlist); ExportExcelHSSFTest<ExportDateTest> ee= new ExportExcelHSSFTest<ExportDateTest>(); String[] headers = { "id","devid","time","point1x", "point1y","point1z", "point2x","point2y", "point2z", "point3x","point3y", "point3z", "point4x","point4y", "point4z", "point5x","point5y", "point5z", "point6x","point6y", "point6z", "point7x","point7y", "point7z", "point8x","point8y", "point8z", "point9x","point9y", "point9z", "point10x","point10y", "point10z", "point11x","point11y", "point11z", "point12x","point12y", "point12z", "point13x","point13y", "point13z", "point14x","point14y", "point14z", "point15x","point15y", "point15z", "point16x","point16y", "point16z", "point17x","point17y", "point17z", "point18x","point18y", "point18z", "point19x","point19y", "point19z", "point20x","point20y", "point20z","ps","ip","pointx","pointy","sup"}; String fileName = "趋势分析表"; ee.exportExcel(headers,exportlist,fileName,response); } @JsonIgnoreProperties(ignoreUnknown = true) public class ExportDateTest implements Serializable { //private static final long serialVersionUID = 1L; private String id; //设备ID private String devid; private Date time; private BigDecimal point1x; private BigDecimal point1y; private BigDecimal point1z; private BigDecimal point2x; private BigDecimal point2y; private BigDecimal point2z; private BigDecimal point3x; private BigDecimal point3y; private BigDecimal point3z; private BigDecimal point4x; private BigDecimal point4y; private BigDecimal point4z; private BigDecimal point5x; private BigDecimal point5y; private BigDecimal point5z; private BigDecimal point6x; private BigDecimal point6y; private BigDecimal point6z; private BigDecimal point7x; private BigDecimal point7y; private BigDecimal point7z; private BigDecimal point8x; private BigDecimal point8y; private BigDecimal point8z; private BigDecimal point9x; private BigDecimal point9y; private BigDecimal point9z; private BigDecimal point10x; private BigDecimal point10y; private BigDecimal point10z; private BigDecimal point11x; private BigDecimal point11y; private BigDecimal point11z; private BigDecimal point12x; private BigDecimal point12y; private BigDecimal point12z; private BigDecimal point13x; private BigDecimal point13y; private BigDecimal point13z; private BigDecimal point14x; private BigDecimal point14y; private BigDecimal point14z; private BigDecimal point15x; private BigDecimal point15y; private BigDecimal point15z; private BigDecimal point16x; private BigDecimal point16y; private BigDecimal point16z; private BigDecimal point17x; private BigDecimal point17y; private BigDecimal point17z; private BigDecimal point18x; private BigDecimal point18y; private BigDecimal point18z; private BigDecimal point19x; private BigDecimal point19y; private BigDecimal point19z; private BigDecimal point20x; private BigDecimal point20y; private BigDecimal point20z; private String ps; private String ip; private BigDecimal pointx; private BigDecimal pointy; private Integer sup; public ExportDateTest() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDevid() { return devid; } public void setDevid(String devid) { this.devid = devid; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } public BigDecimal getPoint1x() { return point1x; } public void setPoint1x(BigDecimal point1x) { this.point1x = point1x; } public BigDecimal getPoint1y() { return point1y; } public void setPoint1y(BigDecimal point1y) { this.point1y = point1y; } public BigDecimal getPoint1z() { return point1z; } public void setPoint1z(BigDecimal point1z) { this.point1z = point1z; } public BigDecimal getPoint2x() { return point2x; } public void setPoint2x(BigDecimal point2x) { this.point2x = point2x; } public BigDecimal getPoint2y() { return point2y; } public void setPoint2y(BigDecimal point2y) { this.point2y = point2y; } public BigDecimal getPoint2z() { return point2z; } public void setPoint2z(BigDecimal point2z) { this.point2z = point2z; } public BigDecimal getPoint3x() { return point3x; } public void setPoint3x(BigDecimal point3x) { this.point3x = point3x; } public BigDecimal getPoint3y() { return point3y; } public void setPoint3y(BigDecimal point3y) { this.point3y = point3y; } public BigDecimal getPoint3z() { return point3z; } public void setPoint3z(BigDecimal point3z) { this.point3z = point3z; } public BigDecimal getPoint4x() { return point4x; } public void setPoint4x(BigDecimal point4x) { this.point4x = point4x; } public BigDecimal getPoint4y() { return point4y; } public void setPoint4y(BigDecimal point4y) { this.point4y = point4y; } public BigDecimal getPoint4z() { return point4z; } public void setPoint4z(BigDecimal point4z) { this.point4z = point4z; } public BigDecimal getPoint5x() { return point5x; } public void setPoint5x(BigDecimal point5x) { this.point5x = point5x; } public BigDecimal getPoint5y() { return point5y; } public void setPoint5y(BigDecimal point5y) { this.point5y = point5y; } public BigDecimal getPoint5z() { return point5z; } public void setPoint5z(BigDecimal point5z) { this.point5z = point5z; } public BigDecimal getPoint6x() { return point6x; } public void setPoint6x(BigDecimal point6x) { this.point6x = point6x; } public BigDecimal getPoint6y() { return point6y; } public void setPoint6y(BigDecimal point6y) { this.point6y = point6y; } public BigDecimal getPoint6z() { return point6z; } public void setPoint6z(BigDecimal point6z) { this.point6z = point6z; } public BigDecimal getPoint7x() { return point7x; } public void setPoint7x(BigDecimal point7x) { this.point7x = point7x; } public BigDecimal getPoint7y() { return point7y; } public void setPoint7y(BigDecimal point7y) { this.point7y = point7y; } public BigDecimal getPoint7z() { return point7z; } public void setPoint7z(BigDecimal point7z) { this.point7z = point7z; } public BigDecimal getPoint8x() { return point8x; } public void setPoint8x(BigDecimal point8x) { this.point8x = point8x; } public BigDecimal getPoint8y() { return point8y; } public void setPoint8y(BigDecimal point8y) { this.point8y = point8y; } public BigDecimal getPoint8z() { return point8z; } public void setPoint8z(BigDecimal point8z) { this.point8z = point8z; } public BigDecimal getPoint9x() { return point9x; } public void setPoint9x(BigDecimal point9x) { this.point9x = point9x; } public BigDecimal getPoint9y() { return point9y; } public void setPoint9y(BigDecimal point9y) { this.point9y = point9y; } public BigDecimal getPoint9z() { return point9z; } public void setPoint9z(BigDecimal point9z) { this.point9z = point9z; } public BigDecimal getPoint10x() { return point10x; } public void setPoint10x(BigDecimal point10x) { this.point10x = point10x; } public BigDecimal getPoint10y() { return point10y; } public void setPoint10y(BigDecimal point10y) { this.point10y = point10y; } public BigDecimal getPoint10z() { return point10z; } public void setPoint10z(BigDecimal point10z) { this.point10z = point10z; } public BigDecimal getPoint11x() { return point11x; } public void setPoint11x(BigDecimal point11x) { this.point11x = point11x; } public BigDecimal getPoint11y() { return point11y; } public void setPoint11y(BigDecimal point11y) { this.point11y = point11y; } public BigDecimal getPoint11z() { return point11z; } public void setPoint11z(BigDecimal point11z) { this.point11z = point11z; } public BigDecimal getPoint12x() { return point12x; } public void setPoint12x(BigDecimal point12x) { this.point12x = point12x; } public BigDecimal getPoint12y() { return point12y; } public void setPoint12y(BigDecimal point12y) { this.point12y = point12y; } public BigDecimal getPoint12z() { return point12z; } public void setPoint12z(BigDecimal point12z) { this.point12z = point12z; } public BigDecimal getPoint13x() { return point13x; } public void setPoint13x(BigDecimal point13x) { this.point13x = point13x; } public BigDecimal getPoint13y() { return point13y; } public void setPoint13y(BigDecimal point13y) { this.point13y = point13y; } public BigDecimal getPoint13z() { return point13z; } public void setPoint13z(BigDecimal point13z) { this.point13z = point13z; } public BigDecimal getPoint14x() { return point14x; } public void setPoint14x(BigDecimal point14x) { this.point14x = point14x; } public BigDecimal getPoint14y() { return point14y; } public void setPoint14y(BigDecimal point14y) { this.point14y = point14y; } public BigDecimal getPoint14z() { return point14z; } public void setPoint14z(BigDecimal point14z) { this.point14z = point14z; } public BigDecimal getPoint15x() { return point15x; } public void setPoint15x(BigDecimal point15x) { this.point15x = point15x; } public BigDecimal getPoint15y() { return point15y; } public void setPoint15y(BigDecimal point15y) { this.point15y = point15y; } public BigDecimal getPoint15z() { return point15z; } public void setPoint15z(BigDecimal point15z) { this.point15z = point15z; } public BigDecimal getPoint16x() { return point16x; } public void setPoint16x(BigDecimal point16x) { this.point16x = point16x; } public BigDecimal getPoint16y() { return point16y; } public void setPoint16y(BigDecimal point16y) { this.point16y = point16y; } public BigDecimal getPoint16z() { return point16z; } public void setPoint16z(BigDecimal point16z) { this.point16z = point16z; } public BigDecimal getPoint17x() { return point17x; } public void setPoint17x(BigDecimal point17x) { this.point17x = point17x; } public BigDecimal getPoint17y() { return point17y; } public void setPoint17y(BigDecimal point17y) { this.point17y = point17y; } public BigDecimal getPoint17z() { return point17z; } public void setPoint17z(BigDecimal point17z) { this.point17z = point17z; } public BigDecimal getPoint18x() { return point18x; } public void setPoint18x(BigDecimal point18x) { this.point18x = point18x; } public BigDecimal getPoint18y() { return point18y; } public void setPoint18y(BigDecimal point18y) { this.point18y = point18y; } public BigDecimal getPoint18z() { return point18z; } public void setPoint18z(BigDecimal point18z) { this.point18z = point18z; } public BigDecimal getPoint19x() { return point19x; } public void setPoint19x(BigDecimal point19x) { this.point19x = point19x; } public BigDecimal getPoint19y() { return point19y; } public void setPoint19y(BigDecimal point19y) { this.point19y = point19y; } public BigDecimal getPoint19z() { return point19z; } public void setPoint19z(BigDecimal point19z) { this.point19z = point19z; } public BigDecimal getPoint20x() { return point20x; } public void setPoint20x(BigDecimal point20x) { this.point20x = point20x; } public BigDecimal getPoint20y() { return point20y; } public void setPoint20y(BigDecimal point20y) { this.point20y = point20y; } public BigDecimal getPoint20z() { return point20z; } public void setPoint20z(BigDecimal point20z) { this.point20z = point20z; } public String getPs() { return ps; } public void setPs(String ps) { this.ps = ps; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public BigDecimal getPointx() { return pointx; } public void setPointx(BigDecimal pointx) { this.pointx = pointx; } public BigDecimal getPointy() { return pointy; } public void setPointy(BigDecimal pointy) { this.pointy = pointy; } public Integer getSup() { return sup; } public void setSup(Integer sup) { this.sup = sup; } public ExportDateTest(String id, String devid, Date time, BigDecimal point1x, BigDecimal point1y, BigDecimal point1z, BigDecimal point2x, BigDecimal point2y, BigDecimal point2z, BigDecimal point3x, BigDecimal point3y, BigDecimal point3z, BigDecimal point4x, BigDecimal point4y, BigDecimal point4z, BigDecimal point5x, BigDecimal point5y, BigDecimal point5z, BigDecimal point6x, BigDecimal point6y, BigDecimal point6z, BigDecimal point7x, BigDecimal point7y, BigDecimal point7z, BigDecimal point8x, BigDecimal point8y, BigDecimal point8z, BigDecimal point9x, BigDecimal point9y, BigDecimal point9z, BigDecimal point10x, BigDecimal point10y, BigDecimal point10z, BigDecimal point11x, BigDecimal point11y, BigDecimal point11z, BigDecimal point12x, BigDecimal point12y, BigDecimal point12z, BigDecimal point13x, BigDecimal point13y, BigDecimal point13z, BigDecimal point14x, BigDecimal point14y, BigDecimal point14z, BigDecimal point15x, BigDecimal point15y, BigDecimal point15z, BigDecimal point16x, BigDecimal point16y, BigDecimal point16z, BigDecimal point17x, BigDecimal point17y, BigDecimal point17z, BigDecimal point18x, BigDecimal point18y, BigDecimal point18z, BigDecimal point19x, BigDecimal point19y, BigDecimal point19z, BigDecimal point20x, BigDecimal point20y, BigDecimal point20z, String ps, String ip, BigDecimal pointx, BigDecimal pointy, Integer sup) { super(); this.id = id; this.devid = devid; this.time = time; this.point1x = point1x; this.point1y = point1y; this.point1z = point1z; this.point2x = point2x; this.point2y = point2y; this.point2z = point2z; this.point3x = point3x; this.point3y = point3y; this.point3z = point3z; this.point4x = point4x; this.point4y = point4y; this.point4z = point4z; this.point5x = point5x; this.point5y = point5y; this.point5z = point5z; this.point6x = point6x; this.point6y = point6y; this.point6z = point6z; this.point7x = point7x; this.point7y = point7y; this.point7z = point7z; this.point8x = point8x; this.point8y = point8y; this.point8z = point8z; this.point9x = point9x; this.point9y = point9y; this.point9z = point9z; this.point10x = point10x; this.point10y = point10y; this.point10z = point10z; this.point11x = point11x; this.point11y = point11y; this.point11z = point11z; this.point12x = point12x; this.point12y = point12y; this.point12z = point12z; this.point13x = point13x; this.point13y = point13y; this.point13z = point13z; this.point14x = point14x; this.point14y = point14y; this.point14z = point14z; this.point15x = point15x; this.point15y = point15y; this.point15z = point15z; this.point16x = point16x; this.point16y = point16y; this.point16z = point16z; this.point17x = point17x; this.point17y = point17y; this.point17z = point17z; this.point18x = point18x; this.point18y = point18y; this.point18z = point18z; this.point19x = point19x; this.point19y = point19y; this.point19z = point19z; this.point20x = point20x; this.point20y = point20y; this.point20z = point20z; this.ps = ps; this.ip = ip; this.pointx = pointx; this.pointy = pointy; this.sup = sup; } @Override public String toString() { return "ExportDateTest [id=" + id + ", devid=" + devid + ", time=" + time + ", point1x=" + point1x + ", point1y=" + point1y + ", point1z=" + point1z + ", point2x=" + point2x + ", point2y=" + point2y + ", point2z=" + point2z + ", point3x=" + point3x + ", point3y=" + point3y + ", point3z=" + point3z + ", point4x=" + point4x + ", point4y=" + point4y + ", point4z=" + point4z + ", point5x=" + point5x + ", point5y=" + point5y + ", point5z=" + point5z + ", point6x=" + point6x + ", point6y=" + point6y + ", point6z=" + point6z + ", point7x=" + point7x + ", point7y=" + point7y + ", point7z=" + point7z + ", point8x=" + point8x + ", point8y=" + point8y + ", point8z=" + point8z + ", point9x=" + point9x + ", point9y=" + point9y + ", point9z=" + point9z + ", point10x=" + point10x + ", point10y=" + point10y + ", point10z=" + point10z + ", point11x=" + point11x + ", point11y=" + point11y + ", point11z=" + point11z + ", point12x=" + point12x + ", point12y=" + point12y + ", point12z=" + point12z + ", point13x=" + point13x + ", point13y=" + point13y + ", point13z=" + point13z + ", point14x=" + point14x + ", point14y=" + point14y + ", point14z=" + point14z + ", point15x=" + point15x + ", point15y=" + point15y + ", point15z=" + point15z + ", point16x=" + point16x + ", point16y=" + point16y + ", point16z=" + point16z + ", point17x=" + point17x + ", point17y=" + point17y + ", point17z=" + point17z + ", point18x=" + point18x + ", point18y=" + point18y + ", point18z=" + point18z + ", point19x=" + point19x + ", point19y=" + point19y + ", point19z=" + point19z + ", point20x=" + point20x + ", point20y=" + point20y + ", point20z=" + point20z + ", ps=" + ps + ", ip=" + ip + ", pointx=" + pointx + ", pointy=" + pointy + ", sup=" + sup + "]"; }