天天看点

jasperreport 导出的excel全部是文本,数字无法求和

导出excel无法进行运算,如下图

jasperreport 导出的excel全部是文本,数字无法求和

解决办法

首先,javabean的字段类型设置为bigdecimal(数字类)

jasperreport 导出的excel全部是文本,数字无法求和

第二步,重新读取属性,设置报表field类型为java.math.BigDecimal(如果是jdbc连接的,ireport会自动识别类别)

右击报表---->edit query---->java beandatasource---->read attribute--->add selected fields

jasperreport 导出的excel全部是文本,数字无法求和

确认报表中类型是bigdecimal

jasperreport 导出的excel全部是文本,数字无法求和

第三步,识别单元格类型

ireport开发工具,

工具----》选项---》iReport---》export options---》excel----》勾选detect cell type

jasperreport 导出的excel全部是文本,数字无法求和

显示正常

jasperreport 导出的excel全部是文本,数字无法求和

工程中解决办法

程序中设置识别单元格

//设置导出时参数
		SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
		conf.setWhitePageBackground(false);
		conf.setDetectCellType(true);
		
		JRXlsxExporter exporter = new JRXlsxExporter();
		exporter.setConfiguration(conf);
           

完整代码

public void testExportExcel() throws JRException{
		System.out.println(System.getProperty("user.dir"));
		String sourceFileName = "D:/eclipse_workspace/test_report.jrxml";
		long starttime = System.currentTimeMillis();
		
		//jasper文件
		JasperReport jasperReport = JasperCompileManager.compileReport(sourceFileName);
		long endtime1 = System.currentTimeMillis();
		System.out.println("第一次编译共用时:"+((endtime1-starttime)/1000));
		
		//填充报表的参数
		Map<String,Object> params  = new HashMap<String,Object>();
		
		List<ReportVO<MemberConsumeInfo>>  beanCollection = CustomerReportFactoryBean.getMemberConsumeList();
		JRDataSource dataSource = new JRBeanCollectionDataSource(beanCollection, true);
		//print文件
		JasperPrint print =  JasperFillManager.fillReport(jasperReport, params, dataSource);
		System.out.println("打印文件是:"+print);
		
		//如果只注明文件名字,默认会生成在user.dir
		String fileName = "asfdsf1.xlsx";
		
		//设置导出时参数
		SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();
		conf.setWhitePageBackground(false);
		conf.setDetectCellType(true);
		
		JRXlsxExporter exporter = new JRXlsxExporter();
		exporter.setConfiguration(conf);
		
		
		//设置输入项
		ExporterInput exporterInput = new SimpleExporterInput(print);
		exporter.setExporterInput(exporterInput);
		
		//设置输出项
		OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(fileName);
		exporter.setExporterOutput(exporterOutput);
		
		
		exporter.exportReport();
	}