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))
- {
- 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 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 + " ";
- 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 + " ";
- 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);
- 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>");
- }
- }