Commit fc7baa8f by IMRAN AHMED KHAN

codeFormatted

parent ffa172b3
...@@ -28,11 +28,12 @@ namespace ExcelToJson.Controllers ...@@ -28,11 +28,12 @@ namespace ExcelToJson.Controllers
{ {
return new string[] { "value1", "value2" }; return new string[] { "value1", "value2" };
} }
// GET api/values // GET api/values
[HttpGet("excel/getJson")] [HttpGet("excel/getJson")]
public ActionResult GetJson() public ActionResult GetJson()
{ {
Utility utility =new Utility(); Utility utility = new Utility();
var webRoot = _env.WebRootPath; var webRoot = _env.WebRootPath;
FileInfo file = new FileInfo(System.IO.Path.Combine(webRoot, "savar-shivalay-manual data-v3.xlsx")); FileInfo file = new FileInfo(System.IO.Path.Combine(webRoot, "savar-shivalay-manual data-v3.xlsx"));
List<ProjectData> projectDataList = new List<ProjectData>(); List<ProjectData> projectDataList = new List<ProjectData>();
...@@ -40,9 +41,9 @@ namespace ExcelToJson.Controllers ...@@ -40,9 +41,9 @@ namespace ExcelToJson.Controllers
{ {
using (ExcelPackage package = new ExcelPackage(file)) using (ExcelPackage package = new ExcelPackage(file))
{ {
for (int i=1;i<= package.Workbook.Worksheets.Count;i++) for (int i = 1; i <= package.Workbook.Worksheets.Count; i++)
{ {
ExcelWorksheet worksheet = package.Workbook.Worksheets[i]; ExcelWorksheet worksheet = package.Workbook.Worksheets[i];
int rowCount = worksheet.Dimension.Rows; int rowCount = worksheet.Dimension.Rows;
...@@ -53,103 +54,106 @@ namespace ExcelToJson.Controllers ...@@ -53,103 +54,106 @@ namespace ExcelToJson.Controllers
int counter = 1; int counter = 1;
if( worksheet.Cells[row, 1].Value!=null ){ if (worksheet.Cells[row, 1].Value != null)
projectData.PROJECTID=worksheet.Cells[row, 1].Value.ToString(); {
projectData.PROJECTID = worksheet.Cells[row, 1].Value.ToString();
} }
if( worksheet.Cells[row, 2].Value!=null ){ if (worksheet.Cells[row, 2].Value != null)
projectData.CODE_PART1=worksheet.Cells[row, 2].Value.ToString();; {
projectData.CODE_PART1 = worksheet.Cells[row, 2].Value.ToString(); ;
} }
if( worksheet.Cells[row, 3].Value!=null ){ if (worksheet.Cells[row, 3].Value != null)
projectData.CODE_PART2=worksheet.Cells[row, 3].Value.ToString();; {
projectData.CODE_PART2 = worksheet.Cells[row, 3].Value.ToString(); ;
} }
if( worksheet.Cells[row, 4].Value!=null ){ if (worksheet.Cells[row, 4].Value != null)
projectData.PROJECTTITLE=worksheet.Cells[row, 4].Value.ToString();; {
projectData.PROJECTTITLE = worksheet.Cells[row, 4].Value.ToString(); ;
} }
if( worksheet.Cells[row, 5].Value!=null ){ if (worksheet.Cells[row, 5].Value != null)
{
projectData.PROJECTNAME = worksheet.Cells[row, 5].Value.ToString(); projectData.PROJECTNAME = worksheet.Cells[row, 5].Value.ToString();
} }
if( worksheet.Cells[row, 6].Value!=null ){ if (worksheet.Cells[row, 6].Value != null)
projectData.SCHEMENAME_ENG=worksheet.Cells[row, 6].Value.ToString();; {
projectData.SCHEMENAME_ENG = worksheet.Cells[row, 6].Value.ToString(); ;
} }
if( worksheet.Cells[row, 7].Value!=null ){ if (worksheet.Cells[row, 7].Value != null)
projectData.SCHEMENAME_BEN=worksheet.Cells[row, 7].Value.ToString();; {
projectData.SCHEMENAME_BEN = worksheet.Cells[row, 7].Value.ToString(); ;
} }
if( worksheet.Cells[row, 8].Value!=null ){ if (worksheet.Cells[row, 8].Value != null)
projectData.PACKAGEID=worksheet.Cells[row, 8].Value.ToString();; {
projectData.PACKAGEID = worksheet.Cells[row, 8].Value.ToString(); ;
} }
if( worksheet.Cells[row, 9].Value!=null ){ if (worksheet.Cells[row, 9].Value != null)
{
projectData.PACKAGECODE = worksheet.Cells[row, 9].Value.ToString(); projectData.PACKAGECODE = worksheet.Cells[row, 9].Value.ToString();
} }
if( worksheet.Cells[row, 10].Value!=null ){ if (worksheet.Cells[row, 10].Value != null)
projectData.COMPONENTSUBHEADID=worksheet.Cells[row, 10].Value.ToString();; {
projectData.COMPONENTSUBHEADID = worksheet.Cells[row, 10].Value.ToString(); ;
} }
if( worksheet.Cells[row, 11].Value!=null ){ if (worksheet.Cells[row, 11].Value != null)
projectData.COMPONENTSUBHEADNAME=worksheet.Cells[row, 11].Value.ToString();; {
projectData.COMPONENTSUBHEADNAME = worksheet.Cells[row, 11].Value.ToString(); ;
} }
if( worksheet.Cells[row, 12].Value!=null ){ if (worksheet.Cells[row, 12].Value != null)
projectData.UPAZILAID = worksheet.Cells[row, 12].Value.ToString(); {
projectData.UPAZILAID = worksheet.Cells[row, 12].Value.ToString();
} }
if (worksheet.Cells[row, 13].Value != null) if (worksheet.Cells[row, 13].Value != null)
{ {
projectData.ROADID = worksheet.Cells[row, 13].Value.ToString(); projectData.ROADID = worksheet.Cells[row, 13].Value.ToString();
} }
if ( worksheet.Cells[row, 14].Value!=null ){ if (worksheet.Cells[row, 14].Value != null)
{
projectData.ROADLENGTH = worksheet.Cells[row, 14].Value.ToString(); projectData.ROADLENGTH = worksheet.Cells[row, 14].Value.ToString();
} }
if( worksheet.Cells[row, 15].Value!=null ){ if (worksheet.Cells[row, 15].Value != null)
projectData.DISTRICTID=worksheet.Cells[row, 15].Value.ToString();; {
projectData.DISTRICTID = worksheet.Cells[row, 15].Value.ToString(); ;
} }
if( worksheet.Cells[row, 16].Value!=null ){ if (worksheet.Cells[row, 16].Value != null)
projectData.ACOMPLETIONDATE = Utility.GetStandardFormattedDate(worksheet.Cells[row, 16].Value.ToString()) ; {
projectData.ACOMPLETIONDATE = Utility.GetStandardFormattedDate(worksheet.Cells[row, 16].Value.ToString());
} }
if( worksheet.Cells[row, 17].Value!=null ){ if (worksheet.Cells[row, 17].Value != null)
{
projectData.CONTRACTORNAME = worksheet.Cells[row, 17].Value.ToString(); projectData.CONTRACTORNAME = worksheet.Cells[row, 17].Value.ToString();
} }
if (worksheet.Cells[row, 18].Value != null)
{
projectData.CONTRACTSIGNDATE = Utility.GetStandardFormattedDate(worksheet.Cells[row, 18].Value.ToString());
if( worksheet.Cells[row, 18].Value!=null ){
projectData.CONTRACTSIGNDATE =Utility.GetStandardFormattedDate(worksheet.Cells[row, 18].Value.ToString()) ;
} }
if( worksheet.Cells[row, 19].Value!=null ){ if (worksheet.Cells[row, 19].Value != null)
projectData.FINANCIALYEAR=worksheet.Cells[row, 19].Value.ToString();; {
projectData.FINANCIALYEAR = worksheet.Cells[row, 19].Value.ToString(); ;
} }
if( worksheet.Cells[row, 20].Value!=null ){ if (worksheet.Cells[row, 20].Value != null)
projectData.SCHEMECODE=worksheet.Cells[row, 20].Value.ToString();; {
projectData.SCHEMECODE = worksheet.Cells[row, 20].Value.ToString(); ;
} }
if( worksheet.Cells[row, 21].Value!=null ){ if (worksheet.Cells[row, 21].Value != null)
projectData.PHYSICALPROGGRESS = worksheet.Cells[row,21].Value.ToString(); {
projectData.PHYSICALPROGGRESS = worksheet.Cells[row, 21].Value.ToString();
} }
if( worksheet.Cells[row, 22].Value!=null ){ if (worksheet.Cells[row, 22].Value != null)
projectData.STATUS=worksheet.Cells[row, 22].Value.ToString();; {
projectData.STATUS = worksheet.Cells[row, 22].Value.ToString(); ;
} }
projectDataList.Add(projectData); projectDataList.Add(projectData);
} }
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
//return "Some error occured while importing." + ex.Message; return Ok("Some error occured while importing." + ex.Message);
} }
return Ok(projectDataList); return Ok(projectDataList);
...@@ -158,92 +162,93 @@ namespace ExcelToJson.Controllers ...@@ -158,92 +162,93 @@ namespace ExcelToJson.Controllers
//https://www.talkingdotnet.com/import-export-xlsx-asp-net-core/ //https://www.talkingdotnet.com/import-export-xlsx-asp-net-core/
//export a excel file //export a excel file
/* /*
[HttpGet] [HttpGet]
[Route("Export")] [Route("Export")]
public string Export() public string Export()
{ {
string sWebRootFolder = _hostingEnvironment.WebRootPath; string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"demo.xlsx"; string sFileName = @"demo.xlsx";
string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName); string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
if (file.Exists) if (file.Exists)
{ {
file.Delete(); file.Delete();
file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
} }
using (ExcelPackage package = new ExcelPackage(file)) using (ExcelPackage package = new ExcelPackage(file))
{ {
// add a new worksheet to the empty workbook // add a new worksheet to the empty workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee"); ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
//First add the headers //First add the headers
worksheet.Cells[1, 1].Value = "ID"; worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name"; worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Gender"; worksheet.Cells[1, 3].Value = "Gender";
worksheet.Cells[1, 4].Value = "Salary (in $)"; worksheet.Cells[1, 4].Value = "Salary (in $)";
//Add values //Add values
worksheet.Cells["A2"].Value = 1000; worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["B2"].Value = "Jon"; worksheet.Cells["B2"].Value = "Jon";
worksheet.Cells["C2"].Value = "M"; worksheet.Cells["C2"].Value = "M";
worksheet.Cells["D2"].Value = 5000; worksheet.Cells["D2"].Value = 5000;
worksheet.Cells["A3"].Value = 1001; worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "Graham"; worksheet.Cells["B3"].Value = "Graham";
worksheet.Cells["C3"].Value = "M"; worksheet.Cells["C3"].Value = "M";
worksheet.Cells["D3"].Value = 10000; worksheet.Cells["D3"].Value = 10000;
worksheet.Cells["A4"].Value = 1002; worksheet.Cells["A4"].Value = 1002;
worksheet.Cells["B4"].Value = "Jenny"; worksheet.Cells["B4"].Value = "Jenny";
worksheet.Cells["C4"].Value = "F"; worksheet.Cells["C4"].Value = "F";
worksheet.Cells["D4"].Value = 5000; worksheet.Cells["D4"].Value = 5000;
package.Save(); //Save the workbook. package.Save(); //Save the workbook.
} }
return URL; return URL;
} }*/
*/
//read from a excel //read from a excel
/* /*
[HttpGet] [HttpGet]
[Route("Import")] [Route("Import")]
public string Import() public string Import()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"demo.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
try
{
using (ExcelPackage package = new ExcelPackage(file))
{
StringBuilder sb = new StringBuilder();
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{ {
if (bHeaderRow) string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"demo.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
try
{ {
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t"); using (ExcelPackage package = new ExcelPackage(file))
{
StringBuilder sb = new StringBuilder();
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
bool bHeaderRow = true;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{
if (bHeaderRow)
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
else
{
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t");
}
}
sb.Append(Environment.NewLine);
}
return sb.ToString();
}
} }
else catch (Exception ex)
{ {
sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t"); return "Some error occured while importing." + ex.Message;
} }
} }
sb.Append(Environment.NewLine); */
}
return sb.ToString();
}
}
catch (Exception ex)
{
return "Some error occured while importing." + ex.Message;
}
}*/
// GET api/values/5 // GET api/values/5
[HttpGet("{id}")] [HttpGet("{id}")]
......
91c3067690a97cdc4d11db1ac361192d56571460 400339070a838111c02fcd0e2f0e5e42cb5e9487
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment