使用XML模板填充数据

java 代码如下:

public class GeneralXML {
	public static String readFile(String filePath) {
		StringBuffer sb = new StringBuffer();
		File file = new File(filePath);
		FileReader fr = null;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedReader br = new BufferedReader(fr);

		String strLine = "";
		try {
			while ((strLine = br.readLine()) != null) {
				sb.append(strLine);
				sb.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fr.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		final String  head = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
		final String root_start = "<root>";
		final String root_end = "</root>";
		StringBuffer sb = new StringBuffer();
		sb.append(head);
		sb.append(root_start);
		MessageFormat mf = new MessageFormat(readFile("src/main/resources/template.xml"));
		sb.append(mf.format(new Object[] { "11", "12", "13", "14"}));
		sb.append(mf.format(new Object[] { "21", "22", "23", "24"}));
		sb.append(root_end);
		System.out.println(sb.toString());
	}
}

xml模板如下:

<study name="{0}">
	<one>{1}</one>
	<two>{2}</two>
	<three>{3}</three>
</study>


在实际应用中,我们可以将代码中的xml的头和尾都放在xml模板中,而代码就可以省略 StringBuffer了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用Spring Boot和EasyExcel进行模板填充数据的操作,你可以按照以下步骤进行: 1. 首先,确保你已经在你的项目中添加了EasyExcel的依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.3.0</version> </dependency> ``` 2. 创建一个模板文件,可以是Excel文件或者是Excel模板文件,用于定义数据的结构和样式。 3. 创建一个Java类,用于表示要填充模板中的数据。该类的字段应该与模板文件中的单元格一一对应。 4. 在Spring Boot应用程序中,使用EasyExcel提供的API来读取模板文件和写入数据。可以使用`ExcelWriter`类来写入数据模板中。 下面是一个示例代码,演示如何使用EasyExcel进行模板填充数据: ```java import com.alibaba.excel.EasyExcel; public class TemplateFillExample { public static void main(String[] args) { // 读取模板文件 String templateFileName = "template.xlsx"; EasyExcel.read(templateFileName) .sheet().doRead(); // 创建要填充数据 List<DataModel> data = new ArrayList<>(); data.add(new DataModel("John", 25)); data.add(new DataModel("Alice", 30)); data.add(new DataModel("Bob", 35)); // 写入数据模板中 String outputFileName = "output.xlsx"; EasyExcel.write(outputFileName) .withTemplate(templateFileName) .sheet().doWrite(data); } public static class DataModel { private String name; private int age; // 构造方法、getter和setter省略 public DataModel(String name, int age) { this.name = name; this.age = age; } } } ``` 在上面的示例代码中,我们通过`EasyExcel.read()`方法来读取模板文件,然后使用`EasyExcel.write()`方法来写入数据。通过`withTemplate()`方法指定模板文件,`sheet().doWrite()`方法将数据写入到指定的sheet中。 你可以根据实际需求进行相应的调整和扩展。希望对你有帮助!如有需要,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值