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

本文介绍了解决Java导出的Excel文件无法进行数字运算的问题。主要步骤包括:使用BigDecimal类型处理数字,确保报表字段类型正确,并启用Excel导出时的单元格类型检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



解决办法

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


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


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



确认报表中类型是bigdecimal



第三步,识别单元格类型
ireport开发工具,

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



显示正常




工程中解决办法


程序中设置识别单元格

	//设置导出时参数
		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();
	}


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值