//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); //Load template file Template_FamilyInfo.xlsx from resource var templateFile = this.GetResourceStream("xlsx\\Template_FamilyInfo.xlsx"); workbook.Open(templateFile); #region Define custom classes //public class StudentInfos //{ // public List<StudentInfo> student; //} //public class StudentInfo //{ // public string name; // public string address; // public List<Family> family; //} //public class Family //{ // public Guardian father; // public Guardian mother; //} //public class Guardian //{ // public string name; // public string occupation; //} #endregion //Get data from json file string jsonText = string.Empty; using (Stream stream = this.GetResourceStream("Template_FamilyInfo.json")) using (StreamReader reader = new StreamReader(stream)) { jsonText = reader.ReadToEnd(); } var datasource = Newtonsoft.Json.JsonConvert.DeserializeObject<StudentInfos>(jsonText); //Add data source workbook.AddDataSource("ds", datasource); //Invoke to process the template workbook.ProcessTemplate(); //save to an excel file workbook.Save("jsonfile.xlsx");
' Create a new Workbook Dim workbook As New Workbook 'Load template file Template_FamilyInfo.xlsx from resource Dim templateFile = GetResourceStream("xlsx\Template_FamilyInfo.xlsx") workbook.Open(templateFile) #Region "Define custom classes" 'Public Class StudentInfos ' Public student As List(Of StudentInfo) 'End Class 'Public Class StudentInfo ' Public name As String ' Public address As String ' Public family As List(Of Family) 'End Class 'Public Class Family ' Public father As Guardian ' Public mother As Guardian 'End Class 'Public Class Guardian ' Public name As String ' Public occupation As String 'End Class #End Region 'Get data from json file Dim jsonText As String Using stream = GetResourceStream("Template_FamilyInfo.json"), reader As New StreamReader(stream) jsonText = reader.ReadToEnd() End Using Dim datasource = JsonConvert.DeserializeObject(Of StudentInfos)(jsonText) 'Add data source workbook.AddDataSource("ds", datasource) 'Invoke to process the template workbook.ProcessTemplate() ' save to an excel file workbook.Save("jsonfile.xlsx")