TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
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
Forums
Monthly Leaders
Forum guidelines
ricki Hutagaol
NA
6
850
Import Data From excel into Sql Server using C# and Eppplus
Jun 15 2020 1:42 AM
I Have a Problem with my project ,
It's a Employee absence application,
one of the features is
upload schedule, the people from human resource department ,can upload schedule of each employee by using this form /features
, but there is some problem with this features,
in some case people can upload excel file with wrong date format
but in excel file format cells like this
when i upload this excel file ,it's failed ,
but i try to changes into format cells like this
and it was succesfully uploaded ,
it become a problem because not only me use this application,
how can i solve this?
this is my code
protected
void
excel(
object
sender, EventArgs e) {
using
(ExcelPackage package =
new
ExcelPackage(existingFile))
//mypath
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int
colCount = worksheet.Dimension.End.Column;
int
rowCount = worksheet.Dimension.End.Row;
table.Columns.Add(
"Schedule_Date"
,
typeof
(DateTime));
for
(
int
i = 2; i <= rowCount; i++) {
if
(!worksheet.Cells[i, 1].Text.ToString().Equals(
""
)) {
if
(!worksheet.Cells[i, 2].Text.ToString().Equals(
""
)) {
if
(!worksheet.Cells[i, 3].Text.ToString().Equals(
""
)) {
DateTime d;
string
[] formats = {
"MM/dd/yyyy"
,
"M/d/yyyy"
,
"MM/d/yyyy"
,
"M/dd/yyyy"
,
"MM/dd/yyyy hh:mm:ss"
,
"MM/dd/yyyy hh:mm:ss tt"
,
"M/dd/yyyy hh:mm:ss tt"
,
"M/d/yyyy hh:mm:ss tt"
,
"MM/d/yyyy hh:mm:ss tt"
,
"MM/dd/yyyy h:mm:ss"
,
"MM/dd/yyyy"
,
"M/d/yyyy h:mm:ss"
};
TableRow row =
new
TableRow();
TableCell cell1 =
new
TableCell();
TableCell cell2 =
new
TableCell();
string
schedule_date = Convert.ToString(worksheet.Cells[i, 3].Value.ToString());
string
currentrow = worksheet.Cells[i, 1].Text.ToString() + worksheet.Cells[i, 3].Text.ToString();
for
(
int
j = i + 1; j < rowCount + 1; j++) {
string
nextrow = worksheet.Cells[j, 1].Text.ToString() + worksheet.Cells[j, 3].Text.ToString();
if
(!currentrow.Equals(nextrow)) {
if
(DateTime.TryParseExact(schedule_date, formats,
new
CultureInfo(
"en-US"
), DateTimeStyles.None,
out
d)) {
count += 0;
string
labelmonth = ddlTest.SelectedValue.ToString();
// string shift = worksheet.Cells[i, 2].Value.ToString();
string
employee_id = worksheet.Cells[i, 1].Value.ToString();
string
user = Session[
"LogedUserID"
].ToString();
bool
validatemonth = ddlTest.SelectedValue.ToString() == DateTime.Parse(worksheet.Cells[i, 3].Value.ToString()).ToString(
"MM"
);
if
(validatemonth.Equals(
false
)) {
count += 1;
cell1.Text =
"Line"
+ i +
" "
;
// +employee_id + " " + schedule_date;//" " + Convert.ToDateTime(schedule_date).ToString("MM/dd/yyyy");//+ " Baris ke " + i + " Bulan Tidak Sesuai";
cell2.Text =
"Invalid Month "
;
row.Cells.Add(cell1);
row.Cells.Add(cell2);
mytable.Rows.Add(row);
}
else
{
if
(!shifts.Equals(sshift)) {
count += 1;
count += 1;
cell1.Text =
"Line "
+ i +
" "
;
// +employee_id + " " + schedule_date;//" " + Convert.ToDateTime(schedule_date).ToString("MM/dd/yyyy");//+ " Baris ke " + i + " Bulan Tidak Sesuai";
cell2.Text =
"No Shift"
;
row.Cells.Add(cell1);
row.Cells.Add(cell2);
mytable.Rows.Add(row);
}
}
}
else
{
count += 1;
cell1.Text =
"Line "
+ i +
" "
+ Convert.ToString(schedule_date);
//.ToString("MM/dd/yyyy");
cell2.Text =
"Invalid Format"
;
row.Cells.Add(cell1);
row.Cells.Add(cell2);
mytable.Rows.Add(row);
}
}
else
{
cell1.Text =
" Line "
+ i.ToString();
cell2.Text =
"same value with "
+ j.ToString();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
mytable.Rows.Add(row);
count += 1;
}
}
}
}
}
}
}
if
(count.Equals(0)) {
insert();
File.Move(tempfile, arsipfile);
}
else
{
Response.Write(
"<script>window.alert('Data Tidak Tersimpan')</script>"
);
File.Delete(tempfile);
}
}
else
{
File.Delete(tempfile);
Response.Write(
"<script>window.alert('Pilih Bulan Terlebih dahulu')</script>"
);
}
}
}
else
{
Response.Write(
"<script>window.alert('File Belum Di Upload')</script>"
);
}
}
Reply
Answers (
0
)
how to create a token and then after authorize person access
How to call two models on a View in asp.net mvc?