Watch Pre-recorded Live Shows Here
Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
Monthly Leaders
ASK A QUESTION
Forum guidelines
Harish Batchu
1.9k
247
47k
Format excel using epplus?
Oct 30 2019 2:33 AM
I would like to format excel using epplus like below
but when i do that i am getting following
The following code I am using
public
static
DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable =
new
DataTable(
typeof
(T).Name);
dataTable.Columns.AddRange(
new
DataColumn[3] {
new
DataColumn(
"Common Fields"
),
new
DataColumn(
"Retail Handyman"
,
typeof
(
string
)),
new
DataColumn(
"QuickBooks"
,
typeof
(
string
))
});
dataTable.Columns.AddRange(
new
DataColumn[9] {
new
DataColumn(
"S.No"
,
typeof
(
int
)),
new
DataColumn(
"Job ID"
,
typeof
(
string
)),
new
DataColumn(
"Invoice ID"
,
typeof
(
string
)),
new
DataColumn(
"PO #"
,
typeof
(
string
)),
new
DataColumn(
"Sign Date"
,
typeof
(
string
)),
new
DataColumn(
"Store #"
,
typeof
(
string
)),
new
DataColumn(
"PO "
,
typeof
(
string
)),
new
DataColumn(
"SignDate"
,
typeof
(
string
)),
new
DataColumn(
"Store"
,
typeof
(
string
))});
//Get all the properties by using reflection
PropertyInfo[] Props =
typeof
(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// foreach (PropertyInfo prop in Props)
// {
// //Setting column names as Property names
// dataTable.Columns.Add(prop.Name);
// }
foreach
(T item
in
items)
{
var values =
new
object
[Props.Length];
for
(
int
i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item,
null
);
}
dataTable.Rows.Add(values);
}
return
dataTable;
}
For converting excel
public
static
Attachment GetAttachment(DataTable dataTable)
{
MemoryStream outputStream =
new
MemoryStream();
using
(ExcelPackage package =
new
ExcelPackage(outputStream))
{
ExcelWorksheet facilityWorksheet = package.Workbook.Worksheets.Add(
"sheetName"
);
facilityWorksheet.Cells.LoadFromDataTable(dataTable,
true
);
package.Save();
}
outputStream.Position = 0;
Attachment attachment =
new
Attachment(outputStream,
"MisMatchReport.xlsx"
,
"application/vnd.ms-excel"
);
return
attachment;
}
Please Help
Reply
Answers (
1
)
The Document not loading into wordprocessing
webconfig issue