I am exporting excel file from database using following method.But i have one problem when i am exporting excel file than it is automatically downloading to download folder and i don't want this to be happen,I want my excel file to be downloaded in my project folder
var formsection = from fs in db.FormSections
join form in Form on fs.FormId equals form.FormId
select fs;
XLWorkbook wb = new XLWorkbook();
string sheetName = "ARTICLE"; //Give name for export file.
var Fs = wb.Worksheets.Add("FORMSECTION");
Fs.Cell(2, 1).InsertTable(formsection.ToList());// assign list here.
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Response.AddHeader("content-disposition", String.Format(#"attachment;filename={0}.xlsx", sheetName.Replace(" ", "_")));
var filePath = Path.Combine(Server.MapPath("~/Content"));
using (MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
memoryStream.WriteTo(HttpContext.Response.OutputStream);
memoryStream.Close();
}
HttpContext.Response.End();
Here is an example that converts a datatable to a .csv file and save the file to the folder of your project for me its store the file in Domestic folder as the path given by me.
private void test(DataTable dt1) {
string csv = string.Empty;
foreach (DataColumn column in dt1.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt1.Rows)
{
foreach (DataColumn column in dt1.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") +',';
}
//Add new line.
csv += "\r\n";
}
string datetime = Convert.ToString(DateTime.Today.ToString("dd-MM-yyyy")).Trim();
string filepath = "C:\\Users\\Prateek\\Desktop\\MMR New 27-07 -\\Domestic\\";
string filename= #"BILLING_BOOK_NO" + "_4005" + "_"+datetime+".CSV";
string combinepath = filepath + filename;
System.IO.File.WriteAllText(combinepath,csv);
}`
You need to write the excel to your server first.
wb.SaveAs(filePath);
//encrypt the file
Encrypt(filePath);
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
memoryStream.Write(bytes, 0, (int)file.Length);
memoryStream.WriteTo(HttpContext.Response.OutputStream);
}
}
use code as below
public void ImportXLX()
{
string filePath = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), #"C:\Users\Vipin\Desktop\Sheets\MyXL6.xlsx");
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
Request.Files["xlsFile"].SaveAs(filePath);
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
}
Related
I'm trying to implement this application in ASP.NET MVC 5
Export And Import Excel Data Using NPOI Library In .NET Core 2 Razor Pages
specifically, I'm trying this part which is to make the excel file downloadable one
So here my code for creating and downloading file
public FileResult OnPostExporttoExcel()
{
string webRootPath = _hostingEnvironment.WebRootPath;
string fileName = #"Testingdummy.xlsx";
string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, fileName);
FileInfo file = new FileInfo(Path.Combine(webRootPath, fileName));
var memoryStream = new MemoryStream();
// --- Below code would create excel file with dummy data----
using (var fs = new FileStream(Path.Combine(webRootPath, fileName), FileMode.Create, FileAccess.Write)) {
IWorkbook workbook = new XSSFWorkbook();
ISheet excelSheet = workbook.CreateSheet("Testingdummy");
IRow row = excelSheet.CreateRow(0);
row.CreateCell(0).SetCellValue("CustomerId");
row.CreateCell(1).SetCellValue("FirstName");
row.CreateCell(2).SetCellValue("LastName");
row.CreateCell(3).SetCellValue("Email");
cust = from s in _context.Customers select s;
int counter = 1;
foreach (var customer in cust) {
string FirstName = string.Empty;
if (customer.FirstName.Length > 100)
FirstName = customer.FirstName.Substring(0, 100);
else
FirstName = customer.FirstName;
row = excelSheet.CreateRow(counter);
row.CreateCell(0).SetCellValue(customer.CustomerId);
row.CreateCell(1).SetCellValue(FirstName);
row.CreateCell(2).SetCellValue(customer.LastName);
row.CreateCell(3).SetCellValue(customer.Email);
counter++;
}
workbook.Write(fs);
}
using (var fileStream = new FileStream(Path.Combine(webRootPath, fileName), FileMode.Open)) {
fileStream.CopyTo(memoryStream);
}
memoryStream.Position = 0;
return File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
}
but I don't know how to get _hostingEnvironment.WebRootPath; in asp.net MVC 5
I am trying to append to an existing worksheet using the code below but it overwrites the existing data instead of appending.
string filePath = "c:\\CompareReport.xlsx";
FileInfo fi = new FileInfo(filePath);
using (var package = new ExcelPackage(fi)) {
var currentSheet = package.Workbook.Worksheets;
var sheet = currentSheet.First();
sheet.Row(sheet.Dimension.End.Row);
sheet.Cells.LoadFromCollection(myData, true);
package.SaveAs(fi);
}
This is the old code where copy to a local path, but now i need to save the file on SharePoint. How can i use stream with it and write the stream to file.
File.Copy(oTemplatePath, destinationPath, true);
using (WordprocessingDocument document = WordprocessingDocument.Open(destinationPath, true))
{
document.GetMergeFields("reference_number").ReplaceWithText(refrenceNumber);
document.MainDocumentPart.Document.Save();
for (int i = 0; i < newDoc.jsonFields.Count; i++)
{
if (newDoc.jsonFields[i].type == "date")
{
document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(DateTime.Parse(newDoc.jsonFields[i].data).ToShortDateString());
document.MainDocumentPart.Document.Save();
}
else
{
document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data);
document.MainDocumentPart.Document.Save();
}
}
//document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data);
//document.MainDocumentPart.Document.Save();
}
You can save to local temp folder
string tempDocx = Path.Combine(Path.GetTempPath(), fileName);
logger.Trace($"Creating Word document {tempDocx}");
File.Delete(tempDocx);
File.Copy("Template.docx", tempDocx);
Then upload to SharePoint, here using a FileStream
using (IO.FileStream fs = new IO.FileStream(tempDocx, IO.FileMode.Open))
{
List documentsList = clientContext.Web.Lists.GetByTitle(libTitle);
clientContext.Load(documentsList.RootFolder);
clientContext.ExecuteQuery();
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.ContentStream = fs;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = documentsList.RootFolder.ServerRelativeUrl + "/" + IO.Path.GetFileName(tempDocx);
uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
}
I am trying to add new worksheet into existing workbook, code runs fine without any error. But changes are not being updated to the excel file.
Here is my code
string path = "C:\\TestFileSave\\ABC.xlsx";
FileInfo filePath = new FileInfo(path);
if (File.Exists(path))
{
using(ExcelPackage p = new ExcelPackage())
{
using(stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
p.Load(stream);
ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
ws.Cells[1, 1].Value = wsName;
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
ws.Cells[1, 1].Style.Font.Bold = true;
p.Save();
}
}
}
The stream object is not tied to the package. The only relationship is it copies its bytes in your call to Load afterwards they are separate.
You do not need to even use a stream - better to let the package handle it on its own like this:
var fileinfo = new FileInfo(path);
if (fileinfo.Exists)
{
using (ExcelPackage p = new ExcelPackage(fileinfo))
{
//using (stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
//p.Load(stream);
ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
ws.Cells[1, 1].Value = wsName;
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
ws.Cells[1, 1].Style.Font.Bold = true;
p.Save();
}
}
}
Here I have shown to write data into exiting excel file by creating a new sheet in same file. To answer your question: try using last two lines File.WriteAllBytes instead of p.Save().
string strfilepath = "C:\\Users\\m\\Desktop\\Employeedata.xlsx";
using (ExcelPackage p = new ExcelPackage())
{
using (FileStream stream = new FileStream(strfilepath, FileMode.Open))
{
p.Load(stream);
//deleting worksheet if already present in excel file
var wk = p.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Hola");
if (wk != null) { p.Workbook.Worksheets.Delete(wk); }
p.Workbook.Worksheets.Add("Hola");
p.Workbook.Worksheets.MoveToEnd("Hola");
ExcelWorksheet worksheet = p.Workbook.Worksheets[p.Workbook.Worksheets.Count];
worksheet.InsertRow(5, 2);
worksheet.Cells["A9"].LoadFromDataTable(dt1, true);
// Inserting values in the 5th row
worksheet.Cells["A5"].Value = "12010";
worksheet.Cells["B5"].Value = "Drill";
worksheet.Cells["C5"].Value = 20;
worksheet.Cells["D5"].Value = 8;
// Inserting values in the 6th row
worksheet.Cells["A6"].Value = "12011";
worksheet.Cells["B6"].Value = "Crowbar";
worksheet.Cells["C6"].Value = 7;
worksheet.Cells["D6"].Value = 23.48;
}
//p.Save() ;
Byte[] bin = p.GetAsByteArray();
File.WriteAllBytes(#"C:\Users\m\Desktop\Employeedata.xlsx", bin);
}
I originally got the error code "A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT)) " from using this, but later learned that it was because the existing Excel file that I wanted to modify wasn't fully MS-Excel format compliant. I created thee original excel file in Open office as an .xls file, but EPPlus was not able to read it. When I regenerated this original excel file in Online Excel, everything worked fine.
I am using EPPlus to open a spreadsheet and then populate it with pictures and information.
When i try to delete a folder containing all the pictures i used to populate my spreadsheet i get the error that this file is in use with another application. What would be the correct way to release the objects used and close the spreadsheet?
using (var package = new ExcelPackage(existingFile))
{
ExcelWorkbook workBook = package.Workbook;
if (workBook != null)
{
if (workBook.Worksheets.Count > 0)
{
int i = 0;
foreach(ExcelWorksheet worksheet in workBook.Worksheets)
{
xlWorkSeet1[i] = worksheet;
i = i + 1;
}
}
}
//More code ...
FileStream aFile = new FileStream(tempFolderPathAlt + saveas + ".xls", FileMode.Create);
byte[] byData = package.GetAsByteArray();
aFile.Seek(0, SeekOrigin.Begin);
aFile.Write(byData, 0, byData.Length);
aFile.Close();
xlWorkSeet1 = null;
workBook = null;
}//End using
String P = Path.Combine(tempFolderPathAlt, "ExtractedFiles");
bool directoryExists = Directory.Exists(P);
if (directoryExists)
Directory.Delete(P, true); // deletes sub-directories
The error i get is when it is trying to delete a photo i added to my spreadsheet.
Try out following and please let me know whether this helps
int writeTimeout = 200;
using (var aFile = new FileStream(tempFolderPathAlt + saveas + ".xls", FileMode.Create))
{
aFile.WriteTimeout = writeTimeout;
byte[] byData = package.GetAsByteArray();
aFile.Seek(0, SeekOrigin.Begin);
aFile.Write(byData, 0, byData.Length);
xlWorkSeet1 = null;
workBook = null;
Thread.Sleep(writeTimeout);
}