Skip to content

Commit eff6956

Browse files
authored
Merge pull request #1817 from syncfusion-content/989035-DisplayFormatAttribute
989035-How to apply the formatting for a particular column while importing data from collection objects
2 parents 9b5a68a + 521ed42 commit eff6956

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: Apply formatting to a specific column when importing data | Syncfusion
3+
description: Code example showing how to apply formatting to a specific column when importing data from collection objects using the Syncfusion .NET Excel library (XlsIO).
4+
platform: document-processing
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# How to apply formatting to a specific column while importing data?
10+
11+
To apply formatting to a specific column while importing data from collection objects, use the **DisplayFormatAttribute** in the class definition, as shown in the example below.
12+
13+
{% tabs %}
14+
{% highlight c# tabtitle="C# [Cross-platform]" %}
15+
using (ExcelEngine excelEngine = new ExcelEngine())
16+
{
17+
IApplication application = excelEngine.Excel;
18+
application.DefaultVersion = ExcelVersion.Xlsx;
19+
IWorkbook workbook = application.Workbooks.Create(1);
20+
IWorksheet worksheet = workbook.Worksheets[0];
21+
22+
//Import the data to worksheet
23+
IList<Customer> reports = GetSalesReports();
24+
worksheet.ImportData(reports, 2, 1, false);
25+
26+
#region Save
27+
//Saving the workbook
28+
workbook.SaveAs(Path.GetFullPath("Output/ImportCollectionObjects.xlsx"));
29+
#endregion
30+
}
31+
32+
//Gets a list of sales reports
33+
public static List<Customer> GetSalesReports()
34+
{
35+
List<Customer> reports = new List<Customer>();
36+
reports.Add(new Customer("Andy Bernard", 45000, 58000));
37+
reports.Add(new Customer("Jim Halpert", 34000, 65000));
38+
reports.Add(new Customer("Karen Fillippelli", 75000, 64000));
39+
reports.Add(new Customer("Phyllis Lapin", 56500, 33600));
40+
reports.Add(new Customer("Stanley Hudson", 46500, 52000));
41+
return reports;
42+
}
43+
44+
//Customer details
45+
public class Customer
46+
{
47+
[DisplayNameAttribute("Sales Person Name")]
48+
public string SalesPerson
49+
{
50+
get; set;
51+
}
52+
**[DisplayFormat(DataFormatString = "$#,###.00")]**
53+
public int SalesJanJun { get; set; }
54+
55+
**[DisplayFormat(DataFormatString = "$#,###.00")]**
56+
public int SalesJulDec { get; set; }
57+
58+
public Customer(string name, int janToJun, int julToDec)
59+
{
60+
SalesPerson = name;
61+
SalesJanJun = janToJun;
62+
SalesJulDec = julToDec;
63+
}
64+
}
65+
{% endhighlight %}
66+
67+
{% highlight c# tabtitle="C# [Windows-specific]" %}
68+
using (ExcelEngine excelEngine = new ExcelEngine())
69+
{
70+
IApplication application = excelEngine.Excel;
71+
application.DefaultVersion = ExcelVersion.Xlsx;
72+
IWorkbook workbook = application.Workbooks.Create(1);
73+
IWorksheet worksheet = workbook.Worksheets[0];
74+
75+
//Import the data to worksheet
76+
IList<Customer> reports = GetSalesReports();
77+
worksheet.ImportData(reports, 2, 1, false);
78+
79+
#region Save
80+
//Saving the workbook
81+
workbook.SaveAs(Path.GetFullPath("Output/ImportCollectionObjects.xlsx"));
82+
#endregion
83+
}
84+
85+
//Gets a list of sales reports
86+
public static List<Customer> GetSalesReports()
87+
{
88+
List<Customer> reports = new List<Customer>();
89+
reports.Add(new Customer("Andy Bernard", 45000, 58000));
90+
reports.Add(new Customer("Jim Halpert", 34000, 65000));
91+
reports.Add(new Customer("Karen Fillippelli", 75000, 64000));
92+
reports.Add(new Customer("Phyllis Lapin", 56500, 33600));
93+
reports.Add(new Customer("Stanley Hudson", 46500, 52000));
94+
return reports;
95+
}
96+
97+
//Customer details
98+
public class Customer
99+
{
100+
[DisplayNameAttribute("Sales Person Name")]
101+
public string SalesPerson
102+
{
103+
get; set;
104+
}
105+
**[DisplayFormat(DataFormatString = "$#,###.00")]**
106+
public int SalesJanJun { get; set; }
107+
108+
**[DisplayFormat(DataFormatString = "$#,###.00")]**
109+
public int SalesJulDec { get; set; }
110+
111+
public Customer(string name, int janToJun, int julToDec)
112+
{
113+
SalesPerson = name;
114+
SalesJanJun = janToJun;
115+
SalesJulDec = julToDec;
116+
}
117+
}
118+
{% endhighlight %}
119+
120+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
121+
Using excelEngine As New ExcelEngine()
122+
Dim application As IApplication = excelEngine.Excel
123+
application.DefaultVersion = ExcelVersion.Xlsx
124+
125+
Dim workbook As IWorkbook = application.Workbooks.Create(1)
126+
Dim worksheet As IWorksheet = workbook.Worksheets(0)
127+
128+
' Import the data to worksheet
129+
Dim reports As IList(Of Customer) = GetSalesReports()
130+
worksheet.ImportData(reports, 2, 1, False)
131+
132+
' Saving the workbook
133+
workbook.SaveAs(Path.GetFullPath("Output/ImportCollectionObjects.xlsx"))
134+
End Using
135+
136+
' Gets a list of sales reports
137+
Public Function GetSalesReports() As List(Of Customer)
138+
Dim reports As New List(Of Customer)()
139+
reports.Add(New Customer("Andy Bernard", 45000, 58000))
140+
reports.Add(New Customer("Jim Halpert", 34000, 65000))
141+
reports.Add(New Customer("Karen Fillippelli", 75000, 64000))
142+
reports.Add(New Customer("Phyllis Lapin", 56500, 33600))
143+
reports.Add(New Customer("Stanley Hudson", 46500, 52000))
144+
Return reports
145+
End Function
146+
147+
' Customer details
148+
Public Class Customer
149+
<DisplayName("Sales Person Name")>
150+
Public Property SalesPerson As String
151+
152+
**<DisplayFormat(DataFormatString:="$#,###.00")>**
153+
Public Property SalesJanJun As Integer
154+
155+
**<DisplayFormat(DataFormatString:="$#,###.00")>**
156+
Public Property SalesJulDec As Integer
157+
158+
Public Sub New(name As String, janToJun As Integer, julToDec As Integer)
159+
SalesPerson = name
160+
SalesJanJun = janToJun
161+
SalesJulDec = julToDec
162+
End Sub
163+
End Class
164+
{% endhighlight %}
165+
{% endtabs %}
166+
167+
## See Also
168+
169+
* [Collection Objects to Excel](https://help.syncfusion.com/document-processing/excel/excel-library/net/import-export/import-to-excel#collection-objects-to-excel)
170+
* [How to import data table with its data type using template markers?](https://help.syncfusion.com/document-processing/excel/excel-library/net/faqs/how-to-import-data-table-with-its-data-type-using-template-markers)
171+
* [Working with Template Markers](https://help.syncfusion.com/document-processing/excel/excel-library/net/working-with-template-markers)

0 commit comments

Comments
 (0)