常见问题:TFORMer SDK
如何通过程序代码导入 CSV 文件?
如何通过程序代码导入 CSV 文件?
以下示例代码展示了如何使用在 TFF 版式文件中定义的 CSV 数据源(通过 TFORMer Designer 定义)进行 CSV 导入。为提高灵活性,该数据源已配置了一个用于指定文件路径的参数:parFilePath
TFORMer Java API
// create new job
job = new Job();
File file = new File ("c:\\Temp\\MyTemplate.tff");
job.setRepositoryName(file.getAbsolutePath());
// Select the Data Source named 'ImportFromCSV' (defined in the Form Layout)
JobDataDataSource jobData = new JobDataDataSource ("ImportFromCSV");
// Set the Data Source Parameter named 'parFilePath'
jobData.setParameterValues ("parFilePath", "C:\\Temp\\myImportFile.csv");
// Set the output format:
// e.g. EPrinterType.PDFFile, EPrinterType.ZPLFile, EPrinterType.ImagePng
job.setPrinterType(EPrinterType.PDFFile);
// Output file name
job.setOutputName(System.getProperty("user.dir") + File.separator + "MyLabel.PDF");
System.out.println("Printing to " + job.getOutputName());
// Generate Output
job.setJobData(jobData);
job.print();
使用版式中定义的数据源的优势在于:该数据源定义中可以包含导入字段与版式字段之间的映射关系,此外您还可以(按需)添加计算字段。可选的数据源参数为编程或命令行交互提供了灵活性。