Opening a client-side Excel file using EPPlus - c#

I am the middle of writing a program which has to create an excel file on a client machine and then, after they have saved and updated it then I need to import it again and apply the changes to my database. The export bit of it works fine, however when I import from the web application I get zero worksheets in my workbook. If I run it on my development machine, i.e. the website is running on the same machine as the file is located then it works fine.
My code is as follows:
try
{
Productsfile = new FileInfo(PathandFileName);
}
catch (Exception ex)
{
lblStatus.Text = string.Format("Error getting file info {0}", ex.Message);
return;
}
lblStatus.Text += "File Opened succesfully " + Productsfile.FullName + Environment.NewLine;
//Open and read file
try
{
using (ExcelPackage datafile = new ExcelPackage(Productsfile))
{
ExcelWorkbook wkb = datafile.Workbook;
if (wkb != null)
{
lblStatus.Text += string.Format("The workbook is not null and there are {0} worksheets", wkb.Worksheets.Count) + Environment.NewLine;
I have confirmed that the code gets to the last label update, which tells me there are zero worksheets in the workbook, but importantly that the workbook itself is not null.
When we created the export the following code was used, which transfers the data stream from server to client - I am wondering if something to reverse it is needed:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Products.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();

Right, I believe I've found the answer:
1) Get the file details from a fileupload control
2) Convert this to a memory stream
3) Create the package using the memory stream
And the code is as follows:
if (FileUpload1.HasFile)
{
HttpPostedFile file = Request.Files[0];
myLabel.Text = file.FileName;
MemoryStream mem = new MemoryStream();
mem.SetLength((int)file.ContentLength);
file.InputStream.Read(mem.GetBuffer(), 0, (int)file.ContentLength);
ExcelPackage package = new ExcelPackage(mem);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
myLabel.Text += " " + worksheet.Name;
}

Related

How to delete exist Excel file from uploaded folder?

How to close excel file or delete from folder. I tried a lot but its not getting file there.so always throwing error : The process cannot access the file because it is being used by another process.How to solve it?
first time not throwing any error .going successfully uploaded but when next time with same file i am trying to upload then imideatly throwing an error before call upload method
creating excel
System.Data.DataTable dtexcel = new System.Data.DataTable();
dtexcel = BindComboWithParm("Get_Cols_Forexcelsheet");
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dtexcel, "Customer");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Customer_Creation.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
checking for file
string FileName = "Customer_Creation";
string Paths = Server.MapPath("~/Uploads/") + FileName;
FileInfo file = new FileInfo(Paths);
if (file.Exists)
{
file.Delete();
}
upload event click
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string FileName = "Customer_Creation";
string Paths = Server.MapPath("~/Uploads/") + FileName;
FileInfo file = new FileInfo(Paths);
if (file.Exists)
{
file.Delete();
}
if (FileUpload1.HasFile)
{
string excelPath = Server.MapPath("~/Uploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
ImporttoSQL(excelPath);
}
else
{
ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "ClientScript", "alert('Please select Excelsheet')", true);
return;
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
}
finally
{
ViewState["ExcelUploaded"] = "false";
}
}
I believe you just want to create a file, download it and then delete it once it has downloaded.
1. Create a custom FileHttpResponseMessage.
public class FileHttpResponseMessage : HttpResponseMessage
{
private readonly string filePath;
public FileHttpResponseMessage(string filePath)
{
this.filePath = filePath;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Content.Dispose();
if(File.Exist(filePath))
File.Delete(filePath);
}
}
2. Create a function which will return generated file path. and use that path in below code :
public HttpResponseMessage Get()
{
var filePath = GetNewFilePath();//your function which will create new file.
var response = new FileHttpResponseMessage(filePath);
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName ="YourCustomFileName"
};
return response;
}
3. Above code will delete file automatically once file will be served to user.
Right now it's difficult to say what is wrong. Most likely your file is still in use by some part of your program. Pleases check this link it contains useful information about how to debug it.
Your app has uploaded a file to a server. For this purpose it used managed resources (like FileStream etc). For some reason this file remains opened. Later your app tries to delete it when it's still in use. And you get this "File in use" exception.
What i'd recommend to do is try to delete this file directly and if this works then the problem is hidden somewhere in your 'upload part' of your code. If its not then problem is most likely lay with some external processes that uses this file.

Allow Client to Download Excel File

I'm using EPPLUS to generate an Excel File. I have code to save it to the server but I can't figure out how to allow the client to download the Excel sheet.The user clicks the button and it downloads to where ever they have it set in their browser. I found the client download code here that I can't get to work...It's not throwing an error in Firebug or Fiddler
JQuery
$('#ExcelExport').on('click', function () {
$.post("/Reports/ExportToExcel", function (data) {
alert("The Export was Succussful");
})
.fail(function () {
alert("The Export has Failed");
});
C#
public ActionResult ExportToExcel()
{
using (ExcelPackage package = new ExcelPackage())
{
// Get Data
var excelData = _repository.ExcelData();
//Basic Info
package.Workbook.Properties.Author = "MEO System";
package.Workbook.Properties.Title = "PendingMEOs";
//Excel worksheet
package.Workbook.Worksheets.Add("Pending MEOs");
ExcelWorksheet ws = package.Workbook.Worksheets[1]; // 1 is the position of the worksheet
ws.Name = "Pending MEOs";
//adds the data to the excel sheet and formats the data
ws.Cells.LoadFromCollection(excelData, true, OfficeOpenXml.Table.TableStyles.Medium6);
//sets the cell width to fit the contents
ws.Cells["A1:L" + excelData.Count()].AutoFitColumns();
// Save the Excel file
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=PendingMEOS.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
}
return Json(new { success = true });
}
Save Code to the server, works but not what I want
string path = "C:\\PendingMEOS.xlsx";
Byte[] bin = package.GetAsByteArray();
System.IO.File.WriteAllBytes(path, bin);
The problem is you are writing directly to the Response stream but then return a Json result.
Take a look at MVC Controller Using Response Stream

Error dialog displayed when opening an excel file generated with EPPlus

I am creating an Excel file using the EPPlus library. When I create file and open up the file, the following pop up message shows:
We found a problem with some content in 'ExcelDemo.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, Click Yes
I am using following code
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells[1, 2].Value = "Excel Download";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
}
Is there problem in my code or is this an Excel issue?
In my case the problem was in calling
package.Save();
and using
Response.BinaryWrite(package.GetAsByteArray());
at the same time.
When you call package.GetAsByteArray() it perfoms following operations internally:
this.Workbook.Save();
this._package.Close();
this._package.Save(this._stream);
So, calling package.Save two times leads to this error when opening in Excel.
At the start, you need to add in a:
Response.Clear();
Then at the end add a
Response.End();
I will share my solution. I am using a template excel file and then create new excel from it.
Receiving the same error. My code was
using (Stream newFileStream = File.Open(this.tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
using (Stream originalFile = File.Open(this.initialFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (ExcelPackage excelPackage = new ExcelPackage(newFile, template))
{
// ... Do work here
}
I had to change code to:
FileInfo intialInfo = new FileInfo(this.initialFilePath);
FileInfo tempFileInfo = new FileInfo(this.tempFilePath);
using (ExcelPackage excelPackage = new ExcelPackage(tempFileInfo, intialInfo))
{
//... Do work here
}
Also I am using ASP MVC and the response is:
byte[] result = exporter.GetBytesFromGeneratedExcel();
return this.File(result, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Test.xlsx");
In my case, problem was data table name which I did not set earlier.
Try this one,
dt.TableName = "ExcelSheet1";
I was having this problem because I was writing a string larger than 32,767 characters into a single cell. Excel doesn't like this, but EPPlus won't stop you from doing it.
My code was updated... and was getting the same error.... and I finally found my solution
Original Code:
public static void ExportToExcel(HttpContext ctx, DataTable tbl, string fileName)
{
try
{
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(fileName);
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(tbl, true);
int rowCount = tbl.Rows.Count;
List<int> dateColumns = new List<int>();
foreach (DataColumn d in tbl.Columns)
{
if (d.DataType == typeof(DateTime))
dateColumns.Add(d.Ordinal + 1);
}
CultureInfo info = new CultureInfo(ctx.Session["Language"].ToString());
foreach (int dc in dateColumns)
ws.Cells[2, dc, rowCount + 1, dc].Style.Numberformat.Format = info.DateTimeFormat.ShortDatePattern;
//Write it back to the client
ctx.Response.Clear();
ctx.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx");
ctx.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
ctx.Response.Buffer = false;
ctx.Response.BufferOutput = false;
ctx.Response.BinaryWrite(pck.GetAsByteArray());
ctx.Response.End();
}
}
catch (Exception EX)
{
ctx.Response.Write(EX.ToString());
}
}
Code update:
catch (Exception EX)
{
if (!(EX is System.Threading.ThreadAbortException))
{
ctx.Response.Write(EX.ToString());
}
}
IT WORKED!
A solution I came up with was just returning the file object in the controller. The first argument should be the byte array (file), the second argument is the content type, and the last argument is the filename (in my case "report.xlsx").
return File(file, "application/octet-stream", fileName);
Hope this helped.

stream to excel without saving

I am creating excel and filling values into it. i am using EPPlus.
I can save it using filestream and open it.
but i want to open it without saving.
I thought we can use memorystream in some way to generate the excel directly.
Please guide.
try
{
MemoryStream newFile = new MemoryStream();
using (ExcelPackage package = new ExcelPackage(newFile))
{
// Add Data Collection worksheet
ExcelWorksheet dataWorksheet = package.Workbook.Worksheets.Add("Sheet1");
dataWorksheet.Cells[1, 1].Value = " My Text";
dataWorksheet.Cells[1, 1].Style.Font.Size = 14;
dataWorksheet.Cells[3, 1].Value = BALGlobalVariables.cocName;
dataWorksheet.Cells[5, 1].Value = "IR From Date :";
dataWorksheet.Cells[6, 1].Value = "IR To Date : ";
dataWorksheet.Cells[5, 6].Value = "From Date :";
dataWorksheet.Cells[6, 6].Value = "To Date : ";
dataWorksheet.Cells[5, 2].Value = fromDate;
dataWorksheet.Cells[6, 2].Value = toDate;
dataWorksheet.Cells[5, 7].Value = invFromDate;
dataWorksheet.Cells[6, 7].Value = invFromDate;
// Template specific excel generation goes in here
FillPurchaseExcelData(ref dataWorksheet, masterTable, subTable);
// save package
package.Save();
}
byte[] fileContent = newFile.ToArray();
#if DEBUG
string tempName = "MTemp.xlsx";
string tempFileName = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + #"\" + tempName;
//Write the stream data of workbook to the root directory
using (FileStream file = new FileStream(tempFileName, FileMode.Create))
{
file.Write(fileContent, 0, fileContent.Length);
}
System.Diagnostics.Process.Start(tempFileName);
You can generate excel file and choose not to save it, but Excel can't open files that are not saved on disk (it becomes a 'file' when it's on disk), so in short - you have to save document in order to open it in Excel.
If you're concerned about storing data on hard drive, as an alternative you could create a data source on a medium you trust, and then create an xslx that would instruct Excel to consume that data source when opening worksheet (instead of creating a file pre-filled with data). But this is a whole other story...

Download EXCEL file from ASP.NET page without Generating physical file on server (On The Fly)

I am trying to export C# DataTable to EXCEL file on the fly (without creating physical file) using Microsoft Office EXCEL INTEROP and download it through asp.net webpage through Response object. I am able to generate memorystream using the library But somehow the file is not getting saved through the memory stream. Reference code is given below. You will be required to take reference for DocumentFormat.OpenXml.dll & WindowsBase.DLL (that you can download from microsoft site).
Any Idea how to resolve the problem ? ? ?
Private void DownloadFile()
{
DataSet objTable = ReadTableFromViewstate();
if (objTable != null && objTable.Rows.Count > 0)
{
string strDownloadableFilename = "TestExcelFileName.xls";
MemoryStream fs1 = new MemoryStream();
if (CreateExcelFile.CreateExcelDocument(objTable, fs1))
{
Response.Clear();
byte[] data1 = new byte[fs1.Length];
fs1.Read(data1, 0, data1.Length);
fs1.Close();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", strDownloadableFilename));
Response.BinaryWrite(data1); ;
Response.End();
}
else
{
LblErrorMessage.Text = "Error Exporting File.";
}
}
}
..
public static bool CreateExcelDocument(DataSet ds, System.IO.Stream excelFileStream)
{
try
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(excelFileStream, SpreadsheetDocumentType.Workbook))
{
CreateParts(ds, document);
}
Trace.WriteLine("Successfully created: " + excelFileStream);
return true;
}
catch (Exception ex)
{
Trace.WriteLine("Failed, exception thrown: " + ex.Message);
return false;
}
}
..
private static void CreateParts(DataSet ds, SpreadsheetDocument document)
{
WorkbookPart workbookPart = document.AddWorkbookPart();
Workbook workbook = new Workbook();
workbookPart.Workbook = workbook;
// If we don't add a "WorkbookStylesPart", OLEDB will refuse to connect to this .xlsx file !
WorkbookStylesPart workbookStylesPart = workbookPart.AddNewPart<WorkbookStylesPart>("rIdStyles");
Stylesheet stylesheet = new Stylesheet();
workbookStylesPart.Stylesheet = stylesheet;
Sheets sheets = new Sheets();
// Loop through each of the DataTables in our DataSet, and create a new Excel Worksheet for each.
uint worksheetNumber = 1;
foreach (DataTable dt in ds.Tables)
{
// For each worksheet you want to create
string workSheetID = "rId" + worksheetNumber.ToString();
string worksheetName = dt.TableName;
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(workSheetID);
WriteDataTableToExcelWorksheet(dt, worksheetPart);
Sheet sheet = new Sheet() { Name = worksheetName, SheetId = (UInt32Value)worksheetNumber, Id = workSheetID };
sheets.Append(sheet);
worksheetNumber++;
}
workbook.Append(sheets);
}
Thanks to All, Finally I Got solution of my problem. I just replaced following line of code:
byte[] data1 = new byte[fs1.Length];
fs1.Read(data1, 0, data1.Length);
fs1.Close();
with this line
byte[] data1 = fs1.ToArray();
and my problem got resolved.
After finishing writing to the memory stream, you need to set back the pointer to its origin before reading it.
fs1.Seek(0, SeekOrigin.Begin);

Categories