Skip to content

Commit 4e38187

Browse files
Merge branch 'development' into EJ2-999367-ug2
2 parents 19d8c9e + c48aa9e commit 4e38187

89 files changed

Lines changed: 11398 additions & 560 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Document-Processing-toc.html

Lines changed: 113 additions & 53 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: How to get the column color | Syncfusion.
3+
description: This page explains how to get the styled column color when column cells have different colors in an Excel document using Syncfusion .NET Excel library (XlsIO).
4+
platform: document-processing
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# How to get the column color when column cells have different colors?
10+
11+
According to Microsoft Excel behavior, when a column's cells have different fill colors, the column color property returns an empty value. Syncfusion XlsIO mirrors this behavior, as Excel doesn't set a unified column color in such cases. This can lead to issues when trying to retrieve color using column cell style properties.
12+
The following code example illustrates how to get the column color when column cells have different colors in an Excel document.
13+
14+
{% tabs %}
15+
{% highlight c# tabtitle="C# [Cross-platform]" %}
16+
using (ExcelEngine excelEngine = new ExcelEngine())
17+
{
18+
IApplication application = excelEngine.Excel;
19+
application.DefaultVersion = ExcelVersion.Xlsx;
20+
21+
IWorkbook workbook = application.Workbooks.Open("Column styles.xlsx", ExcelOpenType.Automatic);
22+
23+
IWorksheet worksheet = workbook.Worksheets[0];
24+
25+
ExtendedFormatImpl format = (workbook as WorkbookImpl).InnerExtFormats[(worksheet["A1"] as RangeImpl).ExtendedFormatIndex];
26+
Color color = format.Color;
27+
}
28+
{% endhighlight %}
29+
30+
{% highlight c# tabtitle="C# [Windows-specific]" %}
31+
using (ExcelEngine excelEngine = new ExcelEngine())
32+
{
33+
IApplication application = excelEngine.Excel;
34+
application.DefaultVersion = ExcelVersion.Xlsx;
35+
36+
IWorkbook workbook = application.Workbooks.Open("Column styles.xlsx", ExcelOpenType.Automatic);
37+
38+
IWorksheet worksheet = workbook.Worksheets[0];
39+
40+
ExtendedFormatImpl format = (workbook as WorkbookImpl).InnerExtFormats[(worksheet["A1"] as RangeImpl).ExtendedFormatIndex];
41+
Color color = format.Color;
42+
}
43+
{% endhighlight %}
44+
45+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
46+
Using excelEngine As New ExcelEngine()
47+
Dim application As IApplication = excelEngine.Excel
48+
application.DefaultVersion = ExcelVersion.Xlsx
49+
50+
Dim workbook As IWorkbook = application.Workbooks.Open("Column styles.xlsx", ExcelOpenType.Automatic)
51+
Dim worksheet As IWorksheet = workbook.Worksheets(0)
52+
53+
Dim format As ExtendedFormatImpl = DirectCast(workbook, WorkbookImpl).InnerExtFormats(DirectCast(worksheet("A1"), RangeImpl).ExtendedFormatIndex)
54+
Dim color As Color = format.Color
55+
56+
End Using
57+
{% endhighlight %}
58+
{% endtabs %}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Preserve leading zeros when importing data to Excel | Syncfusion
3+
description: Discover how to keep leading zeros in text when importing a DataTable to Excel using Syncfusion .NET Excel library.
4+
platform: document-processing
5+
control: XlsIO
6+
documentation: UG
7+
---
8+
9+
# How to preserve leading zeros when importing DataTable to Excel?
10+
11+
Excel often treats numeric-looking text as numbers, removing leading zeros. The same behavior is followed when importing DataTable to Excel by the Syncfusion .NET Excel (XlsIO) library. The leading zeros can be preserved by providing the parameter "PreserveDataTypes" in the ImportDataTable method as "True" when importing the data.
12+
13+
The following code snippet shows how to use the PreserveDataTypes option in XlsIO.
14+
15+
{% tabs %}
16+
{% highlight c# tabtitle="C# [Cross-platform]" %}
17+
18+
using (ExcelEngine excelEngine = new ExcelEngine())
19+
{
20+
IApplication application = excelEngine.Excel;
21+
application.DefaultVersion = ExcelVersion.Xlsx;
22+
IWorkbook workbook = application.Workbooks.Create(1);
23+
IWorksheet worksheet = workbook.Worksheets[0];
24+
25+
#region Import from Data Table
26+
//Initialize the DataTable
27+
DataTable table = SampleDataTable();
28+
//Set the final parameter (preserveTypes) to true to preserve the data types.
29+
worksheet.ImportDataTable(table, true, 1, 1, true);
30+
#endregion
31+
32+
#region Save
33+
//Saving the workbook
34+
workbook.SaveAs(Path.GetFullPath("Output/ImportDataTable.xlsx"));
35+
#endregion
36+
}
37+
static DataTable SampleDataTable()
38+
{
39+
//Create a DataTable with four columns
40+
DataTable table = new DataTable();
41+
table.Columns.Add("Dosage", typeof(int));
42+
table.Columns.Add("Drug", typeof(string));
43+
table.Columns.Add("Patient", typeof(string));
44+
table.Columns.Add("Date", typeof(DateTime));
45+
46+
//Add five DataRows
47+
table.Rows.Add(25, "032132", "David", DateTime.Now);
48+
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
49+
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
50+
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
51+
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
52+
53+
return table;
54+
}
55+
56+
{% endhighlight %}
57+
58+
{% highlight c# tabtitle="C# [Windows-specific]" %}
59+
60+
using (ExcelEngine excelEngine = new ExcelEngine())
61+
{
62+
IApplication application = excelEngine.Excel;
63+
application.DefaultVersion = ExcelVersion.Excel2016;
64+
IWorkbook workbook = application.Workbooks.Create(1);
65+
IWorksheet worksheet = workbook.Worksheets[0];
66+
67+
//Initialize the DataTable
68+
DataTable table = SampleDataTable();
69+
70+
//Set the final parameter (preserveTypes) to true to preserve the data types.
71+
worksheet.ImportDataTable(table, true, 1, 1, true);
72+
73+
workbook.SaveAs("ImportFromDT.xlsx");
74+
}
75+
private static DataTable SampleDataTable()
76+
{
77+
//Create a DataTable with four columns
78+
DataTable table = new DataTable();
79+
table.Columns.Add("Dosage", typeof(int));
80+
table.Columns.Add("Drug", typeof(string));
81+
table.Columns.Add("Patient", typeof(string));
82+
table.Columns.Add("Date", typeof(DateTime));
83+
84+
//Add five DataRows
85+
table.Rows.Add(25, "032132", "David", DateTime.Now);
86+
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
87+
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
88+
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
89+
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
90+
91+
return table;
92+
}
93+
94+
{% endhighlight %}
95+
96+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
97+
98+
Using excelEngine As ExcelEngine = New ExcelEngine()
99+
Dim application As IApplication = excelEngine.Excel
100+
application.DefaultVersion = ExcelVersion.Excel2016
101+
Dim workbook As IWorkbook = application.Workbooks.Create(1)
102+
Dim worksheet As IWorksheet = workbook.Worksheets(0)
103+
104+
'Initialize the DataTable
105+
Dim table As DataTable = sampleDataTable()
106+
'Set the final parameter (preserveTypes) to true to preserve the data types.
107+
worksheet.ImportDataTable(table, True, 1, 1, True)
108+
109+
workbook.SaveAs("ImportFromDT.xlsx")
110+
End Using
111+
Private Function SampleDataTable() As DataTable
112+
' Create a DataTable with four columns
113+
Dim table As New DataTable()
114+
table.Columns.Add("Dosage", GetType(Integer))
115+
table.Columns.Add("Drug", GetType(String))
116+
table.Columns.Add("Patient", GetType(String))
117+
table.Columns.Add("Date", GetType(Date))
118+
119+
' Add five DataRows
120+
table.Rows.Add(25, "032132", "David", DateTime.Now)
121+
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
122+
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
123+
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
124+
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
125+
Return table
126+
End Function
127+
128+
{% endhighlight %}
129+
{% endtabs %}
130+
131+
## See Also
132+
133+
* [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)
134+
* [Import to Excel Document](https://help.syncfusion.com/document-processing/excel/excel-library/net/import-export/import-to-excel)

0 commit comments

Comments
 (0)