Aspose.cell和Aspose.pdf根据json输入的模板输出数据

时间:2018-03-19 18:12:23

标签: templates xlsx aspose

我们希望数据占位符在excel中。我们以json格式输入数据。 我们如何使用aspose.cell在运行时在提供的xlsx中将数据映射到占位符并生成excel输出?

有没有办法为pdf定义模板,可以从json加载数据?

使用aspose的转换例程在几行代码中将json转换为xls格式和pdf格式。即

  1. 创建工作簿
  2. 将数据映射到工作簿
  3. 是否有一个样本我可以在xlsx和pdf中引用aspose占位符?

2 个答案:

答案 0 :(得分:0)

我们担心,您无法直接将数据从JSON放到Aspose.Cells Workbook中。您必须先从JSON创建数据表或数组列表,然后才能根据需要使用智能标记功能。您将在Excel文件中指定一些标签,它将被称为模板Excel文件或Designer Excel文件。然后,您将从各种数据源将数据导入其中,例如列表或数据表等。

除此之外,您还可以使用Aspose.Cells API将数据导入工作表或从工作表中导出数据。

最后,您可以将工作簿或Excel文件保存为Pdf格式。

请参阅在线Aspose.Cells文档中的这些文档,以获取更多帮助和示例代码。

  
      
  • 智能标记>使用智能标记
  •   
  • 智能标记>如果数据太大,则自动将智能标记数据填充到其他工作表
  •   
  • 数据>将数据导入工作表
  •   
  • 数据>从工作表导出数据
  •   
  • 加载,保存,转换和管理>将Excel工作簿转换为PDF
  •   

如果您发现从Aspose文档中搜索文章有任何困难,请随时告诉我们,我们将为您提供直接链接。

更新1

请参阅以下屏幕截图,其中显示了以下 Java和C#代码中使用的以下Excel文件。

  • TemplateDesigner.xlsx
  • outputSmartMarker.xlsx

请阅读屏幕截图中的注释,说明什么是智能标记(标记)及其处理方式。

Explain Aspose.Cells Smart Marker Feature with Java

<强>爪哇

//Person class having Country, Name, Age and Address Properties
public class Person
{
    String m_country;
    String m_name;
    int m_age;
    String m_address;

    public Person(String country, String name, int age, String address)
    {
        this.m_country = country;
        this.m_name = name;
        this.m_age = age;
        this.m_address = address;
    }

    public String getCountry()
    {
        return this.m_country;
    }

    public String getName()
    {
        return this.m_name;
    }

    public int getAge()
    {
        return this.m_age;
    }

    public String getAddress()
    {
        return this.m_address;
    }
}

//---------------------------------------------

public void Run() throws Exception
{
    //List of the persons
    ArrayList<Person> lstPersons = new ArrayList<Person>();

    //China
    lstPersons.add(new Person("China", "Simon1", 33, "AB Town1"));
    lstPersons.add(new Person("China", "Simon2", 34, "AB Town2"));
    lstPersons.add(new Person("China", "Simon3", 35, "AB Town3"));
    lstPersons.add(new Person("China", "Simon4", 36, "AB Town4"));

    //England
    lstPersons.add(new Person("England", "John1", 28, "CH Town1"));
    lstPersons.add(new Person("England", "John2", 27, "CH Town2"));
    lstPersons.add(new Person("England", "John3", 26, "CH Town3"));

    //USA
    lstPersons.add(new Person("USA", "James1", 19, "JF Town1"));
    lstPersons.add(new Person("USA", "James2", 15, "JF Town2"));

    //Iran
    lstPersons.add(new Person("Iran", "Jack1", 24, "SK Town1"));
    lstPersons.add(new Person("Iran", "Jack2", 25, "SK Town2"));
    lstPersons.add(new Person("Iran", "Jack3", 26, "SK Town3"));

    //Load the template Excel file containing Smart Markers
    Workbook wb = new Workbook(dirPath + "TemplateDesigner.xlsx");

    //Create workbook designer
    WorkbookDesigner wd = new WorkbookDesigner(wb);

    //Set the data source
    wd.setDataSource("Person", lstPersons);

    //Process the smart marker
    wd.process();

    //Save the output Excel file
    wb.save(dirPath + "outputSmartMarker.xlsx");
}

<强> C#

//Person class having Country, Name, Age and Address Properties
class Person
{
    public string Country { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

    public string Address { get; set; }
}

//---------------------------------------------

static void Run()
{
    //List of the persons
    List<Person> lst = new List<Person>();

    //China
    lst.Add(new Person() { Country = "China", Name = "Simon1", Age = 33, Address = "AB Town1" });
    lst.Add(new Person() { Country = "China", Name = "Simon2", Age = 34, Address = "AB Town2" });
    lst.Add(new Person() { Country = "China", Name = "Simon3", Age = 35, Address = "AB Town3" });
    lst.Add(new Person() { Country = "China", Name = "Simon4", Age = 36, Address = "AB Town4" });

    //England
    lst.Add(new Person() { Country = "England", Name = "John1", Age = 28, Address = "CH Town1" });
    lst.Add(new Person() { Country = "England", Name = "John2", Age = 27, Address = "CH Town2" });
    lst.Add(new Person() { Country = "England", Name = "John3", Age = 26, Address = "CH Town3" });

    //USA
    lst.Add(new Person() { Country = "USA", Name = "James1", Age = 19, Address = "JF Town1" });
    lst.Add(new Person() { Country = "USA", Name = "James2", Age = 18, Address = "JF Town2" });

    //Iran
    lst.Add(new Person() { Country = "Iran", Name = "Jack1", Age = 24, Address = "SK Town1" });
    lst.Add(new Person() { Country = "Iran", Name = "Jack2", Age = 25, Address = "SK Town2" });
    lst.Add(new Person() { Country = "Iran", Name = "Jack3", Age = 26, Address = "SK Town3" });

    //Load the template Excel file containing Smart Markers
    Workbook wb = new Workbook("TemplateDesigner.xlsx");

    //Create workbook designer
    WorkbookDesigner wd = new WorkbookDesigner(wb);

    //Set the data source
    wd.SetDataSource("Person", lst);

    //Process the smart marker
    wd.Process();

    //Save the output Excel file
    wb.Save("outputSmartMarker.xlsx");

}

更新2

以下智能标记说明了如何使用重复动态公式

&=&=Sum(E{r}:F{r})

整个智能标记如下

Country Age1    Age2    Repeat Dynamic Formula
&=Person.Country(group:merge,skip:2)    &=Person.Age    &=Person.Age    &=&=Sum(E{r}:F{r})

请参阅以下屏幕截图。它显示包含智能标记的模板Excel文件和输出Excel文件以及单击显示公式时的输出Excel。

Aspose.Cells Placeholders Smart Markers - Repeat Dynamic Formula

注意:我在Aspose

担任开发人员传播者

答案 1 :(得分:0)

关于Aspose.PDF API,一旦将JSON数据转换为数据表或数组列表,就可以将文本添加到PDF文档中的任何占位符。您可以使用下面的代码段在PDF页面上添加文字。

// Open document
Document pdfDocument = new Document(dataDir + "input.pdf");

// Get particular page
Page pdfPage = (Page)pdfDocument.Pages[1];

// Create text fragment
TextFragment textFragment = new TextFragment("main text");
textFragment.Position = new Position(100, 600);

// Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

// Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);

// Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);

dataDir = dataDir + "AddText_out.pdf";

// Save resulting PDF document.
pdfDocument.Save(dataDir);

有关使用文字的详细信息,请访问Add text to a PDF file

注意:我在Aspose

担任开发人员传播者