Skip to content

Export-To-Excel-Updated #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
53 changes: 24 additions & 29 deletions Export-Array-to-Excel/Export-Array-to-Excel/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,39 @@ class Program
{
static void Main(string[] args)
{
try

using (ExcelEngine excelEngine = new ExcelEngine())
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Reads input Excel stream as a workbook
IWorkbook workbook = application.Workbooks.Open(File.OpenRead(Path.GetFullPath(@"../../../Expenses.xlsx")));
IWorksheet sheet = workbook.Worksheets[0];
//Reads input Excel stream as a workbook
IWorkbook workbook = application.Workbooks.Open(File.OpenRead(Path.GetFullPath(@"../../../Expenses.xlsx")));
IWorksheet sheet = workbook.Worksheets[0];

//Preparing first array with different data types
object[] expenseArray = new object[14]
{"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)"};
//Preparing first array with different data types
object[] expenseArray = new object[14]
{"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)"};

//Inserting a new row by formatting as a previous row.
sheet.InsertRow(11, 1, ExcelInsertOptions.FormatAsBefore);
//Inserting a new row by formatting as a previous row.
sheet.InsertRow(11, 1, ExcelInsertOptions.FormatAsBefore);

//Import Peter's expenses and fill it horizontally
sheet.ImportArray(expenseArray, 11, 1, false);
//Import Peter's expenses and fill it horizontally
sheet.ImportArray(expenseArray, 11, 1, false);

//Preparing second array with double data type
double[] expensesOnDec = new double[6]
{179.00d, 298.00d, 484.00d, 145.00d, 20.00d, 497.00d};
//Preparing second array with double data type
double[] expensesOnDec = new double[6]
{179.00d, 298.00d, 484.00d, 145.00d, 20.00d, 497.00d};

//Modify the December month's expenses and import it vertically
sheet.ImportArray(expensesOnDec, 6, 13, true);
//Modify the December month's expenses and import it vertically
sheet.ImportArray(expensesOnDec, 6, 13, true);

//Save the file in the given path
Stream excelStream = File.Create(Path.GetFullPath(@"Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose();
}
}
catch (Exception e)
{
Console.WriteLine("Unexpected Exception:"+e.Message);
//Save the file in the given path
Stream excelStream = File.Create(Path.GetFullPath(@"Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose();
}

}
}
}
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Export-CSV-to-Excel/Export-CSV-to-Excel/Output.xlsx
Binary file not shown.
102 changes: 28 additions & 74 deletions Export-CSV-to-Excel/Export-CSV-to-Excel/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Syncfusion. XlsIO;
using Syncfusion.XlsIO;
using System;
using System. IO;
using System.Linq;
Expand All @@ -8,86 +8,40 @@ class Program
{
static void Main(string[] args)
{
try
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;

//Preserve data types as per the value
application.PreserveCSVDataTypes = true;

//Read the CSV file
Stream csvStream = File.OpenRead(Path.GetFullPath(@"../../../TemplateSales.csv")); ;

//Reads CSV stream as a workbook
IWorkbook workbook = application.Workbooks.Open(csvStream);
IWorksheet sheet = workbook.Worksheets[0];

//Formatting the CSV data as a Table
IListObject table = sheet.ListObjects.Create("SalesTable", sheet.UsedRange);
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium6;
IRange location = table.Location;
location.AutofitColumns();

//Apply the proper latitude & longitude numerformat in the table
TryAndUpdateGeoLocation(table,"Latitude");
TryAndUpdateGeoLocation(table,"Longitude");
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Apply currency numberformat in the table column 'Price'
IRange columnRange = GetListObjectColumnRange(table,"Price");
if(columnRange != null)
columnRange.CellStyle.NumberFormat = "$#,##0.00";
//Preserve data types as per the value
application.PreserveCSVDataTypes = true;

//Apply Date time numberformat in the table column 'Transaction_date'
columnRange = GetListObjectColumnRange(table,"Transaction_date");
if(columnRange != null)
columnRange.CellStyle.NumberFormat = "m/d/yy h:mm AM/PM;@";
//Read the CSV file
Stream csvStream = File.OpenRead(Path.GetFullPath(@"../../../TemplateSales.csv")); ;

//Sort the data based on 'Products'
IDataSort sorter = table.AutoFilters.DataSorter;
ISortField sortField = sorter. SortFields. Add(0, SortOn. Values, OrderBy. Ascending);
sorter. Sort();
//Reads CSV stream as a workbook
IWorkbook workbook = application.Workbooks.Open(csvStream);
IWorksheet sheet = workbook.Worksheets[0];

//Save the file in the given path
Stream excelStream;
excelStream = File.Create(Path.GetFullPath(@"../../../Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose();
}
}
catch(Exception e)
{
Console.WriteLine("Unexpected Exception:"+e.Message);
}
}

private static void TryAndUpdateGeoLocation(IListObject table, string unitString)
{
IRange columnRange = GetListObjectColumnRange(table, unitString);
if(columnRange == null) return;
columnRange.Worksheet.EnableSheetCalculations();
foreach(IRange range in columnRange.Cells)
{
string currentValue = range.Value;
range.Value2 = "=TEXT(TRUNC("+currentValue+"), \"0\" & CHAR(176) & \" \") &" +
" TEXT(INT((ABS("+currentValue+")- INT(ABS("+currentValue+")))*60), \"0' \") " +
"& TEXT(((((ABS("+currentValue+")-INT(ABS("+currentValue+")))*60)-" +
" INT((ABS("+currentValue+") - INT(ABS("+currentValue+")))*60))*60), \" 0''\")";
}
}

private static IRange GetListObjectColumnRange(IListObject table, string name)
{
IListObjectColumn column = table.Columns.FirstOrDefault(x=> x.Name.Contains(name, StringComparison.InvariantCultureIgnoreCase));
if(column!=null)
{
//Formatting the CSV data as a Table
IListObject table = sheet.ListObjects.Create("SalesTable", sheet.UsedRange);
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium6;
IRange location = table.Location;
return location.Worksheet[location.Row+1, location.Column + column.Index - 1, location.LastRow, location.Column + column.Index - 1 ];
location.AutofitColumns();

//Sort the data based on 'Products'
IDataSort sorter = table.AutoFilters.DataSorter;
ISortField sortField = sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
sorter.Sort();

//Save the file in the given path
Stream excelStream;
excelStream = File.Create(Path.GetFullPath(@"../../../Output.xlsx"));
workbook.SaveAs(excelStream);
excelStream.Dispose();
}
else
return null;

}
}
}
Loading