Skip to content

Commit 4d1f2d6

Browse files
1. Added the samples
2. Updated ReadMe with 6 methods of exporting data to Excel
1 parent bc7d274 commit 4d1f2d6

File tree

85 files changed

+4248
-1
lines changed

Some content is hidden

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

85 files changed

+4248
-1
lines changed
Binary file not shown.
Binary file not shown.

Export-Array-to-Excel/.vs/Export-Array-to-Excel/v15/Server/sqlite3/db.lock

Whitespace-only changes.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Export-Array-to-Excel", "Export-Array-to-Excel\Export-Array-to-Excel.csproj", "{ADBF6C2A-CB8D-4D03-AB01-702ECD65E8E0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{ADBF6C2A-CB8D-4D03-AB01-702ECD65E8E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{ADBF6C2A-CB8D-4D03-AB01-702ECD65E8E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{ADBF6C2A-CB8D-4D03-AB01-702ECD65E8E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{ADBF6C2A-CB8D-4D03-AB01-702ECD65E8E0}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {9F9BF09D-D5CC-4ED4-9763-C3F244AD5E99}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="17.1.0.38" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Syncfusion. XlsIO;
2+
using System;
3+
using System.IO;
4+
5+
namespace ImportFromArray
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
try
12+
{
13+
using (ExcelEngine excelEngine = new ExcelEngine())
14+
{
15+
IApplication application = excelEngine.Excel;
16+
application.DefaultVersion = ExcelVersion.Excel2016;
17+
18+
//Reads input Excel stream as a workbook
19+
IWorkbook workbook = application.Workbooks.Open(File.OpenRead(Path.GetFullPath(@"../../../Expenses.xlsx")));
20+
IWorksheet sheet = workbook.Worksheets[0];
21+
22+
//Preparing first array with different data types
23+
object[] expenseArray = new object[14]
24+
{"Paul Pogba", 469.00d, 263.00d, 131.00d, 139.00d, 474.00d, 253.00d, 467.00d, 142.00d, 417.00d, 324.00d, 328.00d, 497.00d, "=SUM(B11:M11)"};
25+
26+
//Inserting a new row by formatting as a previous row.
27+
sheet.InsertRow(11, 1, ExcelInsertOptions.FormatAsBefore);
28+
29+
//Import Peter's expenses and fill it horizontally
30+
sheet.ImportArray(expenseArray, 11, 1, false);
31+
32+
//Preparing second array with double data type
33+
double[] expensesOnDec = new double[6]
34+
{179.00d, 298.00d, 484.00d, 145.00d, 20.00d, 497.00d};
35+
36+
//Modify the December month's expenses and import it vertically
37+
sheet.ImportArray(expensesOnDec, 6, 13, true);
38+
39+
//Save the file in the given path
40+
Stream excelStream = File.Create(Path.GetFullPath(@"Output.xlsx"));
41+
workbook.SaveAs(excelStream);
42+
excelStream.Dispose();
43+
}
44+
}
45+
catch (Exception e)
46+
{
47+
Console.WriteLine("Unexpected Exception:"+e.Message);
48+
}
49+
}
50+
}
51+
}
Binary file not shown.

Export-CSV-to-Excel/.vs/Export-CSV-to-Excel/v15/Server/sqlite3/db.lock

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Export-CSV-to-Excel", "Export-CSV-to-Excel\Export-CSV-to-Excel.csproj", "{2FB159E0-C43B-4B88-BAF1-9030DF5A0195}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{2FB159E0-C43B-4B88-BAF1-9030DF5A0195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2FB159E0-C43B-4B88-BAF1-9030DF5A0195}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2FB159E0-C43B-4B88-BAF1-9030DF5A0195}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2FB159E0-C43B-4B88-BAF1-9030DF5A0195}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EA68AB82-33F1-4E03-90A7-730AAB8EDD3F}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="17.1.0.38" />
10+
</ItemGroup>
11+
12+
</Project>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using Syncfusion. XlsIO;
2+
using System;
3+
using System. IO;
4+
using System.Linq;
5+
namespace ImportFromCSV
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
try
12+
{
13+
using (ExcelEngine excelEngine = new ExcelEngine())
14+
{
15+
IApplication application = excelEngine.Excel;
16+
application.DefaultVersion = ExcelVersion.Excel2016;
17+
18+
//Preserve data types as per the value
19+
application.PreserveCSVDataTypes = true;
20+
21+
//Read the CSV file
22+
Stream csvStream = File.OpenRead(Path.GetFullPath(@"../../../TemplateSales.csv")); ;
23+
24+
//Reads CSV stream as a workbook
25+
IWorkbook workbook = application.Workbooks.Open(csvStream);
26+
IWorksheet sheet = workbook.Worksheets[0];
27+
28+
//Formatting the CSV data as a Table
29+
IListObject table = sheet.ListObjects.Create("SalesTable", sheet.UsedRange);
30+
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium6;
31+
IRange location = table.Location;
32+
location.AutofitColumns();
33+
34+
//Apply the proper latitude & longitude numerformat in the table
35+
TryAndUpdateGeoLocation(table,"Latitude");
36+
TryAndUpdateGeoLocation(table,"Longitude");
37+
38+
//Apply currency numberformat in the table column 'Price'
39+
IRange columnRange = GetListObjectColumnRange(table,"Price");
40+
if(columnRange != null)
41+
columnRange.CellStyle.NumberFormat = "$#,##0.00";
42+
43+
//Apply Date time numberformat in the table column 'Transaction_date'
44+
columnRange = GetListObjectColumnRange(table,"Transaction_date");
45+
if(columnRange != null)
46+
columnRange.CellStyle.NumberFormat = "m/d/yy h:mm AM/PM;@";
47+
48+
//Sort the data based on 'Products'
49+
IDataSort sorter = table.AutoFilters.DataSorter;
50+
ISortField sortField = sorter. SortFields. Add(0, SortOn. Values, OrderBy. Ascending);
51+
sorter. Sort();
52+
53+
//Save the file in the given path
54+
Stream excelStream;
55+
excelStream = File.Create(Path.GetFullPath(@"../../../Output.xlsx"));
56+
workbook.SaveAs(excelStream);
57+
excelStream.Dispose();
58+
}
59+
}
60+
catch(Exception e)
61+
{
62+
Console.WriteLine("Unexpected Exception:"+e.Message);
63+
}
64+
}
65+
66+
private static void TryAndUpdateGeoLocation(IListObject table, string unitString)
67+
{
68+
IRange columnRange = GetListObjectColumnRange(table, unitString);
69+
if(columnRange == null) return;
70+
columnRange.Worksheet.EnableSheetCalculations();
71+
foreach(IRange range in columnRange.Cells)
72+
{
73+
string currentValue = range.Value;
74+
range.Value2 = "=TEXT(TRUNC("+currentValue+"), \"0\" & CHAR(176) & \" \") &" +
75+
" TEXT(INT((ABS("+currentValue+")- INT(ABS("+currentValue+")))*60), \"0' \") " +
76+
"& TEXT(((((ABS("+currentValue+")-INT(ABS("+currentValue+")))*60)-" +
77+
" INT((ABS("+currentValue+") - INT(ABS("+currentValue+")))*60))*60), \" 0''\")";
78+
}
79+
}
80+
81+
private static IRange GetListObjectColumnRange(IListObject table, string name)
82+
{
83+
IListObjectColumn column = table.Columns.FirstOrDefault(x=> x.Name.Contains(name, StringComparison.InvariantCultureIgnoreCase));
84+
if(column!=null)
85+
{
86+
IRange location = table.Location;
87+
return location.Worksheet[location.Row+1, location.Column + column.Index - 1, location.LastRow, location.Column + column.Index - 1 ];
88+
}
89+
else
90+
return null;
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)