====
將 json 字串轉為物件
var list = JsonConvert.DeserializeObject<IEnumerable<Newtonsoft.Json.Linq.JObject>>(jsonlist);
foreach (var obj in list)
foreach (var p in obj.Properties())
obj.
obj.Value<dynamic>(p.Name); //不轉換型態
javascript
====
將物件轉為 json 字串 : JSON.stringify(jsonlist)
將 json 字串轉為物件 : JSON.parse(jsonstring)
範例 : 把前端物件陣列匯出成excel
c#
====
public string JsonExcel(string jsonlist, string 檔名)
{
var list = JsonConvert.DeserializeObject
using (var ep = new OfficeOpenXml.ExcelPackage())
{
int row = 1, col = 1;
var ws = ep.Workbook.Worksheets.Add("工作表1");
if (list.Count()>0)
foreach (var p in list.FirstOrDefault().Properties())
ws.Cells[row, col++].Value = p.Name;
foreach (var obj in list)
{
row++;
col = 1;
foreach (var p in obj.Properties())
{
ws.Cells[row, col++].Value = obj.Value
if (obj.Value
ws.Cells[row, col-1].Style.Numberformat.Format = "yyyy/MM/dd";
}
}
var fi = new System.IO.FileInfo(Server.MapPath("~") + "\\download\\{0}.xlsx".FormatString(檔名));
ep.SaveAs(fi);
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
return baseUrl + "download/{0}.xlsx".FormatString(檔名);
}
}
javascript
====
$http.post('@Url.Action("JsonExcel")', { jsonlist: JSON.stringify($scope.gridOptions.rowData), 檔名: "總分類帳明細" })
.then(function (result) {
window.location = result.data;
});
沒有留言:
張貼留言