Save an Excel file into Local Directory using c# - c#

I am creating an Excel using EPPlus and Datatable.Now as per my requirement i have to save this into my local system directory but i am not able to get how to achieve this ..
The path where i have to save is C://Reports//excel.xls
Here is my code
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(reportName);
ws.Cells["A1"].LoadFromDataTable(DT, true);
pck.Save();
}
Please help me ..

FileInfo newFile = new FileInfo(#"C:\Reports\excel.xls");
if (newFile.Exists)
{
newFile.Delete(); // ensures we create a new workbook
newFile = new FileInfo(#"C:\Reports\excel.xls");
}
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(reportName);
ws.Cells["A1"].LoadFromDataTable(DT, true);
pck.Save();
}

Related

apply macro xslm to new file its creating new sheets and applying to other sheet instead of required sheet in c#Xml ( (DocumentFormat.Open))

code used for copy macro from another sheet to a new sheet
string oldfile = #"C:\Users\Desktop\Old.xlsm";
string finalfile = #"C:\Users\temp\new.xlsm";
List<VbaProjectPart> newParts = new List<VbaProjectPart>();
using (var originalDocument = SpreadsheetDocument.Open(oldfile, false))
{
newParts = originalDocument.WorkbookPart.GetPartsOfType<VbaProjectPart>().ToList();
byte[] byteArray = File.ReadAllBytes(finalfile);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (var document = SpreadsheetDocument.Open(finalfile, true))
{
//document.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.MacroEnabledWorkbook);
var vba = document.WorkbookPart.VbaProjectPart;
document.WorkbookPart.DeletePart(vba);
foreach (var part in newParts)
{
VbaProjectPart vbaProjectPart = document.WorkbookPart.AddNewPart<VbaProjectPart>();
using (Stream data = part.GetStream())
{
vbaProjectPart.FeedData(data);
}
}
document.WorkbookPart.Workbook.WorkbookProperties.CodeName = "ThisWorkbook";
//File.WriteAllBytes(finalfile, stream.ToArray());
}
}
}
while doing this macro applying to other sheet but the macro should
applied to sheet7 please help me why its creating new sheets and
how to apply macro to required sheet After Applying macro its
adding new sheets and macro code copied to sheet3
Expected macro should be in sheet 7

Can not read from inputstream in FileMode.Append,FileAccess.Write

I am trying to read a excel file using
using (var stream = new FileStream(filepath, FileMode.Append,FileAccess.Write))
{
ExcelWorksheet worksheet;
using (ExcelPackage pck = new ExcelPackage(stream))
actually I am trying to add a worksheet in existing excel file so that's why I am using append , and when I used filemode.create then it is overtiring my existing tab with the new one so what should I do?
Just use the following code and it will add a new worksheet to your current file:
FileInfo newFile = new FileInfo("YourFile.xlsx");
using (ExcelPackage p = new ExcelPackage(newFile))
{
p.Workbook.Worksheets.Add("YourNewSheet");
p.Save();
}
You can do the same actions easily with Microsoft open xml
https://learn.microsoft.com/en-us/office/open-xml/how-to-get-worksheet-information-from-a-package

Exporting large(>500000 rows) Datasets into xlsx

I have problem with export dataset into xlsx. My code:
DataSet freeTable = new DataSet();
freeTable = FoxDal.Instance.freeTable;
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(freeTable.Tables[0], "sheet1");
wb.SaveAs(#"E:\test5.xlsx");
While running code VS give's me prompt ( asking me where is XLReentrantEnumerableSet.cs) if i dont choose it VS throw exception System.OutOfMemoryException. Im using ClosedXML. There are some attached libraries for this framework? With small tables my code working corectly.
I change framework to EPPlus and now i can convert large files.
My new code:
DataSet freeTable = new DataSet();
freeTable = FoxDal.Instance.freeTable;
FileInfo fileInfo = new FileInfo(#"E:\Test.xlsx");
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("table");
ws.Cells["A1"].LoadFromDataTable(freeTable.Tables[0], true);
pck.SaveAs(fileInfo);
}

Updating the excelsheet using EPPlus in c#

I have a excel file which contains sheet named Data. This sheet already contains few data. I need to open this file and add more data in it. I have tried searching about this but everyone is just creating new sheets in their workbook. I need to update the current sheet. Below is my code:
MemoryStream ms = new MemoryStream();
using (FileStream fs = File.OpenRead(#"Path\File.xlsx")
using (ExcelPackage excelPackage = new ExcelPackage(fs))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells[1, 1].Value = "Test";
excelWorksheet.Cells[3, 2].Value = "Test2";
excelWorksheet.Cells[3, 3].Value = "Test3";
excelPackage.SaveAs(ms);
}
but it didnt update the sheet. I do not what I am doing wrong. Can anyone help me please. Thanks
You are doing this:
Create a MemoryStream object in memory to store binary data.
Open file and read it into the MemoryStream object.
Create an ExcelPackage object based on the data in the MemoryStream object.
Make changes to the spreadsheet.
Save changes back to the MemoryStream object.
That's why the spreadsheet file does not get updated.
Use FileInfo and open the file directly with ExcelPackage:
// using System.IO;
FileInfo file = new FileInfo(#"Path\File.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells[1, 1].Value = "Test";
excelWorksheet.Cells[3, 2].Value = "Test2";
excelWorksheet.Cells[3, 3].Value = "Test3";
excelPackage.Save();
}
Try this
var file = new FileInfo(path);
if (file .Exists)
{
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
ExcelWorksheet ws = excelPackage.Workbook.Worksheets.First();
ws.Cells[1,1].Value = "Test1";
excelPackage.Save();
}
}

ExcelPackage: Office Open XML

I am new to this Open Office XML and I was wondering what file extension the XLPackage takes.
For example I assumed I just needed to input the file location of a CSV file I am using, but it does not work, do I have to convert the file to .xlsx or is there something other then the XLPackage that I should use?
The problem is that once it gets to the using a new OpenDialog is initiated and I cant find my file. I am probably just missing something obvious. File Contains Corrupt data, FileFormatException, I assume I need to convert the file before use?
I appreciate any feedback.
Some code:
FileInfo existingFile = new FileInfo(eFilePath);
using (ExcelPackage xlPackage = new ExcelPackage(existingFile)) // I think the issue is here.
{
ExcelWorksheet exeedSheet = xlPackage.Workbook.Worksheets[1];
//Total rows
for (int row = 1; row > 0; )
If you are working with a CSV the ExcelPackage is overkill for what you are doing.
CSV:
using (var Sr = new StreamReader("\\SomeCoolFile.CSV"))
{
var text = Sr.ReadToEnd();
Sr.Close();
text = text.Replace("\n", string.Empty);
var lines = text.Split('\r');
var info = lines.Select(line => line.Split(',')).ToList();
......
}
ExcelPackage:
using (var fs = new FileStream("\\SomeCoolFile.xlsx", FileMode.Open))
{
using (var package = new ExcelPackage(fs))
{
var workBook = package.Workbook;
.....
}
}

Categories