Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ExcelToJson-asp.net-Core
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
IMRAN AHMED KHAN
ExcelToJson-asp.net-Core
Commits
fc7baa8f
Commit
fc7baa8f
authored
Apr 03, 2019
by
IMRAN AHMED KHAN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
codeFormatted
parent
ffa172b3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
147 additions
and
142 deletions
+147
-142
ValuesController.cs
ExcelToJson/Controllers/ValuesController.cs
+146
-141
ExcelToJson.assets.cache
ExcelToJson/obj/Debug/netcoreapp2.1/ExcelToJson.assets.cache
+0
-0
ExcelToJson.csproj.CoreCompileInputs.cache
.../netcoreapp2.1/ExcelToJson.csproj.CoreCompileInputs.cache
+1
-1
ExcelToJson.csprojAssemblyReference.cache
...g/netcoreapp2.1/ExcelToJson.csprojAssemblyReference.cache
+0
-0
No files found.
ExcelToJson/Controllers/ValuesController.cs
View file @
fc7baa8f
...
...
@@ -28,11 +28,12 @@ namespace ExcelToJson.Controllers
{
return
new
string
[]
{
"value1"
,
"value2"
};
}
// GET api/values
[
HttpGet
(
"excel/getJson"
)]
public
ActionResult
GetJson
()
{
Utility
utility
=
new
Utility
();
Utility
utility
=
new
Utility
();
var
webRoot
=
_env
.
WebRootPath
;
FileInfo
file
=
new
FileInfo
(
System
.
IO
.
Path
.
Combine
(
webRoot
,
"savar-shivalay-manual data-v3.xlsx"
));
List
<
ProjectData
>
projectDataList
=
new
List
<
ProjectData
>();
...
...
@@ -40,9 +41,9 @@ namespace ExcelToJson.Controllers
{
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
];
int
rowCount
=
worksheet
.
Dimension
.
Rows
;
...
...
@@ -53,103 +54,106 @@ namespace ExcelToJson.Controllers
int
counter
=
1
;
if
(
worksheet
.
Cells
[
row
,
1
].
Value
!=
null
){
projectData
.
PROJECTID
=
worksheet
.
Cells
[
row
,
1
].
Value
.
ToString
();
if
(
worksheet
.
Cells
[
row
,
1
].
Value
!=
null
)
{
projectData
.
PROJECTID
=
worksheet
.
Cells
[
row
,
1
].
Value
.
ToString
();
}
if
(
worksheet
.
Cells
[
row
,
2
].
Value
!=
null
){
projectData
.
CODE_PART1
=
worksheet
.
Cells
[
row
,
2
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
2
].
Value
!=
null
)
{
projectData
.
CODE_PART1
=
worksheet
.
Cells
[
row
,
2
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
3
].
Value
!=
null
){
projectData
.
CODE_PART2
=
worksheet
.
Cells
[
row
,
3
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
3
].
Value
!=
null
)
{
projectData
.
CODE_PART2
=
worksheet
.
Cells
[
row
,
3
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
4
].
Value
!=
null
){
projectData
.
PROJECTTITLE
=
worksheet
.
Cells
[
row
,
4
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
4
].
Value
!=
null
)
{
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
();
}
if
(
worksheet
.
Cells
[
row
,
6
].
Value
!=
null
){
projectData
.
SCHEMENAME_ENG
=
worksheet
.
Cells
[
row
,
6
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
6
].
Value
!=
null
)
{
projectData
.
SCHEMENAME_ENG
=
worksheet
.
Cells
[
row
,
6
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
7
].
Value
!=
null
){
projectData
.
SCHEMENAME_BEN
=
worksheet
.
Cells
[
row
,
7
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
7
].
Value
!=
null
)
{
projectData
.
SCHEMENAME_BEN
=
worksheet
.
Cells
[
row
,
7
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
8
].
Value
!=
null
){
projectData
.
PACKAGEID
=
worksheet
.
Cells
[
row
,
8
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
8
].
Value
!=
null
)
{
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
();
}
if
(
worksheet
.
Cells
[
row
,
10
].
Value
!=
null
){
projectData
.
COMPONENTSUBHEADID
=
worksheet
.
Cells
[
row
,
10
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
10
].
Value
!=
null
)
{
projectData
.
COMPONENTSUBHEADID
=
worksheet
.
Cells
[
row
,
10
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
11
].
Value
!=
null
){
projectData
.
COMPONENTSUBHEADNAME
=
worksheet
.
Cells
[
row
,
11
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
11
].
Value
!=
null
)
{
projectData
.
COMPONENTSUBHEADNAME
=
worksheet
.
Cells
[
row
,
11
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
12
].
Value
!=
null
){
projectData
.
UPAZILAID
=
worksheet
.
Cells
[
row
,
12
].
Value
.
ToString
();
if
(
worksheet
.
Cells
[
row
,
12
].
Value
!=
null
)
{
projectData
.
UPAZILAID
=
worksheet
.
Cells
[
row
,
12
].
Value
.
ToString
();
}
if
(
worksheet
.
Cells
[
row
,
13
].
Value
!=
null
)
{
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
();
}
if
(
worksheet
.
Cells
[
row
,
15
].
Value
!=
null
){
projectData
.
DISTRICTID
=
worksheet
.
Cells
[
row
,
15
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
15
].
Value
!=
null
)
{
projectData
.
DISTRICTID
=
worksheet
.
Cells
[
row
,
15
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
16
].
Value
!=
null
){
projectData
.
ACOMPLETIONDATE
=
Utility
.
GetStandardFormattedDate
(
worksheet
.
Cells
[
row
,
16
].
Value
.
ToString
())
;
if
(
worksheet
.
Cells
[
row
,
16
].
Value
!=
null
)
{
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
();
}
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
){
projectData
.
FINANCIALYEAR
=
worksheet
.
Cells
[
row
,
19
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
19
].
Value
!=
null
)
{
projectData
.
FINANCIALYEAR
=
worksheet
.
Cells
[
row
,
19
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
20
].
Value
!=
null
){
projectData
.
SCHEMECODE
=
worksheet
.
Cells
[
row
,
20
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
20
].
Value
!=
null
)
{
projectData
.
SCHEMECODE
=
worksheet
.
Cells
[
row
,
20
].
Value
.
ToString
();
;
}
if
(
worksheet
.
Cells
[
row
,
21
].
Value
!=
null
){
projectData
.
PHYSICALPROGGRESS
=
worksheet
.
Cells
[
row
,
21
].
Value
.
ToString
();
if
(
worksheet
.
Cells
[
row
,
21
].
Value
!=
null
)
{
projectData
.
PHYSICALPROGGRESS
=
worksheet
.
Cells
[
row
,
21
].
Value
.
ToString
();
}
if
(
worksheet
.
Cells
[
row
,
22
].
Value
!=
null
){
projectData
.
STATUS
=
worksheet
.
Cells
[
row
,
22
].
Value
.
ToString
();;
if
(
worksheet
.
Cells
[
row
,
22
].
Value
!=
null
)
{
projectData
.
STATUS
=
worksheet
.
Cells
[
row
,
22
].
Value
.
ToString
();
;
}
projectDataList
.
Add
(
projectData
);
}
}
}
}
catch
(
Exception
ex
)
{
//return "Some error occured while importing." + ex.Message
;
return
Ok
(
"Some error occured while importing."
+
ex
.
Message
)
;
}
return
Ok
(
projectDataList
);
...
...
@@ -158,92 +162,93 @@ namespace ExcelToJson.Controllers
//https://www.talkingdotnet.com/import-export-xlsx-asp-net-core/
//export a excel file
/*
[HttpGet]
[Route("Export")]
public string Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"demo.xlsx";
string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
if (file.Exists)
{
file.Delete();
file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
}
using (ExcelPackage package = new ExcelPackage(file))
{
// add a new worksheet to the empty workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
//First add the headers
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Gender";
worksheet.Cells[1, 4].Value = "Salary (in $)";
//Add values
worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["B2"].Value = "Jon";
worksheet.Cells["C2"].Value = "M";
worksheet.Cells["D2"].Value = 5000;
worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "Graham";
worksheet.Cells["C3"].Value = "M";
worksheet.Cells["D3"].Value = 10000;
worksheet.Cells["A4"].Value = 1002;
worksheet.Cells["B4"].Value = "Jenny";
worksheet.Cells["C4"].Value = "F";
worksheet.Cells["D4"].Value = 5000;
package.Save(); //Save the workbook.
}
return URL;
}
*/
[HttpGet]
[Route("Export")]
public string Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = @"demo.xlsx";
string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, sFileName);
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
if (file.Exists)
{
file.Delete();
file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
}
using (ExcelPackage package = new ExcelPackage(file))
{
// add a new worksheet to the empty workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
//First add the headers
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Gender";
worksheet.Cells[1, 4].Value = "Salary (in $)";
//Add values
worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["B2"].Value = "Jon";
worksheet.Cells["C2"].Value = "M";
worksheet.Cells["D2"].Value = 5000;
worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "Graham";
worksheet.Cells["C3"].Value = "M";
worksheet.Cells["D3"].Value = 10000;
worksheet.Cells["A4"].Value = 1002;
worksheet.Cells["B4"].Value = "Jenny";
worksheet.Cells["C4"].Value = "F";
worksheet.Cells["D4"].Value = 5000;
package.Save(); //Save the workbook.
}
return URL;
}*/
//read from a excel
/*
[HttpGet]
[Route("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++)
[HttpGet]
[Route("Import")]
public string Import()
{
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
[
HttpGet
(
"{id}"
)]
...
...
ExcelToJson/obj/Debug/netcoreapp2.1/ExcelToJson.assets.cache
View file @
fc7baa8f
No preview for this file type
ExcelToJson/obj/Debug/netcoreapp2.1/ExcelToJson.csproj.CoreCompileInputs.cache
View file @
fc7baa8f
91c3067690a97cdc4d11db1ac361192d56571460
400339070a838111c02fcd0e2f0e5e42cb5e9487
ExcelToJson/obj/Debug/netcoreapp2.1/ExcelToJson.csprojAssemblyReference.cache
View file @
fc7baa8f
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment