其他分享
首页 > 其他分享> > 自定义excel转pdf

自定义excel转pdf

作者:互联网

读取excel参考
https://blog.csdn.net/L123713/article/details/85004085
汉字暂时无法显示2019-10-12日,已解决,链接地址
https://blog.csdn.net/aixuner1002/article/details/102518746

public class ReadExcel{
	public static void main(String[] args) throws JRException, IOException {
		ReadExcel read=new ReadExcel();
		read.topdf();
	}

	// 去读Excel的方法readExcel,该方法的入口参数为一个File对象
	private  List<ExcelParam> readExcel(File file) {
		try {
			// 创建输入流,读取Excel
			InputStream is = new FileInputStream(file.getAbsolutePath());
			// jxl提供的Workbook类
			Workbook wb = Workbook.getWorkbook(is);
			// Excel的页签数量
			int sheetSize = wb.getNumberOfSheets();
			for (int index = 0; index < sheetSize; index++) {
				List<ExcelParam> outerList = new ArrayList<ExcelParam>();
				// 每个页签创建一个Sheet对象
				Sheet sheet = wb.getSheet(index);
				// sheet.getRows()返回该页的总行数
				for (int i = 0; i < sheet.getRows(); i++) {// 获取每一行的列数
					// sheet.getColumns()返回该页的总列数
					for (int j = 0; j < sheet.getColumns(); j++) {
						ExcelParam param = new ExcelParam();
						String cellinfo = sheet.getCell(j, i).getContents();
						if (cellinfo.isEmpty()) {
							continue;
						}
						param.setStem(sheet.getCell(0, i).getContents());
						j++;
						param.setOptionA(sheet.getCell(1, i).getContents());
						j++;
						param.setOptionB(sheet.getCell(2, i).getContents());
						j++;
						param.setOptionC(sheet.getCell(3, i).getContents());
						j++;
						param.setOptionD(sheet.getCell(4, i).getContents());
						j++;
						if (sheet.getColumns() == 6) {
							param.setAnswer(sheet.getCell(5, i).getContents());
							j++;
							outerList.add(param);
							continue;
						}
						if (sheet.getColumns() == 7) {
							param.setAnswer(sheet.getCell(5, i).getContents());
							j++;
							param.setOptionE(sheet.getCell(6, i).getContents());
							j++;
							outerList.add(param);
							continue;
						}
						if (sheet.getColumns() == 8) {
							param.setAnswer(sheet.getCell(5, i).getContents());
							j++;
							param.setOptionE(sheet.getCell(6, i).getContents());
							j++;
							param.setOptionF(sheet.getCell(7, i).getContents());
							j++;
							outerList.add(param);
							continue;
						}

					}
				}
				return outerList;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 转化为pdf
	 * @throws JRException 
	 * @throws IOException 
	 */
	private  void topdf() throws JRException, IOException {

		Map<String, Object> parameters = new HashMap<String, Object>();
		ReadExcel obj = new ReadExcel();
		// 此处为我创建Excel路径:E:\\test.xls下
		File file = new File("E:\\test.xls");
		List<ExcelParam> excelList = obj.readExcel(file);
		JRDataSource DataSource = new JRBeanCollectionDataSource(excelList);
		//路径可以右键properties查看
		String templatePath = "E:\\Workspace\\Test\\src\\com\\test\\TestExcel.jrxml";
		JasperReport jasperReport = JasperCompileManager.compileReport(templatePath);
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, DataSource);
//		JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
		String desFilePath = "E:\\JasperPdfDemoTest.pdf";
		// 通过JasperExportManager管理工具进行报表输出文档,此外设定为输出PDF文件
		JasperExportManager.exportReportToPdfFile(jasperPrint, desFilePath);

	}
}

创建一个实体类ExcelParam

private String stem;
	
	private String optionA;
	private String optionB;
	private String optionC;
	private String optionD;
	private String optionE;
	private String optionF;
	private String answer;

TIBCO里面的文件的Source如下:不具有参考意义,仅为自己使用

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.2.final using JasperReports Library version 6.2.2  -->
<!-- 2019-10-12T11:24:06 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="TestExcel" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3f965e2d-7220-411b-90a9-e1fb26d2bb09">
	<property name="com.jaspersoft.studio.unit." value="pixel"/>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="stem" class="java.lang.String"/>
	<field name="optionA" class="java.lang.String"/>
	<field name="optionB" class="java.lang.String"/>
	<field name="optionC" class="java.lang.String"/>
	<field name="optionD" class="java.lang.String"/>
	<field name="optionE" class="java.lang.String"/>
	<field name="optionF" class="java.lang.String"/>
	<field name="answer" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<pageHeader>
		<band height="20">
			<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
			<staticText>
				<reportElement x="600" y="0" width="100" height="20" uuid="c4c743a4-2fae-4bc0-9e6f-e05321992f26">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
				</reportElement>
				<text><![CDATA[Answer]]></text>
			</staticText>
		</band>
	</pageHeader>
	<detail>
		<band height="140" splitType="Stretch">
			<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="0" width="500" height="20" uuid="fd051570-ef11-48e3-98f1-c77f1e8d4c84">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
					<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
				</reportElement>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["题干: " + $F{stem}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="20" width="500" height="20" uuid="d2750b9a-4e06-4819-bb88-a8a9eff89ec8">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
				</reportElement>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["A: " + $F{optionA}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="40" width="500" height="20" uuid="08847f11-fa9f-4c71-81f1-2101a15af4c4"/>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["B: " + $F{optionB}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="60" width="500" height="20" uuid="6e0664c0-4ff4-4c15-8c7f-79d18bd5c5eb"/>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["C: " + $F{optionC}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="80" width="500" height="20" uuid="fe534a7c-3ae2-47fb-9963-4513cee1d9b8"/>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["D:" + $F{optionD}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="100" width="500" height="20" uuid="ff5ec0bc-1f54-444d-9b9f-9d4a370158c3"/>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["E:" + $F{optionE}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement stretchType="RelativeToTallestObject" x="80" y="120" width="500" height="20" uuid="4e7cbff4-87c4-49af-b440-d8c1be0ea3a0"/>
				<textElement>
					<font fontName="Monospaced"/>
				</textElement>
				<textFieldExpression><![CDATA["F:" + $F{optionF}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="600" y="0" width="100" height="20" uuid="09cd02ef-2959-4bc8-bbc3-18c1d5de9c44"/>
				<textFieldExpression><![CDATA[$F{answer}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<pageFooter>
		<band height="15" splitType="Stretch">
			<textField>
				<reportElement x="680" y="0" width="60" height="15" uuid="ce57e4e3-a3b3-4c43-ae7b-e40de76a339a">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
				</reportElement>
				<textElement textAlignment="Right"/>
				<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
			</textField>
			<textField evaluationTime="Report">
				<reportElement x="740" y="0" width="60" height="15" uuid="20a812df-5cd9-461c-ae26-193dbc1c1d4b"/>
				<textElement textAlignment="Left"/>
				<textFieldExpression><![CDATA[" /" + $V{PAGE_NUMBER}]]></textFieldExpression>
			</textField>
		</band>
	</pageFooter>
</jasperReport>


标签:getContents,sheet,String,自定义,++,excel,param,getCell,pdf
来源: https://www.cnblogs.com/whtjyt/p/16317658.html