在 C# 中将数据导出到 excel 时,索引超出了数组的范围
Index was outside the bounds of the array while exporting data to excel in C#
我有 ASP.Net
个网站。以下是我将流写入 excel.
的方法
public void JsonToExcel(string jsonData, HttpResponseBase response)
{
try
{
ExcelPackage excel = new ExcelPackage();
var worksheet = excel.Workbook.Worksheets.Add("Sheet1");
//below line is throwing the error
worksheet.Cells[1, 1].LoadFromCollection(jsonData, true);
using (MemoryStream swObj = new MemoryStream())
{
string fileName = DateTime.Now.ToLongDateString() + ".xlsx";
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("content-disposition", "attachment; filename=" + fileName + "");
excel.SaveAs(swObj);
swObj.WriteTo(response.OutputStream);
return;
}
}
catch (Exception ex)
{
//handle exception
return;
}
finally
{
response.Flush();
response.End();
}
在函数的这一行
-worksheet.Cells[1, 1].LoadFromCollection(jsonData, true);
我收到以下异常
Index was outside the bounds of the array
我尝试设置为 worksheet.Cells[0,0]
我正在使用 EPPlus 4.1.0 软件包。
JSON 样本:-
string jsonData = @"jsonData = [
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:06:25.083"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:12:02.497"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:13:48.013"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
}]";
我认为 JSON 和 EPPlus 方法 LoadFromCollection 内部有问题 LoadFromCollection 方法需要一个集合,而不是任意字符串或其他东西,所以当我反序列化它时到适当的 class 和 Newtonsoft.Json 似乎没问题。
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using OfficeOpenXml;
namespace TestC
{
public class PostData
{
public string DocumentName { get; set; }
public DateTime ActionDate { get; set; }
public string ActionType { get; set; }
public string ActionPerformedBy { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (ExcelPackage package = new ExcelPackage()) {
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
string jsonData = @"[
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:06:25.083"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:12:02.497"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:13:48.013"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
}]";
List<PostData> dataForExcel = JsonConvert.DeserializeObject<List<PostData>>(jsonData);
worksheet.Cells[1, 1].LoadFromCollection(dataForExcel, true);
package.SaveAs(File.Create(@"C:\Users\User\Documents\sample.xlsx"));
}
}
}
}
结果:
要在 Excel 中正确输出日期,您应该为第二列 (ActionDate) 中从第二行到最后的单元格应用正确的数字格式:
worksheet.Cells[2, 2, worksheet.Dimension.End.Row, 2].Style.Numberformat.Format = "yyyy-mm-ddThh:mm:ss.000";
我有 ASP.Net
个网站。以下是我将流写入 excel.
public void JsonToExcel(string jsonData, HttpResponseBase response)
{
try
{
ExcelPackage excel = new ExcelPackage();
var worksheet = excel.Workbook.Worksheets.Add("Sheet1");
//below line is throwing the error
worksheet.Cells[1, 1].LoadFromCollection(jsonData, true);
using (MemoryStream swObj = new MemoryStream())
{
string fileName = DateTime.Now.ToLongDateString() + ".xlsx";
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("content-disposition", "attachment; filename=" + fileName + "");
excel.SaveAs(swObj);
swObj.WriteTo(response.OutputStream);
return;
}
}
catch (Exception ex)
{
//handle exception
return;
}
finally
{
response.Flush();
response.End();
}
在函数的这一行
-worksheet.Cells[1, 1].LoadFromCollection(jsonData, true);
我收到以下异常
Index was outside the bounds of the array
我尝试设置为 worksheet.Cells[0,0]
我正在使用 EPPlus 4.1.0 软件包。
JSON 样本:-
string jsonData = @"jsonData = [
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:06:25.083"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:12:02.497"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:13:48.013"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
}]";
我认为 JSON 和 EPPlus 方法 LoadFromCollection 内部有问题 LoadFromCollection 方法需要一个集合,而不是任意字符串或其他东西,所以当我反序列化它时到适当的 class 和 Newtonsoft.Json 似乎没问题。
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using OfficeOpenXml;
namespace TestC
{
public class PostData
{
public string DocumentName { get; set; }
public DateTime ActionDate { get; set; }
public string ActionType { get; set; }
public string ActionPerformedBy { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (ExcelPackage package = new ExcelPackage()) {
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
string jsonData = @"[
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:06:25.083"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:12:02.497"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
},
{
""DocumentName"": ""Test Document"",
""ActionDate"": ""2015-09-25T16:13:48.013"",
""ActionType"": ""View"",
""ActionPerformedBy"": ""Sreeja SJ""
}]";
List<PostData> dataForExcel = JsonConvert.DeserializeObject<List<PostData>>(jsonData);
worksheet.Cells[1, 1].LoadFromCollection(dataForExcel, true);
package.SaveAs(File.Create(@"C:\Users\User\Documents\sample.xlsx"));
}
}
}
}
结果:
要在 Excel 中正确输出日期,您应该为第二列 (ActionDate) 中从第二行到最后的单元格应用正确的数字格式:
worksheet.Cells[2, 2, worksheet.Dimension.End.Row, 2].Style.Numberformat.Format = "yyyy-mm-ddThh:mm:ss.000";