Creating Report Using Spire Doc

Kailash Chandra Behera | Tuesday, June 28, 2016

Introduction

This article demonstrate how to create report document using spire doc,This article specially demonstrate how to use table in spire.Doc build a report.In this illustration i have displayed list of students and their details like , class, section, number etc. in the tabular format.

Getting Started


Spire Doc Library provides easy way to display list of collection data in tabular format using word document. This library is available in Manage NuGet Packages. You can easily download this library from there.
Here in this article demonstrates in the following way
1. Filling data of students
2. Creates word document using spire. doc
3. Inserting a table into a document and Display student data

This below code is the structure of student class which helps to contain details of data about students.

 public class Student   
   {   
     public int ID { get; set; }   
     public string Name { get; set; }   
     public string Class { get; set; }   
     public string Section { get; set; }   
     public int Number { get; set; }   
     public double Percentage { get; set; }   
   }  
Fill the list, here GatSutednts function is filling list of student data.
 private static void GetStudents(List students)   
     {   
       Student student = new Student();   
       student.ID = 1;   
       student.Name = "Kaialsh";   
       student.Class = "X";   
       student.Section = "A";   
       student.Number = 40;   
       students.Add(student);   
       student = new Student();   
       student.ID = 2;   
       student.Name = "Vinay";   
       student.Class = "X";   
       student.Section = "A";   
       student.Number = 50;   
       students.Add(student);   
       student = new Student();   
       student.ID = 3;   
       student.Name = "Ruchit";   
       student.Class = "X";   
       student.Section = "B";   
       student.Number = 60;   
       students.Add(student);   
       student = new Student();   
       student.ID = 4;   
       student.Name = "Sushil";   
       student.Class = "X";   
       student.Section = "B";   
       student.Number = 70;   
       students.Add(student);   
       student = new Student();   
       student.ID = 5;   
       student.Name = "Rajesh";   
       student.Class = "X";   
       student.Section = "C";   
       student.Number = 30;   
       students.Add(student);   
     }   
Creating Document
 Spire.Doc.Document doc = new Spire.Doc.Document();   
 Spire.Doc.Section section = doc.AddSection();  
Above code creates an object of new word document and inserts a page into the document, here the Document class helps to create object of document and the addSection function helps to insert page into document.
 //Adding and formating Header Paragraph   
       Spire.Doc.HeaderFooter header = section.HeadersFooters.Header;   
       Spire.Doc.Documents.Paragraph headerParagraph = header.AddParagraph();   
       headerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
       headerParagraph.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.ThickThinMediumGap;   
       headerParagraph.Format.Borders.Bottom.Space = 0.05f;   
       headerParagraph.Format.Borders.Bottom.Color = System.Drawing.Color.Navy;   
       Spire.Doc.Fields.TextRange headerText = headerParagraph.AppendText("Annual Report of Students");   
       headerText.CharacterFormat.FontName = "Cambria";   
       headerText.CharacterFormat.FontSize = 15;   
       headerText.CharacterFormat.TextColor = System.Drawing.Color.Navy;   
Above code inserts header into page and formats the header text. TheTextRange class of Spire.Doc.Fields namespace contains text of header.
Inserting Table Into Document
Now we will see how table will be inserted into document, below code creates object of table that to be inserted into document. and add a new row into table with cells for displaying as column header.
 //Creating object of table  
 Spire.Doc.Table table = section.AddTable(true);  
 //Adding Header Row into table   
       string[] headerData = { "ID", "Name", "Class", "Section", "Number" };   
       table.ResetCells(students.Count + 1, headerData.Length);   
       Spire.Doc.TableRow HRow = table.Rows[0];   
       HRow.IsHeader = true;   
       HRow.Height = 23;   
       HRow.RowFormat.BackColor = System.Drawing.Color.AliceBlue;   
       for (int i = 0; i < headerData.Length; i++)   
       {   
         //Cell Alignment   
         Spire.Doc.Documents.Paragraph cellParagaraph = HRow.Cells[i].AddParagraph();   
         HRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         cellParagaraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         //Data Format   
         Spire.Doc.Fields.TextRange textRange = cellParagaraph.AppendText(headerData[i]);   
         textRange.CharacterFormat.FontName = "Calibri";   
         textRange.CharacterFormat.FontSize = 14;   
         textRange.CharacterFormat.TextColor = System.Drawing.Color.Teal;   
         textRange.CharacterFormat.Bold = true;   
       }   
Again we will insert Rows into table which display student's data (annual report). This below code insert row into table with student's data.
 students.ForEach(s =>   
       {   
         Spire.Doc.TableRow DataRow = table.Rows[ind];   
         DataRow.Height = 20;   
         //Cell Alignment   
         DataRow.Cells[0].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         //Fill Data in Rows   
         Spire.Doc.Documents.Paragraph IDCellParagraph = DataRow.Cells[0].AddParagraph();   
         Spire.Doc.Fields.TextRange IDText = IDCellParagraph.AppendText(s.ID.ToString());   
         //Format Cells   
         IDCellParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         IDText.CharacterFormat.FontName = "Calibri";   
         IDText.CharacterFormat.FontSize = 12;   
         IDText.CharacterFormat.TextColor = System.Drawing.Color.Brown;   
         //Cell Alignment   
         DataRow.Cells[1].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         //Fill Data in Rows   
         Spire.Doc.Documents.Paragraph NameCellParagraph = DataRow.Cells[1].AddParagraph();   
         Spire.Doc.Fields.TextRange NameText = NameCellParagraph.AppendText(s.Name);   
         //Format Cells   
         NameCellParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         NameText.CharacterFormat.FontName = "Calibri";   
         NameText.CharacterFormat.FontSize = 12;   
         NameText.CharacterFormat.TextColor = System.Drawing.Color.Brown;   
         //Cell Alignment   
         DataRow.Cells[2].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         //Fill Data in Rows   
         Spire.Doc.Documents.Paragraph ClassCellParagraph = DataRow.Cells[2].AddParagraph();   
         Spire.Doc.Fields.TextRange ClassText = ClassCellParagraph.AppendText(s.Class);   
         //Format Cells   
         ClassCellParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         ClassText.CharacterFormat.FontName = "Calibri";   
         ClassText.CharacterFormat.FontSize = 12;   
         ClassText.CharacterFormat.TextColor = System.Drawing.Color.Brown;   
         //Cell Alignment   
         DataRow.Cells[3].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         //Fill Data in Rows   
         Spire.Doc.Documents.Paragraph SeCellParagraph = DataRow.Cells[3].AddParagraph();   
         Spire.Doc.Fields.TextRange SecText = SeCellParagraph.AppendText(s.Section);   
         //Format Cells   
         SeCellParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         SecText.CharacterFormat.FontName = "Calibri";   
         SecText.CharacterFormat.FontSize = 12;   
         SecText.CharacterFormat.TextColor = System.Drawing.Color.Brown;   
         //Cell Alignment   
         DataRow.Cells[4].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;   
         //Fill Data in Rows   
         Spire.Doc.Documents.Paragraph NumberCellParagraph = DataRow.Cells[4].AddParagraph();   
         Spire.Doc.Fields.TextRange NumberText = NumberCellParagraph.AppendText(s.Number.ToString());   
         //Format Cells   
         NumberCellParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;   
         NumberText.CharacterFormat.FontName = "Calibri";   
         NumberText.CharacterFormat.FontSize = 12;   
         NumberText.CharacterFormat.TextColor = System.Drawing.Color.Brown;   
         ind++;   
       }   
         );   
Save your document using SaveToFile function of document class, see the below code
 doc.SaveToFile("D://MyReport.docx", Spire.Doc.FileFormat.Docx);   
 System.Diagnostics.Process.Start("D://MyReport.docx");   

Thanks