-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelUploadController.cs
68 lines (62 loc) · 3.02 KB
/
ExcelUploadController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ExcelUpload.Controllers
{
public class ExcelUploadController : Controller
{
//
// GET: /ExcelUpload/
public String Index()
{
return "Upload Code is Ready";
}
[HttpPost]
public String ExcelFilePost(HttpPostedFileBase fileData)
{
String filename,type;
byte[] fileBytes=null;
List<ExcelUpload.Models.Insurance_tbl> InsList = new List<ExcelUpload.Models.Insurance_tbl>();
if(Request!=null)
{
for(int i=0;i<Request.Files.Count;i++)
{
fileData = Request.Files["UploadedFile"];
if((fileData!=null)&&(fileData.ContentLength>0)&& (!string.IsNullOrEmpty(fileData.FileName)))
{
filename=fileData.FileName;
type=fileData.ContentType;
fileBytes=new byte[fileData.ContentLength];
var StreamedData=fileData.InputStream.Read(fileBytes,0,fileData.ContentLength);
using (var excelObj = new ExcelPackage(fileData.InputStream))
{
var currentSheet = excelObj.Workbook.Worksheets;
var workSheet = currentSheet.First();
var noOfCol=workSheet.Dimension.End.Column;
var noOfRow = workSheet.Dimension.End.Row;
//application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
for(int r=2 ;r<=noOfRow;r++)
{
var insObj=new ExcelUpload.Models.Insurance_tbl();
insObj.DateOfService=Convert.ToDateTime(workSheet.Cells[r,1].Value);
insObj.InsuranceCode = Convert.ToString(workSheet.Cells[r, 2].Value);
insObj.InsuranceId = Convert.ToInt32(workSheet.Cells[r, 3].Value);
insObj.Name = Convert.ToString(workSheet.Cells[r, 4].Value);
insObj.PhoneNo = Convert.ToString(workSheet.Cells[r, 5].Value);
insObj.Address = Convert.ToString(workSheet.Cells[r, 6].Value);
insObj.Email = Convert.ToString(workSheet.Cells[r, 7].Value);
//insObj.SumAssured = Convert.ToDouble(workSheet.Cells[r, 8].Value);
insObj.Premium = Convert.ToInt32(workSheet.Cells[r, 9].Value);
insObj.Nominee = Convert.ToString(workSheet.Cells[r, 10].Value);
}
}
}
}
}
return "File uploaded";
}
}
}