Skip to main content
Code Review

Return to Question

Notice removed Draw attention by Community Bot
Bounty Ended with Peter Csala's answer chosen by Community Bot
added 271 characters in body
Source Link
Peter Csala
  • 10.7k
  • 1
  • 16
  • 36

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSONJSON. Header is

Header

in the following format["Title 1 ","Title 2"]

[
 "Title 1 ",
 "Title 2"
]

or *reala real word example ["name","strength"] and Body is

[
 "name",
 "strength"
]

Body

in the following format[["Row A 1","Row A 2"],["Row B 1","Row B 2"]] *real

[
 [
 "Row A 1",
 "Row A 2"
 ],
 [
 "Row B 1",
 "Row B 2"
 ]
]

or a real world example "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";.

[
 [
 "lisinopril",
 "10 mg Tab"
 ],
 [
 "nitroglycerin",
 "0.4 mg Sublingual Tab"
 ],
 [
 "warfarin sodium",
 "3 mg Tab"
 ],
 [
 "metoprolol tartrate",
 "25 mg Tab"
 ]
]

I convert the two objects into list and loop them creating the excel file.

Code

Code

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSON. Header is the format["Title 1 ","Title 2"] or *real word example ["name","strength"] and Body is in the format[["Row A 1","Row A 2"],["Row B 1","Row B 2"]] *real example "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";. I convert the two objects into list and loop them creating the excel file.

Code

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSON.

Header

in the following format

[
 "Title 1 ",
 "Title 2"
]

or a real word example

[
 "name",
 "strength"
]

Body

in the following format

[
 [
 "Row A 1",
 "Row A 2"
 ],
 [
 "Row B 1",
 "Row B 2"
 ]
]

or a real world example

[
 [
 "lisinopril",
 "10 mg Tab"
 ],
 [
 "nitroglycerin",
 "0.4 mg Sublingual Tab"
 ],
 [
 "warfarin sodium",
 "3 mg Tab"
 ],
 [
 "metoprolol tartrate",
 "25 mg Tab"
 ]
]

I convert the two objects into list and loop them creating the excel file.

Code

added 182 characters in body
Source Link
Jefferson
  • 423
  • 5
  • 14
 public class ProgramGenerateDataTableExportExcel : IHttpHandler
 {
 public static void MainProcessRequest(HttpContext context)
 {
 // data coming from front end using httpContext.Current.Request
  //stringvar header = "[\"TitleHttpContext.Current.Request["header"];
 1 \",\"Title 2\"]";
 //var body = HttpContext.Current.Request["body"];
 //stringvar bodyfileName = "[[\"RowHttpContext.Current.Request["fileName"];
 A 1\",\"Row A 2\"],[\"Row B 1\",\"Row B 2\"]]";
 //Real data from backend system
 string header = "[\"name\",\"strength\"]";
 string body = "[[\"lisinopril\",\"10 mg Tab"]Tab\"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";Tab\"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 context.Response.Clear();
 context.Response.Buffer = true;
 context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
 context.Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
 context.Response.Charset = "";
 context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
 context.Response.BinaryWrite(excel.GetAsByteArray());
 context.ApplicationInstance.CompleteRequest();
 
 }
 }
 public class Program
 {
 public static void Main()
 {
 // data coming from front end //string header = "[\"Title 1 \",\"Title 2\"]";
 //string body = "[[\"Row A 1\",\"Row A 2\"],[\"Row B 1\",\"Row B 2\"]]";
 //Real data from backend system
 string header = "[\"name\",\"strength\"]";
 string body = "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 
 }
 }
 public class GenerateDataTableExportExcel : IHttpHandler
 {
 public void ProcessRequest(HttpContext context)
 {
 // data coming from front end using httpContext.Current.Request
  //var header = HttpContext.Current.Request["header"];
  //var body = HttpContext.Current.Request["body"];
 //var fileName = HttpContext.Current.Request["fileName"];
 
 //Real data from backend system
 string header = "[\"name\",\"strength\"]";
 string body = "[[\"lisinopril\",\"10 mg Tab\"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab\"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 context.Response.Clear();
 context.Response.Buffer = true;
 context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
 context.Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
 context.Response.Charset = "";
 context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
 context.Response.BinaryWrite(excel.GetAsByteArray());
 context.ApplicationInstance.CompleteRequest();
 
 }
 }
added 298 characters in body
Source Link
Jefferson
  • 423
  • 5
  • 14

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSON. Header is the format ["Title 1 ","Title 2"] or *real word example ["name","strength"] and Body is in the format [["Row A 1","Row A 2"],["Row B 1","Row B 2"]] *real example "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";. I convert the two objects into list and loop them creating the excel file.

 public class Program
 {
 public static void Main()
 {
 // data coming from front end
 //string header = "[\"Title 1 \",\"Title 2\"]";
 //string body = "[[\"Row A 1\",\"Row A 2\"],[\"Row B 1\",\"Row B 2\"]]";
 //Real data from backend system
 string header = "[\"name\",\"strength\"]";
 string body = "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 
 }
 }

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSON. Header is the format ["Title 1 ","Title 2"] and Body is in the format [["Row A 1","Row A 2"],["Row B 1","Row B 2"]] . I convert the two objects into list and loop them creating the excel file.

 public class Program
 {
 public static void Main()
 {
 // data coming from front end
 string header = "[\"Title 1 \",\"Title 2\"]";
 string body = "[[\"Row A 1\",\"Row A 2\"],[\"Row B 1\",\"Row B 2\"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 
 }
 }

I have two sets of data coming in from the front end. One is the table header and the other is the table body. the data is in the form of JSON. Header is the format ["Title 1 ","Title 2"] or *real word example ["name","strength"] and Body is in the format [["Row A 1","Row A 2"],["Row B 1","Row B 2"]] *real example "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";. I convert the two objects into list and loop them creating the excel file.

 public class Program
 {
 public static void Main()
 {
 // data coming from front end
 //string header = "[\"Title 1 \",\"Title 2\"]";
 //string body = "[[\"Row A 1\",\"Row A 2\"],[\"Row B 1\",\"Row B 2\"]]";
 //Real data from backend system
 string header = "[\"name\",\"strength\"]";
 string body = "[[\"lisinopril\",\"10 mg Tab"],[\"nitroglycerin\",\"0.4 mg Sublingual Tab\"],[\"warfarin sodium\",\"3 mg Tab\"][\"metoprolol tartrate\",\"25 mg Tab"]]";
 
 var headerObjects = JsonConvert.DeserializeObject<List<object>>(header);
 var bodyObjects = JsonConvert.DeserializeObject<List<object>>(body);
 
 var objectHeaderList = headerObjects.ToList();
 var objectsList = bodyObjects.ToList(); 
 
 ExcelPackage excel = new ExcelPackage();
 var workSheet = excel.Workbook.Worksheets.Add("Sheet1");
 
 int row = 1;
 int col = 1;
 foreach (var x in objectHeaderList)
 {
 var cell = workSheet.Cells[row, col];
 cell.Value = x;
 col++;
 }
 
 row = 2;
 col = 1;
 foreach (var x in objectsList)
 { 
 
 foreach (JValue y in (JToken)x)
 { 
 var cell = workSheet.Cells[row,col];
 cell.Value = y;
 col++;
 }
 row++;
 }
 
 }
 }
deleted 4 characters in body
Source Link
Jefferson
  • 423
  • 5
  • 14
Loading
added 197 characters in body
Source Link
Jefferson
  • 423
  • 5
  • 14
Loading
added 1155 characters in body
Source Link
Jefferson
  • 423
  • 5
  • 14
Loading
Notice added Draw attention by Jefferson
Bounty Started worth 50 reputation by Jefferson
deleted 896 characters in body; edited tags; edited title
Source Link
Jefferson
  • 423
  • 5
  • 14
Loading
edited title
Link
toolic
  • 15.2k
  • 5
  • 29
  • 213
Loading
edited body; edited title
Source Link
toolic
  • 15.2k
  • 5
  • 29
  • 213
Loading
Source Link
Jefferson
  • 423
  • 5
  • 14
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /