Quantcast
Channel: EPPlus Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 2262

Created Unassigned: "Object Reference Not Set to an Instance of an Object" when saving the package [15286]

$
0
0
I am trying to create a workbook and send it to the response stream. Any time I call a 'save' method, I get the error "Object Reference Not Set to an Instance of an Object".

Below is the code I'm working with

```
protected void ExportToExcel_Click(object sender, EventArgs e)
{
//There are 30-40 ranks
string[] ranks = {"rank 1", "rank 2", "rank 3"};


//Get SharePoint Site, Web, and List
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;

SPList list = web.Lists["myList"];
SPQuery query = new SPQuery();

//There are additional sensitive fields included, but omitted for the example
query.ViewFields = "<FieldRef Name='MINIMUM_RANK'/><FieldRef Name='MAXIMUM_RANK'/>";

SPListItemCollection collection = list.GetItems(query);

System.Data.DataTable dt = collection.GetDataTable();

dt.Columns.RemoveAt(dt.Columns.Count - 1);

using (ExcelPackage package = new ExcelPackage())
{
//Loop through all the ranks
for (int i = 0; i < ranks.Length; i++)
{
//Create a datatable specifically for the filter
System.Data.DataTable tempTable = dt.Clone();

//Begin looping through the dataset
foreach (DataRow dr in dt.Rows)
{
string minRank = (string)dr["MINIMUM_RANK"];
string maxRank = (string)dr["MAXIMUM_RANK"];

//If the datarow's rank falls within minRank and maxRank,
if (Array.IndexOf(ranks, ranks[i]) >= Array.IndexOf(ranks, minRank) && Array.IndexOf(ranks, ranks[i]) <= Array.IndexOf(ranks, maxRank))
{
//import the row into the temp table
tempTable.ImportRow(dr);
}
}

if (tempTable.Rows.Count > 0)
{
//If we have datas, create a new worksheet and name it the value of rank, and dump the data onto that worksheet.
ExcelWorksheet ws = package.Workbook.Worksheets.Add(ranks[i]);
ws.Name = ranks[i];
ws.Cells["A1"].LoadFromDataTable(tempTable, true);
}
}

//Send the workbook for download
this.Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
this.Context.Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
this.Context.Response.BinaryWrite(package.GetAsByteArray()); //Error occurs here.
this.Context.Response.End();
}
}
```


I have also tried package.SaveAs(ms) where ms is a new memorystream.

BUT

Sometimes it will move past this point (I think after playing with it). I get prompted for download, click Open/Save, and IE tells me it couldn't download the file.

Any ideas on this? The project is time-sensitive. If I can't get it figured out, I'll have to resort to a different plugin unfortunately. :( Thanks in advance!

Viewing all articles
Browse latest Browse all 2262

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>